hadoop程式設計實踐(二)
阿新 • • 發佈:2018-11-09
叢集上使用
jar包
- 首先將之前
FileExist
檔案進行打包,得到.jar
檔案: - 將其拷貝到叢集中,並使用
hadoop jar
命令執行:
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包
編寫程式
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:
- 進行編譯:
- 生成並檢視JAR包:
本地偽分散式執行
-
建立兩個檔案作為輸入,內容為:
-
I love Spark
I love HadoopHadoop 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
-
-
執行:
-
hadoop jar WordCount.jar input output
-
-
檢視結果:
-
hdfs dfs -cat output/*
-
叢集上執行
-
首先將JAR包和檔案放入叢集:
-
將其拷貝到
HDFS
中:-
hdfs dfs -mkdir -p /user/hadoop7/input hdfs dfs -put ./wordfile1.txt input hdfs dfs -put ./wordfile2.txt input
-
-
檢視檔案:
-
執行:
-
hadoop jar WordCount.jar input output
-
-
檢視叢集執行情況
- 在連線VPN時,在瀏覽器中輸入
10.11.6.91:50070
- 在連線VPN時,在瀏覽器中輸入