eclipse 运行WordCount(附源码)
2016-10-23 00:00
316 查看
摘要: 2.7.1 版本hadoop
1.下载hadoop插件(hadoop下载包里好像有这个插件)
hadoop-eclipse-plugin-2.7.1分享链接 https://pan.baidu.com/s/1sldBu9n
放到eclipse/plugins文件夹下,重启eclipse
2.window -> preferences 点击确定 找到 hadoop map/reduce 在右窗口填上hadoop安装地址
3.出现一个和控制台一样位置的map/reduce location ,右击空白处 选择new hadoop location
loaction name填上名字,Map/Reduce (V2) Master的端口填mapred-site.xml端口 。DFS Master填core-site.xml 确定。host都是填localhost。
4.File-->New-->Other-->Map/Reduce Project 创建文件 取名 新建java文件代码如下
5.源代码如下
ps:注意一下注释部分,需要确认你需要计算的文件
6.直接运行 或者导出
7.如果导出,运行命令:
hadoop jar .jar路径 运行的类(含包路径) 类的参数
hadoop jar /home/user/xxx.jar com.filex.WordCount (输入输出文件已经设置好)
hadoop jar /home/user/xxx.jar com.filex.WordCount in put ( 输入输出文件未设置好)。
1.下载hadoop插件(hadoop下载包里好像有这个插件)
hadoop-eclipse-plugin-2.7.1分享链接 https://pan.baidu.com/s/1sldBu9n
放到eclipse/plugins文件夹下,重启eclipse
2.window -> preferences 点击确定 找到 hadoop map/reduce 在右窗口填上hadoop安装地址
3.出现一个和控制台一样位置的map/reduce location ,右击空白处 选择new hadoop location
loaction name填上名字,Map/Reduce (V2) Master的端口填mapred-site.xml端口 。DFS Master填core-site.xml 确定。host都是填localhost。
4.File-->New-->Other-->Map/Reduce Project 创建文件 取名 新建java文件代码如下
5.源代码如下
package com.filex; 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.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Mapper.Context; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.Reducer.Context; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(WordCount.class); job.setJobName("wordcount"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(WordCountMap.class); job.setReducerClass(WordCountReduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// //下面的两句代码,其中参数意义 //hdfs://localhost:9000/in 表示需要计数的文件夹 计算命令行下:hadoop fs -ls /in 出现的文件 //hdfs://localhost:9000/output 表示储存结果的文件夹(不要创建,同时之前不要存在这个文件夹) //new Path(arg[0]) new Path(arg[1])也可以使用命令行传参的方式传入两个文件夹(不可以直接运行) // FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/in")); FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output")); job.waitForCompletion(true); } public static class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> { private final IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer token = new StringTokenizer(line); while (token.hasMoreTokens()) { this.word.set(token.nextToken()); context.write(this.word, this.one); } } } public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> { public 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)); } } }
ps:注意一下注释部分,需要确认你需要计算的文件
6.直接运行 或者导出
7.如果导出,运行命令:
hadoop jar .jar路径 运行的类(含包路径) 类的参数
hadoop jar /home/user/xxx.jar com.filex.WordCount (输入输出文件已经设置好)
hadoop jar /home/user/xxx.jar com.filex.WordCount in put ( 输入输出文件未设置好)。
相关文章推荐
- win7(64位)平台下Cygwin+Eclipse搭建Hadoop单机开发环境 (四) 导入Hadoop源码+wordcount程序+运行
- Hadoop2.2.0源码分析(一)——Eclipse运行WordCount.java
- cygwin+hadoop+eclipse (三) 运行wordcount实例
- 解决Eclipse中运行WordCount出现 java.lang.ClassNotFoundException: org.apache.hadoop.examples.WordCount$TokenizerMapper问题【转】
- linux下在eclipse上运行hadoop自带例子wordcount
- Eclipse运行Hadoop WordCount例程
- eclipse运行wordcount
- eclipse运行wordcount参数配置
- 在windows上用eclipse远程运行hadoop上的wordcount程序出现的问题,求解决
- eclipse运行wordcount参数配置
- eclipse运行WordCount程序记录
- Eclipse运行wordcount步骤
- Eclipse下运行hadoop自带的mapreduce程序--wordcount
- Hadoop + eclipse + linux 单机运行 WordCount
- Eclipse 执行成功的 Hadoop-1.2.1 WordCount 源码
- linux下eclipse上运行hadoop自带wordcount程序
- Hadoop不用eclipse去编译运行WordCount
- Hadoop在Linux下伪分布式的安装以及wordcount实例的运行还有Eclipse的使用
- eclipse运行wordcount
- fedora17中hadoop2.2.0在eclipse下运行wordcount