分析和编写WordCount程序

1)采用Eclipse/IDEA创建一个Maven工程

2)修改pom.xml,增加dependencies,/dependencies、build ,/build节点,如下:

增加依赖

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.7.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-core</artifactId>
      <version>2.7.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.7.7</version>
    </dependency>
  </dependencies>

在build下增加以下内容

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <archive>
            <manifest>
              <!-- main()所在的类,注意修改 -->
              <mainClass>org.example.WordCountMain</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

上面的org.example.WordCountMain需要根据主类进行修改。

3)编写Wordcount代码
首先创建一个Java class,写入代码如下:

package org.example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.util.StringTokenizer;

public class WordCountMain {
    public static class TokenizerMapper extends  Mapper<Object, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }


    public static class IntSumReducer 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);
        }
    }



    public static void main(String[] args) throws Exception {
        if (args == null || args.length < 3) {
            args[0] = "wordcount";
            args[1] = "/input/word.txt";
            args[2] = "/output/wordcountpara1";
        }
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, args[0]);
        job.setJarByClass(WordCountMain.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setInputFormatClass(NLineInputFormat.class);
        // 输入文件路径
        FileInputFormat.addInputPath(job, new Path(args[1]));
        // 输出文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[2]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

4)使用mvn clean package命令打成jar包,将jar包复制到桌面,然后上传到ubuntu服务器本地上

5)使用如下命令运行jar包

hadoop jar shiyan4-1.0-SNAPSHOT.jar org.example.WordCountMain /input/data.txt /output/wx

shiyan4-1.0-SNAPSHOT.jar是jar包的名字
org.example.WordCountMain是类名(与上面修改pom.xml里面的类名一致)
/input/data.txt是hdfs上输入的文件的路径
/output/wx是输出的路径

6)运行结束后使用如下命令就可以查看到运行结果:

hdfs dfs -cat /output/wx/part-r-00000

/output/wx/part-r-00000是输出文件的名字(可在hdfs上自行查看)

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Wordcount程序是一种常见的文本处理程序,用于计算一段文本中单词的出现频率。下面是分析编写Wordcount程序的步骤: 1. 读取文本文件 首先,我们需要读取待处理的文本文件。这可以通过Python的内置函数`open()`和`read()`来实现。`open()`函数用于打开文件,`read()`函数用于读取文件内容。 2. 分割单词 读取文本文件后,我们需要将文本文件中的内容分割成单词。这可以通过Python的内置函数`split()`来实现。`split()`函数会将文本字符串分割成一个单词列表。 3. 统计单词出现的频率 接下来,我们需要统计每个单词在文本中出现的频率。这可以通过Python的字典数据结构来实现。我们可以遍历单词列表,对于每个单词,将其作为字典的键,如果单词已经出现过,则将其对应的值加一,否则将其添加到字典中,初始值为1。 4. 输出单词频率 最后,我们需要将统计结果输出。这可以通过遍历字典,将每个键值对输出到屏幕或写入到文件中来实现。 下面是一个示例代码: ```python # 读取文本文件 with open('text.txt', 'r') as f: text = f.read() # 分割单词 words = text.split() # 统计单词出现的频率 word_freq = {} for word in words: if word in word_freq: word_freq[word] += 1 else: word_freq[word] = 1 # 输出单词频率 for word, freq in word_freq.items(): print(word, freq) ``` 当然,这只是一个简单的示例程序,实际的Wordcount程序可能还需要考虑一些其他的问题,例如去除标点符号和停用词等。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值