Hadoop学习笔记:(一)WordCount运行
2016-07-10 10:51
651 查看
前言:本文是在hadoop已经配置好的情况下
WordCount是hadoop下的HelloWorld程序,是初学者必须要会的。下面是用eclipse进行开发
一、工程与MapReduce代码
新建工程,创建WordCount class
下面的代码是旧版mapreduce
上述代码的运行成功离不开很多jar包,这些jar包主要在安装的hadoop文件夹里面。具体位置参考:
HADOOP_HOME/share/hadoop/
这里的HADOOP_HOME是你安装hadoop的路径。
这个目录下包含下面文件夹:
common
httpfs
hdfs
mapreduce
tool
yarn
其中我们的程序需要从common, mapreduce, yarn文件夹里添加存在的所有jar包以及lib下的所有jar包(这可能有重复,只要相同的覆盖即可)
添加好jar包后,就可以运行程序了。这里我们要为程序配置两个输入。
本地测试时,直接在eclipse点击run configuration ,在arguments下的program argument下添加要统计的文件地址以及输出文件路径。
1.本地模式:测试文件路径 输出文件路径(数据都在本地)
2.HDFS文件:hdfs://localhost/测试文件 hdfs://localhost/输出目录(数据在HDFS中)
配置好了,就可以点击run运行了。
另外:
也可以用命令行
打包:用eclipse将工程打包.
执行:hadoop jar wordcount.jar input output
这里的input最好写成HDFS下的文件路径.(本地的不知道怎么表示,试了下老报错),好像hadoop它会默认加载hdfs的路径.
集群模式:
先将文件复制到HDFS上:hadoop dfs -copyFromLocal 本地文件 hdfs路径
然后命令行运行:hadoop jar wordcout.jar WordCout input output
运行wordcount.jar中的WordCount类,input作为输入,output作为输出.
WordCount是hadoop下的HelloWorld程序,是初学者必须要会的。下面是用eclipse进行开发
一、工程与MapReduce代码
新建工程,创建WordCount class
下面的代码是旧版mapreduce
package mapreduce; import java.io.IOException; import java.util.Iterator; import java.util.StringTokenizer; 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.mapred.FileInputFormat; import org.apache.hadoop.mapred.FileOutputFormat; import org.apache.hadoop.mapred.JobClient; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.MapReduceBase; import org.apache.hadoop.mapred.Mapper; import org.apache.hadoop.mapred.OutputCollector; import org.apache.hadoop.mapred.Reducer; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.mapred.TextInputFormat; import org.apache.hadoop.mapred.TextOutputFormat; public class WordCount { public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while(tokenizer.hasMoreTokens()){ word.set(tokenizer.nextToken()); output.collect(word, one); } } } public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { int sum = 0; while(values.hasNext()){ sum += values.next().get(); } output.collect(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { JobConf conf = new JobConf(WordCount.class); conf.setJobName("wordcount"); conf .setOutputKeyClass(Text.class); conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(Map.class); conf.setReducerClass(Reduce.class); conf.setInputFormat(TextInputFormat.class); conf.setOutputFormat(TextOutputFormat.class); FileInputFormat.setInputPaths(conf, new Path(args[0])); FileOutputFormat.setOutputPath(conf, new Path(args[1])); JobClient.runJob(conf); } }
上述代码的运行成功离不开很多jar包,这些jar包主要在安装的hadoop文件夹里面。具体位置参考:
HADOOP_HOME/share/hadoop/
这里的HADOOP_HOME是你安装hadoop的路径。
这个目录下包含下面文件夹:
common
httpfs
hdfs
mapreduce
tool
yarn
其中我们的程序需要从common, mapreduce, yarn文件夹里添加存在的所有jar包以及lib下的所有jar包(这可能有重复,只要相同的覆盖即可)
添加好jar包后,就可以运行程序了。这里我们要为程序配置两个输入。
本地测试时,直接在eclipse点击run configuration ,在arguments下的program argument下添加要统计的文件地址以及输出文件路径。
1.本地模式:测试文件路径 输出文件路径(数据都在本地)
2.HDFS文件:hdfs://localhost/测试文件 hdfs://localhost/输出目录(数据在HDFS中)
配置好了,就可以点击run运行了。
另外:
也可以用命令行
打包:用eclipse将工程打包.
执行:hadoop jar wordcount.jar input output
这里的input最好写成HDFS下的文件路径.(本地的不知道怎么表示,试了下老报错),好像hadoop它会默认加载hdfs的路径.
集群模式:
先将文件复制到HDFS上:hadoop dfs -copyFromLocal 本地文件 hdfs路径
然后命令行运行:hadoop jar wordcout.jar WordCout input output
运行wordcount.jar中的WordCount类,input作为输入,output作为输出.
相关文章推荐
- Linux(CentOS)挂载U盘、移动硬盘以及文件拷贝、备份
- 图解Linux命令之--file命令
- Part0:认识重构
- linux more命令简单使用
- Linux下的tar压缩解压缩命令详解
- Linux守护进程加上发送信号固定模式
- Linux学习----vim编辑器
- Docker镜像和仓库笔记
- linux启动mysql
- Java千百问_02基本使用(013)_linux系统如何管理环境变量
- apache日志类型及作用
- linux关于readlink函数获取运行路径的小程序
- linux xfs和ext4的区别
- 图解Linux命令之--cmp命令
- Linux命令(004) -- watch
- 初玩OpenWRT
- opengl 笔记(1)
- shell
- 快速解决tomcat指定的服务未安装问题
- centos7和centos6的区别