简单记录在CentOS下安装Eclipse测试Hadoop的过程。
(一),安装eclipse
1,下载eclipse,点这里
2,将文件上传到Centos7,可以用WinSCP
3,解压并安装eclipse
[root@Master opt]# tar zxvf '/home/s/eclipse-jee-neon-1a-linux-gtk-x86_64.tar.gz' -C/opt ---------------> 建立文件:[root@Master opt]# mkdir /usr/bin/eclipse ------------------》添加链接,即快捷方式:[root@Master opt]# ln -s /opt/eclipse/eclipse /usr/bin/eclipse -----------》点击eclipse,即可启动了
(二),建立Hadoop项目
1,下载hadoop plugin 2.7.3 链接: 密码:ms91
2,解压上述jar包插件,放到eclipse中plugins中,并重启eclipse
2, 在eclipse中加载dfs库,点击Windows 工具栏-------->选择show view如图:
2,打开resource 点击Window ----->Perspective----------->open Perspective 选择resource:
3,配置连接端口,点击eclipse下放的MapResource Location,点击添加:其中port号按照hdfs-site.xml 和core-site.xml来填写。
4,上传输入文件:使用hdfs dfs -put /home/file1 /data 即可在eclipse中看到如下:(要确保各个机器的防火墙都关闭,出现异常可以暂时不用关,后面跑下例子就全没了,呵呵)
(三),测试WordCount程序
1,新建项目:点击new ------------》project ----------->Map Reduce,如图:
2,给项目配置本地的hadoop文件,圆圈处写本地hadoop的路径:
3,新建个mappert类,写如下代码:
package word;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class mapper {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println(otherArgs.length);
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(mapper.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.out.print("ok");
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
2,点击run as ------------>RunConfigurations ---------->设置input和output文件参数
3,点击run,查看结果
文件的内容: