1.服务器端配置
服务端使用Ubuntu 16.0搭建
①:准备软件环境
(1) java linux版本,最好是1.8
下载地址:http://www.oracle.com/technetwork/java/javase/downloads /jdk8-downloads-2133151.html
②:Hadoop 下载,我用的hadoop2.6.x
下载地址:http://hadoop.apache.org/releases.html
③:解压配置
使用 Master,Slaver1,Slaver2作为集群主机名
使用tar -zxvf 包名,将java,hadoop解压到/usr/local/下
④:在~/.bashrc 下,配置环境:
Vim ~/.bashrc
如图所示,JAVA_HOME 表示配置java的环境,HADOOP_HOME 表示配置hadoop的环境,最后配置PATH.
⑤:安装ssh服务
使用 apt-get install openssh-server 安装ssh服务
免密码登录ssh:
cd ~
使用 ssh-keygen -t rsa -P “”
就可生成公共秘钥id_rsa.pub
使用 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys,写入公共秘钥,接着Slaver1,Slaver2按照此方法,生成此id_rsa.pub,接着在相应的注意上分别改成slaver1.pub,slaver2.pub。
利用scp slaver1.pub root@Master:~/.ssh/
将slaver1.pub slaver2.pub 分别复制到主机的 ~/.ssh/下
利用cat ~/.ssh/slaver1.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/slaver2.pub >> ~/.ssh/authorized_keys写到Master机器上,接着将Master上的authorized_keys分别复制到对应的~/.ssh/下,ssh免密码登录配置完成。
⑥:配置hadoop
cd /usr/local/Hadoop/etc/Hadoop/
配置core-site.xml
配置hdfs-site.xml
配置mapred-site.xml.template
配置slaves文件
丛机也一样。
配置hadoop-env.sh,配置java环境
配置/etc/hosts文件
配置主从机。
至此hadoop集群配置结束。
二.window eclipse开发环境搭建:
首先下载hadoop+eclipse plugin2.6,下载完成之后,将
将hadoop-eclipse-plugin-2.6.0.jar放在eclipse的plugin的目录下,启动eclipse
在window—》preference下,看到:
看到上图Hadoop Map/Reducer证明hadoop插件安装成功。
eclipse下配置hadoop:
配置本地hadoop解压的目录,另外在window下也要配置hadoop环境
eclipse中配置Map/Reducer
在eclipse看到这些,证明Map/Reducer搭建好了
三.wordcount代码展示:
mapper:
/**
* IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口,
* 都能够被串行化从而便于在分布式环境中进行数据交换,你可以将它们分别视为int,String 的替代品。 声明one常量和word用于存放单词的变量
*/
public class WordCount_Mapper extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
/**
* Mapper中的map方法: void map(K1 key, V1 value, Context context)
* 映射一个单个的输入k/v对到一个中间的k/v对 输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。
* Context:收集Mapper输出的<k,v>对。 Context的write(k, v)方法:增加一个(k,v)对到context
* 程序员主要编写Map和Reduce函数.这个Map函数使用StringTokenizer函数对字符串进行分隔,通过write方法把单词存入word中
* write方法存入(单词,1)这样的二元组到context中
*/
@Override
protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
String text = tokenizer.nextToken();
word.set(text);
context.write(word, one);
}
}
}
Reducer:
public class WordCount_Reducer extends Reducer<Text, IntWritable, Text, IntWritable>{
/**
* Reducer类中的reduce方法:
* void reduce(Text key, Iterable<IntWritable> values, Context context)
* 中k/v来自于map函数中的context,可能经过了进一步处理(combiner),同样通过context输出
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
for(IntWritable val:values){
sum+=val.get();
}
context.write(key, new IntWritable(sum));
}
}
Main:
public class WordCount_Main {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
/**
* Configuration:map/reduce的配置类,向hadoop框架描述map-reduce执行的工作
*/
Configuration conf = new Configuration();
//设置一个用户定义的job名称
Job job =new Job(conf,"wordCount");
job.setJarByClass(WordCount_Main.class);
//为job设置输入路径
FileInputFormat.addInputPath(job, new Path("hdfs://192.168.1.129:9000/myHdfs/word/data"));
//为job设置输出路径
FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.1.129:9000/myHdfs/word/word_result"));
//为job设置Mapper类
job.setMapperClass(WordCount_Mapper.class);
//为job设置Combiner类
job.setCombinerClass(WordCount_Reducer.class);
//为job设置Reducer类
job.setReducerClass(WordCount_Reducer.class);
//为job的输出数据设置Key类
job.setOutputKeyClass(Text.class);
//为job输出设置value类
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true)? 0 : 1);
}
}
运行过程可能会报错。
错误一:
在hadoop_home/bin下加入:
错误二:
在windows/system32下加入:
这样就不会报错了。
另外注意一点,在windows下配置完hadoop的环境之后,会莫名的报错java环境配置不正确,这是因为java环境默认配置在:
有空格,就会报错找不到java路径,解决方法:
①:更换java路径,注意无空格路径
②:打开hadoop的安装路径下的etc下的hadoop下的hadoop-env.cmd
修改:
至此,hadoop的服务与开发环境搭建完毕,有什么问题,欢迎大家指正。如果大家有不明白的地方,也欢迎给我留言。