1. 程式人生 > >hadoop程式設計實踐(二)

hadoop程式設計實踐(二)

叢集上使用

jar包

  • 首先將之前FileExist檔案進行打包,得到.jar檔案:
    • 53932351802
  • 將其拷貝到叢集中,並使用hadoop jar命令執行:
    • 53932361530

WordCount

新增依賴

  • 首先我們需要新建一個WordCount專案,首先要新增Hadoop的包依賴
    • /usr/local/hadoop/share/hadoop/common
      • hadoop-common-xxx.jar
      • hadoop-nfs-xxx.jar
    • /usr/local/hadoop/share/hadoop/common/lib 下的所有Jar包
    • /usr/local/hadoop/share/hadoop/mapreduce該目錄下所有JAR包
    • /usr/local/hadoop/share/hadoop/mapreduce/lib目錄下所有JAR包
    • 53932706913

編寫程式

import java.io.IOException;
import java.util.StringTokenizer;

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 WordCount { public WordCount () { } public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public TokenizerMapper () { } public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { this.word.set(itr.nextToken()); context.write(this.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, Reducer<Text,IntWritable,Text,IntWritable>.Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } this.result.set(sum); context.write(key, this.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>[<in>...] <out>"); System.exit(2); } Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCount.TokenizerMapper.class); job.setCombinerClass(WordCount.IntSumReducer.class); job.setReducerClass(WordCount.IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); for (int i = 0; i < otherArgs.length-1; i++) { FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length-1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

打包成JAR包

  • 開啟Project Structure:
    • 53932738204
  • 進行編譯:
    • 53932743987
  • 生成並檢視JAR包:
    • 53932748893

本地偽分散式執行

  • 建立兩個檔案作為輸入,內容為:

    • I love Spark
      I love Hadoop

      Hadoop is good
      Spark is fast

  • 將本地檔案放入hdfs中:

    • hdfs dfs -mkdir -p /user/hadoop/input
      hdfs dfs -put ./wordfile1.txt input
      hdfs dfs -put ./wordfile2.txt input
      
  • hdfs中檢視:

    • hdfs dfs -ls input
      
    • 53932792299

  • 執行:

    • hadoop jar WordCount.jar input output
      
  • 檢視結果:

    • hdfs dfs -cat output/*
      
    • 53932803571

叢集上執行

  • 首先將JAR包和檔案放入叢集:

    • 53932821775
  • 將其拷貝到HDFS中:

    • hdfs dfs -mkdir -p /user/hadoop7/input
      hdfs dfs -put ./wordfile1.txt input
      hdfs dfs -put ./wordfile2.txt input
      
  • 檢視檔案:

    • 53932836121
  • 執行:

    • hadoop jar WordCount.jar input output
      
    • 53932842481

  • 檢視叢集執行情況

    • 在連線VPN時,在瀏覽器中輸入10.11.6.91:50070
    • 53932864271