初學Hadoop之中文詞頻統計


1、安裝eclipse

准備

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

安裝

  1、解壓文件。

   

  

  2、創建圖標。

ln -s /opt/eclipse/eclipse /usr/bin/eclipse  #使符號鏈接目錄
vim /usr/share/applications/eclipse.desktop  #創建一個  Gnome 啟動

  

  添加如下代碼:

[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

   

  完成以后則會出現下圖中的圖標。

   

  至此,eclipse安裝完成。

2、安裝hadoop插件

  1、下載插件http://pan.baidu.com/s/1ydUEy

  2、將插件放到/opt/eclipse/plugins文件夾下。

   

  3、在eclipse->Windows->preferences設置Hadoop路徑。

   

  至此,插件安裝完成。

3、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);
    }
}

4、創建Hadoop工程

   1、創建一個Map/Reduce Project,名稱為ChineseWordCount。

  

  

  2、創建包com.example.test,創建ChineseWordCount類文件。

  

  

  3、導入IkAnalyzer包。

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

  

  至此,Hadoop工程新建完成。

5、運行工程

  1、在/home/hadoop/目錄下新建一個input文件夾,將中文文本"悟空傳.txt"復制到里面。

  

  

  2、在eclipse中設置運行參數。

  操作時遇到一個問題,當運行參數設置成/opt/hadoop-2.6.0/input/*.* /opt/hadoop-2.6.0/output時,無法運行成功,我想會不會是訪問權限的問題,這個下次再解決。

  

  3、點擊運行。

  

6、查看結果

  

  

  

  至此,中文詞頻統計運行成功。

7、總結

  這次的中文詞頻統計只是一個簡單的實驗,還需要繼續完善統計功能,比如詞頻數量的排序,去除單字統計等等。這方面我接觸的還不深,希望有經驗的朋友能給我一些學習建議和意見,謝謝。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM