1. 程式人生 > >centos下使用Intellij IDEA 編寫 word count

centos下使用Intellij IDEA 編寫 word count

在CentOS 使用 ideaIC 開發工具 ideaIC

準備 idealC 安裝包

ideaIC-2016.3.tar.gz  下載地址

上傳到centos伺服器上解壓

tar -xzvf ideaIC-2016.2.3.tar.gz
進入bin目錄開啟idea.sh 進行安裝


一切按照預設安裝完成
建立專案 create new project


選擇預設帶的JDK 也可以 new sdk 選擇本地安裝的jdk也

下一步 template 不選 直接下一步 輸入專案名稱 WordCount 點選 Finish

寫一個 main 方法測試一下 成功輸出test...

開始編寫 wordcount
匯入hadoop包 (可以把所有jar放到一個資料夾引入) File ->  Project Structure(快捷鍵 Ctrl + Alt + Shift + s),點選Project Structure介面左側的“Modules”顯示下圖介面。

選擇 Dependencies 加 右邊 +
選擇 1 JARs or directories... 編寫程式碼如下:
package z.test;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 java.io.IOException;

/**
 * Created by z on .
 */
public class WordCount {
    public static void main(String[] args) throws  Exception{
        if(args.length == 0){
            System.out.println("args is null exit....");
            System.exit(0);
        }

        Configuration configuration = new Configuration();

        Job job = new Job(configuration);
        job.setJarByClass(WordCount.class);
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        job.setMapperClass(TestMap.class);
        job.setReducerClass(TestReduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        job.waitForCompletion(true);
        System.out.println("test....");


    }

    public static class TestMap extends Mapper{
        @Override
        protected void map(LongWritable key, Text value, Context  context) throws IOException, InterruptedException {
            
            String[] words = value.toString().split(" ");
            for (String word : words){
                context.write(new Text(word),new IntWritable(1));
            }

        }
    }

    public static class TestReduce extends Reducer{
        @Override
        protected void reduce(Text key, Iterable values, org.apache.hadoop.mapreduce.Reducer.Context context) throws IOException, InterruptedException {
            
            int count = 0;
            for(IntWritable i : values){
                count += i.get();
            }
            context.write(key,new IntWritable(count));
        }
    }

}
打包 File->project stucture Artifacts->"+",選jar,選擇from modules with dependencies,然後會有配置窗口出現,配置完成後,勾選Build on make >ok儲存 啟動hadop sbin/start-all.sh 執行 jar 包 hadoop jar WordCount.jar z.test.WordCount /in/words /out/wordcount
-------end----------
歡迎關注精彩黑科技