Hadoop运行wordcount示例

1.首先用hdfs namenode -format和start-all.sh启动Hadoop
在这里插入图片描述
2.使用HDFS运行WordCount程序
(1).在云端创建一个/data/input的文件夹结构
bin/hdfs dfs -mkdir -p /data/input
(2).在本地创建一个test.txt
touch test.txt
(3).编辑test.txt文件,输入一些字符
nano test.txt
在这里插入图片描述
3.把本地的test.txt文件上传到云端,由主机进行文件的分布式存储。
bin/hdfs dfs -put test.txt /data/input在这里插入图片描述
4.运行share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar这个java程序,调用wordcount方法:
bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount /data/input/test.txt /data/out/test
查看云端的/data/output/test/part-r-00000文件。
bin/hdfs dfs -cat /data/out/test/part-r-00000
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop wordcount编程题是一个经典的MapReduce编程题,主要是用来统计给定文本文件中各个单词出现的次数。下面是Hadoop wordcount编程题的代码示例和写答案的步骤: Step 1:编写Mapper类 Mapper类是MapReduce程序中的一个组件,主要负责将输入数据转换成中间数据,并输出键值对。在Hadoop wordcount编程题中,我们可以编写一个Mapper类,将文本文件中的每个单词作为键,出现次数作为值,输出键值对。 示例代码如下: ``` public class WordCountMapper extends 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, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } ``` Step 2:编写Reducer类 Reducer类是MapReduce程序中的另一个组件,主要负责将中间数据进行合并和计算,并输出最终结果。在Hadoop wordcount编程题中,我们可以编写一个Reducer类,对每个单词出现的次数进行求和,并输出最终结果。 示例代码如下: ``` public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } ``` Step 3:编写Driver类 Driver类是MapReduce程序的入口,主要负责调用Mapper和Reducer类,并设置相关的参数和配置。在Hadoop wordcount编程题中,我们可以编写一个Driver类,设置输入输出路径和相关配置参数。 示例代码如下: ``` public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` Step 4:提交任务并查看结果 将上述代码保存为Java文件并编译打包,然后将打包后的Jar文件上传到Hadoop集群上,并执行以下命令提交任务: ``` hadoop jar wordcount.jar input_path output_path ``` 其中,input_path和output_path分别是输入和输出路径。 最后,在Hadoop集群上执行以下命令查看结果: ``` hadoop fs -cat output_path/part-r-00000 ``` 以上就是Hadoop wordcount编程题的代码示例和写答案的步骤。当然,具体的实现细节和操作步骤可能会有所不同,需要根据实际情况进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值