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 ...
随机推荐
- DbScopeFactory
using (var db = DbScopeFactory.Create()) { //这里修改数据 db.SaveChanges(); }
- 使用paramiko中 Server not found in known_hosts的错误解决
报错的格式如下: 主要是缺少set_missing_host_key_policy配置信息. set_missing_host_key_policy方法,是制定连接远程主机没有本地密钥或HostKey ...
- js instanceof 实现原理
function instanceof(left, right) { // 获得类型的原型 let prototype = right.prototype // 获得对象的原型 left = left ...
- 【JDBC】Mysql海量数据插入——PreparedStatement加快数据插入
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5861959.html 使用JDBC连接数据库时,如果插入的数据量大,一条一条地插入数据会变得非常缓慢.此时,我 ...
- wordpress 开源博客系统部署
1.开发工具 server apache 下载地址:http://www.apache.org http://httpd.apache.org/download.cgi 数据库 mys ...
- Selenium 的基础框架类
个人写的一个selenium的base类,应该所有使用selenium的同事都会使用到: package com.hx.baserunner; import static java.io.File.s ...
- 【DB2】数据迁移
数据迁移概述 在日常生活中常有数据的导入导出,为此db2提出了很多工具可以选择,export.import.load.db2look.db2move.db2dart,如下图所示: a.最上面虚线框部分 ...
- SpringCloud学习资料
CSDN博客:http://blog.csdn.net/k21325/article/details/52789829 http://blog.csdn.net/qq_26562641/article ...
- 百度地图 ijintui以及七牛、百度编辑器、kindeditor
密码是明文存储的 sig错误是因为params没拼接上md5后的秘钥,测试时候可以在 Api\Controller\CommonController\_initialize 方法里注释掉效验的代码 代 ...
- POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6448 Accepted: 2654 ...