首页 / HADOOP / 炼数成金hadoop视频干货05
炼数成金hadoop视频干货05
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了炼数成金hadoop视频干货05,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2899字,纯文字阅读大概需要5分钟。
内容图文
视频地址:http://pan.baidu.com/s/1dDEgKwD
这一节是讲师助教带着动手操作写简单的开发环境的部署和两个实例
开发环境的部署:http://www.cnblogs.com/admln/p/test-deployDevelopment.html
第一个实例就是wordcount
第二个实例
1 package testHadoop; 2 3 import java.io.IOException; 4 5 import org.apache.hadoop.conf.Configuration; 6 import org.apache.hadoop.conf.Configured; 7 import org.apache.hadoop.fs.Path; 8 import org.apache.hadoop.io.LongWritable; 9 import org.apache.hadoop.io.Text; 10 import org.apache.hadoop.mapred.TextOutputFormat; 11 import org.apache.hadoop.mapreduce.Job; 12 import org.apache.hadoop.mapreduce.Mapper; 13 import org.apache.hadoop.mapreduce.Reducer; 14 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 15 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 16 import org.apache.hadoop.util.Tool; 17 import org.apache.hadoop.util.ToolRunner; 18 19 @SuppressWarnings("deprecation") 20publicclass ReverseIndex extends Configured implements Tool{ 21enum Counter{ 22 LINESKIP; 23 } 2425publicstaticclass Map extends Mapper<LongWritable,Text,Text,Text> { 26publicvoid map(LongWritable key,Text value,Context context) throws IOException, InterruptedException { 27 String line = value.toString(); 28try { 29 String[] lineSplit = line.split(" "); 30 String anum = lineSplit[0]; 31 String bnum = lineSplit[1]; 3233 context.write(new Text(bnum), new Text(anum)); 34 }catch(java.lang.ArrayIndexOutOfBoundsException e) { 35 context.getCounter(Counter.LINESKIP).increment(1); 36return; 37 } 3839 } 40 } 41publicstaticclass Reduce extends Reducer<Text,Text,Text,Text> { 42publicvoid reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException { 43 String valueString; 44 String out = ""; 4546for(Text value:values) { 47 valueString = value.toString(); 48 out += valueString+"|"; 49 } 50 context.write(key, new Text(out)); 51 } 52 } 53publicint run(String[] args) throws Exception { 54 Configuration conf = getConf(); 5556 Job job = new Job(conf,"ReverseIndex"); 57 job.setJarByClass(ReverseIndex.class); 5859 FileInputFormat.addInputPath(job, new Path(args[0])); 60 FileOutputFormat.setOutputPath(job, new Path(args[1])); 6162 job.setMapperClass(Map.class); 63 job.setReducerClass(Reduce.class); 64//job.setOutputFormatClass(TextOutputFormat.class);65 job.setOutputKeyClass(Text.class); 66 job.setOutputValueClass(Text.class); 6768 job.waitForCompletion(true); 6970return job.isSuccessful()?0:1; 7172 } 73publicstaticvoid main(String[] args) throws Exception { 74int res = ToolRunner.run(new Configuration(), new ReverseIndex(),args); 75 System.exit(res); 76 } 77 }
eclipse中运行没问题后打包用集群运行的时候遇到一个小问题
版本不合。原来我编译的时候在windows下面用的JDK7,Linux中hadoop JDK是1.6。
把源码在Linux上1.6编译一下就可以了。
实践过程中还学到一个小知识,在运行程序命令中输入输出路径如果用例如 input output这样的,就是默认在HDFS上/user/用户名/下面的input和output
如果用例如/input /output这样的路径就是在HDFS根目录下的input 和output
原文:http://www.cnblogs.com/admln/p/dataguru05.html
内容总结
以上是互联网集市为您收集整理的炼数成金hadoop视频干货05全部内容,希望文章能够帮你解决炼数成金hadoop视频干货05所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。