Hadoop编程 实现统计单词词频并找出词频最高的单词(hadoop3.1.1 Centos7)

 

不能盲目寻找一些技术博文来浏览,非常浪费时间,最靠谱的方式莫过于官方文档

hadoop3.1.1 运行词频统计的官方指导文档如下:

https://hadoop.apache.org/docs/r3.1.1/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html

按照文档所提示的内容主要可分为以下几个步骤:

1.配置/etc/profile

export JAVA_HOME=/usr/java/default
export PATH=${JAVA_HOME}/bin:${PATH}
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar

2.将程序移至 hadoop-X.X.X目录下,打包成wc.jar ,执行以下命令

$ bin/hadoop com.sun.tools.javac.Main WordCount.java
$ jar cf wc.jar WordCount*.class

3.运行jar文件,后面为两文件在HDFS中的绝对路径,参数的设置取决于程序

 bin/hadoop jar wc.jar WordCount /user/joe/wordcount/input /user/joe/wordcount/output

 

实验三:HDFS操作方法和基础编程实验 1. 实验目的 了解HDFS的基本操作方法和基础编程实验,掌握Hadoop文件系统的操作。 2. 实验环境 - 操作系统:Windows 10 - 虚拟机软件:VMware Workstation 15 Pro - 虚拟机操作系统:CentOS 7 - Hadoop版本:2.7.7 3. 实验步骤 3.1 HDFS操作方法 3.1.1 启动HDFS服务 在CentOS 7中打开终端,输入以下命令启动HDFS服务: ``` start-dfs.sh ``` 3.1.2 创建文件夹 HDFS中的文件夹称为目录,使用以下命令在HDFS中创建一个目录: ``` hadoop fs -mkdir /test ``` 3.1.3 上传文件 使用以下命令将本地文件上传到HDFS中的目录: ``` hadoop fs -put /opt/test.txt /test ``` 3.1.4 下载文件 使用以下命令将HDFS中的文件下载到本地: ``` hadoop fs -get /test/test.txt /opt ``` 3.1.5 查看文件 使用以下命令查看HDFS中的文件: ``` hadoop fs -ls /test ``` 3.1.6 删除文件 使用以下命令删除HDFS中的文件: ``` hadoop fs -rm /test/test.txt ``` 3.2 基础编程实验 3.2.1 实验要求 编程实现一个完整的Hadoop MapReduce程序,实现词频统计功能。 3.2.2 实验步骤 3.2.2.1 编写Mapper类 在Eclipse中新建一个Java项目,创建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); } } } ``` 3.2.2.2 编写Reducer类 创建Reducer类,代码如下: ``` public class WordCountReducer 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); } } ``` 3.2.2.3 编写Driver类 创建Driver类,代码如下: ``` 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.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); } } ``` 3.2.2.4 打包 右键项目,选择Export,选择JAR file,点击Next,选择要导出的项目和要导出的类,点击Next,选择要导出的JAR文件路径和文件名,点击Finish,即可生成JAR文件。 3.2.2.5 运行 使用以下命令在Hadoop集群上运行程序: ``` hadoop jar /opt/wordcount.jar WordCount /input /output ``` 其中,/input是输入文件所在的目录,/output是输出文件所在的目录。 4. 实验结果与结论 经过以上步骤,我们可以成功地完成HDFS操作方法和基础编程实验,从而掌握了Hadoop文件系统的操作。同时,我们还通过编写MapReduce程序实现词频统计功能,进一步加深了对Hadoop的理解和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值