第一个Hadoop程序WordCount

参考博客:ubuntu安装hadoop详细步骤

环境:
ubuntu16.04LTS+Hadoop2.7.4
配置可参考ubuntu16.04 Hadoop安装

  1. 本地新建一个文件,笔者在~/Documents目录下新建了一个wordCount.txt文档,里面的内容可以随便填写。

    I have an apple
    I have a pen
    emmmmm
    applepen

  2. 在HDFS中新建一个文件夹,用于上传本地的txt文档。
    在hadoop2.7.4目录下输入如下命令:
    bin/hdfs dfs -mkdir /test ,表示在hdfs的根目录下建立了一个test目录。
    使用bin/hdfs dfs -ls / 可查看目录。

  3. 将本地words文档上传到test目录中。
    使用如下命令进行上传操作:
     bin/hdfs dfs -put /home/leesf/words /test/
    使用如下命令进行查看
    bin/hdfs dfs -ls /test/

  4. 运行wordcount
    使用如下命令运行wordcount:

bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.4.jar wordcount /test/WordCount.txt /test/out

运行完成后,在/test目录下生成名为out的文件,使用如下命令查看/test目录下的文件:
bin/hdfs dfs -ls /test
    
5. 查看运行结果
 使用如下命令查看运行结果:
  bin/hadoop fs -cat /test/out/part-r-00000

然而我的虚拟机跑了20分钟也没跑完这个程序。。。
问题出在yarn上了。yarn的配置对虚拟内存,磁盘等有很多要求,按很多博客都没配置好,于是换了一种配置方式。

经过几天调试发现问题,根据ubuntu16.04 Hadoop安装安装的hadoop调用了yarn程序,但是在单机上运行这个就跑不起来,于是参考博客ubuntu安装hadoop详细步骤
重新修改了mapred-site.xml.template文件,

<configuration>
  <property>
  <name>mapreduce.framework.name</name>
  <value>yarn</value>
  </property>
</configuration>

更改为

<configuration>
   <property>
     <name>mapred.job.tracker</name>
       <value>localhost:9001</value>
   </property>
</configuration>

其他配置文件不变。
之后就不需要调用yarn就可以直接运行了。里面具体都是些什么还不太了解,先配好环境慢慢学吧!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Hadoop第一个WordCount程序是一个非常简单的程序,它的主要目的是计算一个文本文件中每个单词出现的次数。 以下是一个基本的WordCount程序: 1. 创建一个Java项目并导入Hadoop库。 2. 创建一个Java类并实现以下Mapper和Reducer: Mapper类: ```java import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> { private final static LongWritable one = new LongWritable(1); private Text word = new Text(); @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = line.split(" "); for (String w : words) { word.set(w); context.write(word, one); } } } ``` Reducer类: ```java import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> { @Override public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { long sum = 0; for (LongWritable val : values) { sum += val.get(); } context.write(key, new LongWritable(sum)); } } ``` 3. 在应用程序的main()方法中,创建一个Job并设置Mapper和Reducer类: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 4. 在命令行中运行程序,并指定输入和输出文件路径: ```bash hadoop jar WordCount.jar WordCount /input /output ``` 其中,/input是输入文件路径,/output是输出文件路径。 这是最基本的WordCount程序,你可以根据需要进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值