準備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安裝完成。學習
一、下載插件http://pan.baidu.com/s/1ydUEy 。google
二、將插件放到/opt/eclipse/plugins文件夾下。spa
三、在eclipse->Windows->preferences設置Hadoop路徑。
至此,插件安裝完成。
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); } }
一、建立一個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時,沒法運行成功,我想會不會是訪問權限的問題,這個下次再解決。
三、點擊運行。
至此,中文詞頻統計運行成功。
此次的中文詞頻統計只是一個簡單的實驗,還須要繼續完善統計功能,好比詞頻數量的排序,去除單字統計等等。這方面我接觸的還不深,但願有經驗的朋友能給我一些學習建議和意見,謝謝。