mapreduce 只使用Mapper往多个hbase表中写数据
只使用Mapper不使用reduce会大大减少mapreduce程序的运行时间。
有时候程序会往多张hbase表写数据。
所以有如题的需求。
下面给出的代码,不是可以运行的代码,只是展示driver中需要进行的必要项设置,mapper类需要实现的接口,map函数需要的参数以及函数内部的处理方式。
实现过程比较曲折,只贴代码:
class Qos2HbaseDriver extends Configured implements Tool
{
private static Logger logger = LoggerFactory
.getLogger(Qos2HbaseDriver.class);
private static final int DEFAULT_NUM_REDUCE = 0; /**
* args[0]输入hdfs文件路径,args[1]输出表
*/ @Override
public int run(String[] args) throws Exception
{
Configuration conf = HBaseConfiguration.create();
conf.set("output", args[1]);//输出表1
conf.set("output2", args[2]);//输出表2 Job job = Job.getInstance(conf);
job.setJobName("iplane_Qos2Hbase");
job.setMapperClass(Qos2HbaseMapper.class);
FileInputFormat.setInputPaths(job, args[0]);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
job.setOutputFormatClass(MultiTableOutputFormat.class); TableMapReduceUtil.addDependencyJars(job);
TableMapReduceUtil.addDependencyJars(job.getConfiguration());
job.setJarByClass(Qos2Hbase.class); // 设置reduce个数,可调节
int numberReduceTasks = 0;
job.setNumReduceTasks(numberReduceTasks);
boolean b = job.waitForCompletion(true);
if (!b)
{
logger.error("工作错误!");
return -1;
}
return 0;
}
} /**
* @ClassName: Qos2HbaseMapper
* @Description: 将结果入Hbase库的mapper类
* @author xxx
* @date 2014-9-16 下午1:18:49
*
*/
class Qos2HbaseMapper extends
Mapper<LongWritable, Text, ImmutableBytesWritable, Put>
{
private static Logger logger = LoggerFactory
.getLogger(Qos2HbaseMapper.class); @Override
public void map(LongWritable key, Text line, Context context)
throws IOException, InterruptedException
{
String output = context.getConfiguration().get("output");
String output2 = context.getConfiguration().get("output2"); // 组装rowkey:ip_ip
StringBuffer rowkeySb = "aaaa"; Put put = null;
String family = "d";
String qualifier = "";
// 直接将结果存入hbase
long ts = System.currentTimeMillis();
put = new Put(Bytes.toBytes(rowkeySb.toString())); qualifier = "del";
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,
Bytes.toBytes(values[6]));// 组装一条数据
if (!put.isEmpty())
{
ImmutableBytesWritable ib = new ImmutableBytesWritable();
ib.set(Bytes.toBytes(output));
context.write(ib, put);// 将结果存入hbase表
} // 存历史表
rowkeySb.append(rowkeySeparator).append(myDate);
put = new Put(Bytes.toBytes(rowkeySb.toString()));
qualifier = "del";
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,
Bytes.toBytes(values[6]));// 组装一条数据
if (!put.isEmpty())
{
ImmutableBytesWritable ib = new ImmutableBytesWritable();
ib.set(Bytes.toBytes(output2));
context.write(ib, put);// 将结果存入hbase表
} }
}
mapreduce 只使用Mapper往多个hbase表中写数据的更多相关文章
- Mapreduce读取Hbase表,写数据到多个Hbase表中
Job端的变化: 通过设置conf,配置输出表,在reduce中获取输出表名字 Configuration conf = job.getConfiguration(); //输出表1 conf.set ...
- Mapreduce读取Hbase表,写数据到一个Hbase表中
public class LabelJob { public static void main(String[] args) throws Exception { Job job = Job.getI ...
- 浅谈hbase表中数据导出导入(也就是备份)
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=23916356&id=3321832 最近因为生产环境hbase ...
- hbase使用MapReduce操作4(实现将 HDFS 中的数据写入到 HBase 表中)
实现将 HDFS 中的数据写入到 HBase 表中 Runner类 package com.yjsj.hbase_mr2; import com.yjsj.hbase_mr2.ReadFruitFro ...
- 【HBase】通过Bulkload批量加载数据到Hbase表中
目录 需求 步骤 一.开发MapReduce 二.打成Jar包放到linux执行 三.有两种办法将HFile文件加载到HBase表中 开发代码 Hadoop命令运行 需求 将hdfs上面的这个路径 / ...
- PHP mysql 删除表中所有数据只保留一条
DELETE FROM `logs` WHERE wangzhi='www.juhutang.com' and id<>101072; 上面这段代码的意思为 删除表logs中 所有字段wa ...
- IDEA中Spark往Hbase中写数据
import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.io.ImmutableBytesWr ...
- sparkstreaming写入hbase表中总结
执行spark代码插入数据到hbase表中去的时候,遇到的错误 1. 缺少hadoop-mapreduce-client-core-2.5.1.jar包 错误:java.lang.ClassNotFo ...
- 通过删除hbase表中的region来达到删除表中数据
公司最近在搞一个hbase删除数据,由于在建表的时候是通过region来对每日的数据进行存储的,所以要求在删除的时候直接通过删除region的来删除数据(最好的方案是只删除region中的数据,不把r ...
随机推荐
- Spring Boot 之 RESTfull API简单项目的快速搭建(二)
1.打包 -- Maven build 2.问题 [WARNING] The requested profile "pom.xml" could not be activated ...
- 用Power BI解读幸福星球指数
"你幸福吗?"已经成了网络热词,简单的问句背后蕴含着一个普通中国人对于所处时代的政治.经济.自然环境等方方面面的感受和体会.引发当代中国人对幸福的深入思考.实际上,国际上对幸福的讨 ...
- Android短轮询解决方案——CountDownTimer+Handler
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7657194.html 一:应用场景 在诸如自动售卖机之类的扫码支付场景中,客户端在获得支付二维码或者发出支付请 ...
- 〖Linux〗Kubuntu KDE开机后总是提示“system program problem detected”的解决方法
自从从Ubuntu切换到了Kubuntu之后,就经常在开机的时候提示“system program problem detected”: 查看 /var/crash/ 发现都是一些无关痛痒的程序在关机 ...
- C# 多线程并发锁模式-总结
开篇: 互斥还是lock Monitor Mutex 模式! Muex Monitor lock AutoEventSet ManualEventSet 后续的 ReaderWriterLock ...
- 如何在 CentOS 7 中安装或升级最新的内核
虽然有些人使用 Linux 来表示整个操作系统,但要注意的是,严格地来说,Linux 只是个内核.另一方面,发行版是一个完整功能的系统,它建立在内核之上,具有各种各样的应用程序工具和库. 在正常操作期 ...
- ios中uiview 转场动画
//转场动画1--头尾 -(void)TransitionAnimation1{ [UIView beginAnimations:nil context:nil]; [UIView setAnimat ...
- Ubuntu登录Windows Server 2008r2 密码总是错误与NLA验证
日期:2013-05-22 经过一天的折腾,终于能够用Ubuntu登录Windows server 2008 R2 了. 寝室里面用小本子,装的ubuntu,实验室的服务器是win server0 ...
- CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别
这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...
- Linux中 /boot 目录介绍 【转载】
Linux中 /boot 目录介绍 转自:点击打开链接 一./boot/目录中的文件和目录 Linux系统在本地启动时,目录/boot/非常重要,其中的文件和目录有: (1)系统Kernel的配置文件 ...