关于自定义数据类型,http://book.douban.com/annotation/17067489/?一文中给出了一个比较清晰的说明和解释。
以wordCount为例子
定义自己的数据类型Http类
import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class Http implements WritableComparable<Http> { public Http(){ } private String value; public Http(String value) { setValue(value); } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public void readFields(DataInput in) throws IOException { value = in.readUTF(); } public void write(DataOutput out) throws IOException { out.writeUTF(value); } public int compareTo(Http http) { return (value.compareTo(http.value)); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; } @Override public boolean equals(Object obj) { if (!(obj instanceof Http)) return false; Http other = (Http)obj; return this.value.equals(other.value); } @Override public String toString() { return value; } }
?编写wordcount程序
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.LongWritable; 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 WordCountEntry { public static class TokenizerMapper extends Mapper<LongWritable, Http, Http, IntWritable> { private final static IntWritable one = new IntWritable(1); private Http word = new Http(); public void map(LongWritable key, Http value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.setValue(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Http, IntWritable, Http, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Http key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { 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); } Path input = new Path(args[0]); Path output = new Path(args[1]); Job job = new Job(conf, "word count"); job.setJarByClass(WordCountEntry.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Http.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, input); FileOutputFormat.setOutputPath(job, output); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
?编写mrUnit测试用例进行mapreduce程序测试
import java.util.ArrayList; import java.util.List; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.mrunit.mapreduce.MapDriver; import org.apache.hadoop.mrunit.mapreduce.ReduceDriver; import org.junit.Before; import org.junit.Test; import com.geo.dmp.WordCountEntry.IntSumReducer; import com.geo.dmp.WordCountEntry.TokenizerMapper; public class WordCountEntryTest { private MapDriver<LongWritable, Http, Http, IntWritable> mapDriver; private ReduceDriver<Http, IntWritable, Http, IntWritable> reduceDriver; @Before public void setUpBeforeClass() throws Exception { TokenizerMapper tm = new TokenizerMapper(); mapDriver = MapDriver.newMapDriver(tm); IntSumReducer isr = new IntSumReducer(); reduceDriver = ReduceDriver.newReduceDriver(isr); } @Test public void TokenizerMapperTest() { mapDriver.withInput(new LongWritable(), new Http("01a55\tablsd")); mapDriver.withOutput(new Http("01a55"), new IntWritable(1)); mapDriver.withOutput(new Http("ablsd"), new IntWritable(1)); mapDriver.runTest(); } @Test public void IntSumReducerTest() { List<IntWritable> values = new ArrayList<IntWritable>(); values.add(new IntWritable(1)); values.add(new IntWritable(1)); reduceDriver.withInput(new Http("01a55"), values); reduceDriver.withOutput(new Http("01a55"), new IntWritable(2)); reduceDriver.runTest(); } }
?
Ubuntu 12.04单机版环境中搭建hadoop详细教程,在Ubuntu下创建hadoop用户组和用,创建hadoop用户。
在mac os上安装hadoop的文章不多,本文安装环境的操作系统是 MAC OS X 10.7 Lion,共分7步,注意第三个配置在OS X上最好进行配置,否则会报错“Unable to load realm info from SCDynamicStore”。
Nutch采用一个MR对爬取下来的文档进行清洗和封装成一个action列表。Nutch会将封装好的数据采用基于http的POST的方法发送一个请求数据包给solr的服务器,solr.commit();这个方法在前面一篇文章中解释有些偏差,solr的整个事务都是在solr服务器端的,这跟以前的的事务有所
、默认项目名称为MapReduceTools,然后在项目MapReduceTools中新建lib目录,先将hadoop下的hadoop-core-1.0.4.jar重命名为hadoop.core.jar,并把hadoop.core.jar、及其lib目录下的commons-cli-1.2.jar、commons-lang-2.4.jar、commons-configuration-1.6.jar、jackson-m
SAP特别设立了一个“大数据”合作伙伴理事会。该理事会致力于进行合作创新,研发基于SAP实时数据平台和Hadoop的新产品解决方案,探索新应用和架构
【聚焦搜索,数智采购】2021第一届百度爱采购数智大会即将于5月28日在上海盛大开启!
本次大会上,紫晶存储董事、总经理钟国裕作为公司代表,与中国—东盟信息港签署合作协议
XEUS统一存储已成功承载宣武医院PACS系统近5年的历史数据迁移,为支持各业务科室蓬勃扩张的数据增量和访问、调用乃至分析需求奠定了坚实基础。
大兆科技全方面展示大兆科技在医疗信息化建设中数据存储系统方面取得的成就。
双方相信,通过本次合作,能够使双方进一步提升技术实力、提升产品品质及服务质量,为客户创造更大价值。