雲主機
- 開放雲主機所有端口(可以限制源IP,避免被挖礦)
- 編寫WordCount程序,並配置相應的參數
package www.immoc.hadoop.mapreduce.yarn;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
public class WordCountOnYarnApp {
public static void main(String[] args) throws Exception{
System.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration conf = new Configuration();
conf.set("yarn.resourcemanager.address", "bigdata:8032");
conf.set("dfs.client.use.datanode.hostname", "true");
conf.set("mapreduce.framework.name", "yarn");
conf.set("fs.defaultFS", "hdfs://bigdata:9000/");
conf.set("mapreduce.app-submission.cross-platform", "true");
Job job = Job.getInstance(conf, "wc");
String inputPath = "hdfs://bigdata:9000/ruozedata/wc/input/wc.data";
// String inputPath = args[0];
String outputPath = "hdfs://bigdata:9000/ruozedata/wc/outputyarn1";
// String outputPath = args[1];
job.setJar("target/hdfs-train-1.0.jar");
job.setJarByClass(WordCountOnYarnApp.class);
job.setMapperClass(myMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setReducerClass(myReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
boolean result = job.waitForCompletion(true);
System.out.println(result ? 0 : 1);
}
public static class myMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] datas = value.toString().split(",");
for (String data : datas) {
context.write(new Text(data), new IntWritable(1));
}
}
}
public static class myReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0;
for (IntWritable value : values) {
count += value.get();
}
context.write(key, new IntWritable(count));
}
}
}
如果實現不了,先配置本地遠行的環境(由於本人是先本地之後纔要提交到遠程集羣中,所以不知道本地運行的環境是有影響)
本地運行環境配置