初学Hadoop之中文词频统计

一、安装eclipse

准备java

  eclipse-dsl-luna-SR2-linux-gtk-x86_64.tar.gzlinux

安装apache

  一、解压文件。vim

   

  

  二、建立图标。app

ln -s /opt/eclipse/eclipse /usr/bin/eclipse  #使符号连接目录
vim /usr/share/applications/eclipse.desktop  #建立一个  Gnome 启动

  

  添加以下代码:eclipse

[Desktop Entry]
Encoding=UTF-8
Name=Eclipse 4.4.2
Comment=Eclipse Luna
Exec=/usr/bin/eclipse
Icon=/opt/eclipse/icon.xpm
Categories=Application;Development;Java;IDE
Version=1.0
Type=Application
Terminal=0

   

  完成之后则会出现下图中的图标。oop

   

  至此,eclipse安装完成。学习

二、安装hadoop插件

  一、下载插件http://pan.baidu.com/s/1ydUEygoogle

  二、将插件放到/opt/eclipse/plugins文件夹下。spa

   

  三、在eclipse->Windows->preferences设置Hadoop路径。

   

  至此,插件安装完成。

三、ChineseWordCount源码

package com.example.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.ByteArrayInputStream;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class ChineseWordCount {

    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 {

            byte[] bt = value.getBytes();
            InputStream ip = new ByteArrayInputStream(bt);
            Reader read = new InputStreamReader(ip);
            IKSegmenter iks = new IKSegmenter(read, true);
            Lexeme t;
            while ((t = iks.next()) != null) {
                word.set(t.getLexemeText());
                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 {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args)
                .getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println("Usage: wordcount <in> <out>");
            System.exit(2);
        }
        Job job = new Job(conf, "word count");
        job.setJarByClass(ChineseWordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

四、建立Hadoop工程

   一、建立一个Map/Reduce Project,名称为ChineseWordCount。

  

  

  二、建立包com.example.test,建立ChineseWordCount类文件。

  

  

  三、导入IkAnalyzer包。

  下载地址:http://code.google.com/p/ik-analyzer/

  

  至此,Hadoop工程新建完成。

五、运行工程

  一、在/home/hadoop/目录下新建一个input文件夹,将中文文本"悟空传.txt"复制到里面。

  

  

  二、在eclipse中设置运行参数。

  操做时遇到一个问题,当运行参数设置成/opt/hadoop-2.6.0/input/*.* /opt/hadoop-2.6.0/output时,没法运行成功,我想会不会是访问权限的问题,这个下次再解决。

  

  三、点击运行。

  

六、查看结果

  

  

  

  至此,中文词频统计运行成功。

七、总结

  此次的中文词频统计只是一个简单的实验,还须要继续完善统计功能,好比词频数量的排序,去除单字统计等等。这方面我接触的还不深,但愿有经验的朋友能给我一些学习建议和意见,谢谢。

相关文章
相关标签/搜索