尽管我们在虚拟机client上能非常快通过shell命令,进行运行一些已经封装好实例程序,可是在应用中还是是自己敲代码,然后部署到server中去,以下,我通过程序进行浅谈一个程序的部署过程。
在启动Hadoop之后,然后把程序达成可运行的jar包,并把对应的第三方jar包 包括进去。运行hadoop jar XXX. +驱动名称。
package com.mapred;
import java.io.IOException;
import java.io.PrintStream;
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.Mapper.Context;
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 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(WordCount.class);
FileInputFormat.addInputPath(job, new Path("hdfs://ubuntu:9000/Input"));
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
FileOutputFormat.setOutputPath(job, new Path("hdfs://ubuntu:9000/output09"));
job.waitForCompletion(true);
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>
{
private IntWritable result;
public IntSumReducer()
{
this.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 class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>
{
private static final IntWritable one = new IntWritable(1);
private Text word;
public TokenizerMapper()
{
this.word = new Text();
}
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);
}
}
}
}
在运行的过程中要注意下面几个事项:
首先要注意的就是,文件在hdfs上的位置是否正确,记住仅仅须要指定目录名称就可以,里面有多少详细文件,Hadoop都一并给你处理,注意观察在运行过程中所出现的异常。
由于我在运行和调试过程中也出现非常多异常,我觉得这些异常是情况非常多的,希望有兴趣的同学和我一起交流,共同分析和研究它。
1:注意观察虚拟机终端中报的错误,依据错误进行对应改进,由于关联jar较多,所以当提示你少对应的某一个包时,你要注意引进过来。
2:这里我是部署到虚拟机中运行的,只是在网上看过非常多资料说,通过Eclipse也能够直接进行数据的处理,可是我没有调试成功,希望大家谁成功了,告知我一声。我感觉我是版本号和虚拟机可能没有绑定好。
3:用Java命令(Java -jar XXX.jar)也能够执行。并且在这样的情况下不须要安装和部署Hadoop环境。可是由于我的Java虚拟机在执行时,老是提示内存不足。没有成功,我还是在Hadoop环境和总成功的。大家能够尝试并交流着去做一下。这个东西,处理数据有点意思。