废话我就不说了,直接上代码,很简单,一看就懂
/**----------------------------------------------------------------
// 文件名:DataToHbase.java
//
// 创建时间:2012-11-7
//----------------------------------------------------------------*/
package com.xiaochen.mr.test;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
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.lib.input.FileInputFormat;
import org.jruby.util.Random;
/**
* 类描述
*
* @version [PZoom-V2.0]
* @author liuxiaochen
*
* 修改标识:
* 修改描述:
*
*/
public class DataToHbase
{
public static class MapHbase extends Mapper<Object, Text, Text, IntWritable>
{
private final static IntWritable one = new IntWritable(1) ;
private Text text = new Text() ;
public void map(Object key, Text value, Context context) throws IOException, InterruptedException
{
String line = value.toString() ;
StringTokenizer stiz = new StringTokenizer(line) ;
while(stiz.hasMoreTokens())
{
text.set(stiz.nextToken()) ;
context.write(text, one) ;
}
}
}
public static class ReduceHbase extends TableReducer<Text, IntWritable, Text>
{
public void reduce(Text text, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException
{
Put put = null ;
String rowkey = "" ;
int sum = 0 ;
for(IntWritable i : values)
{
sum += i.get() ;
}
rowkey = String.valueOf(new Random(100)) ;
put = new Put(rowkey.getBytes()) ;
put.add("content".getBytes(), "text".getBytes(), (sum+"").getBytes()) ;
put.add("content".getBytes(), "sum".getBytes(), text.toString().getBytes()) ;
context.write(new Text("xchen1"), put) ;
}
}
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException
{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.30,192.168.1.31,192.168.1.32");
conf.set("hbase.zookeeper.property.clientPort", "2222");
Job job;
job = new Job(conf, "Runner"); //
Path in = new Path("D:/testFile/mr2/a.txt") ;
job.setJarByClass(DataToHbase.class) ;
job.setMapOutputKeyClass(Text.class) ;
job.setMapOutputValueClass(IntWritable.class) ;
job.setMapperClass(MapHbase.class) ;
job.setReducerClass(ReduceHbase.class) ;
FileInputFormat.setInputPaths(job, in);
// org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.setOutputPath(job, new Path("D:/testFile/mr1/" + new Date().getTime())) ;
TableMapReduceUtil.initTableReducerJob("xchen1", ReduceHbase.class, job) ;
// com.hbase.TableMapReduceUtil.initMulitTableReducerJob("xchen1",ReduceHbase.class, job, null, null, null,
// null, true);
job.waitForCompletion(true);
}
}
废话我就不说了,直接上代码
分享到:
相关推荐
基于Hadoop的mapreduce 在hbase上的使用,基于Hadoop的mapreduce 在hbase上的使用
mapreduce方式入库hbase hive hdfs,速度很快,里面详细讲述了代码的编写过程,值得下载
HBase MapReduce完整实例.rar
对Hadoop中的HDFS、MapReduce、Hbase系列知识的介绍。如果想初略了解Hadoop 可下载观看
google mapreduce bigtable hbase 三篇最著名的中文论文
#资源达人分享计划#
通过对Hadoop分布式计算平台最核心的分布式文件系统HDFS、MapReduce处理过程,以及数据仓库工具Hive和分布式数据库Hbase的介绍,基本涵盖了Hadoop分布式平台的所有技术核心。通过这一阶段的调研总结,从内部机理的...
Eclipse工程 HBase MapReduce完整实例 可远程执行 包含HBase增删改查 执行Test可看到效果
hadoop基础,hdfs,hive,mapreduce,hbase
NULL 博文链接:https://jsh0401.iteye.com/blog/2096103
基于Python+SpringBoot+Vue+HDFS+MapReduce+HBase+Hive+Kafka+Sp
#资源达人分享计划#
利用hadoop的mapreduce和Hbase,基于lucene做的简单的搜索引擎 ## 基本介绍 - InjectDriver 将本地的url注入到hbase数据库中等待下一步执行 - FetchDriver 负责抓取url对应的网页内容 - ParserUrlDriver 解析所抓取...
Map:主要功能是读取经过切割split文件形成一个map任务,分析map任务,得到 中间结构并且将同一类型的中间文件存放在同一个区域内等待特定的reduce程 序读取。 3. Reduce:不同的Reduce读取各个Map得到的特定的中间...
Hadoop-0.20.0-HDFS+MapReduce+Hive+HBase十分钟快速入门
hbase导入hbase导入
利用hadoop的mapreduce和Hbase,基于lucene做的简单的搜索引擎 基本介绍 InjectDriver 将本地的url注入到hbase数据库中等待下一步执行 FetchDriver 负责抓取url对应的网页内容 ParserUrlDriver 解析所抓取网页内容...
HDFS+MapReduce+Hive+HBase十分钟快速入门.pdf
使用MapReduce读取hbase数据库中千万级别的数据,处理数据并统计,将统计后的结果存入mysql
该案例中主要使用MapReduce作为处理组件进行数据处理,实现的案例有如通过javaapi实现hbase数据写入hdfs、hbase表数据复制到另一个表中等操作 对应(《HBase分布式存储系统应用》胡鑫喆 张志刚著)教材中案例