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 ...
随机推荐
- C++ 第八课 标准c字符和字符串
atof() 将字符串转换成浮点数 atoi() 将字符串转换成整数 atol() 将字符串转换成长整型数 isalnum() 当字母或数字字符时, 返回真值 isalpha() 当字母字符时, 返回 ...
- intellij idea 插件安装、卸载
windows 下 intellij idea 插件安装.卸载 安装(在线安装): 根据图一.图二所示(蓝色标记) 卸载: 根据图一所示(橙色标记) 启用.关闭插件: 根据图一所示(绿色标记) 安 ...
- hadoop Mahout中相似度计算方法介绍(转)
来自:http://blog.csdn.net/samxx8/article/details/7691868 相似距离(距离越小值越大) 优点 缺点 取值范围 PearsonCorrelation 类 ...
- RN组件备忘录
1:ActivityIndicator:圆形的loading提示符号. 2:Button:按钮 3:FlatList:高性能列表组件,支持下拉刷新. 4:Image:图片组件,能显示 网络图片.静态资 ...
- java常见数据结构整理
java中容器类数据结构主要在java.util包中. java.util包中三个重要的接口及特点:List(列表).Set(保证集合中元素唯一).Map(维护多个key-value键值对,保证key ...
- 非常漂亮滴皮肤skin++ 终极破解之法
破解includeparametershook汇编windows *[标题]:Skin++通用界面换肤系统V2.0.1破解探讨 *[作者]:gz1X <gz1x(at)tom(dot)com&g ...
- SQL SERVER 2008 “阻止保存要求重新创建表的更改”
在SQL SERVER2008中,新建数据表以后,若再对该表进行更改,则会出现警告信息“不允许保存更改 阻止保存要求重新创建表的更改”,等等,需要进行一下设置: 工具--->选项--->D ...
- Web 进化
来源于:http://xitu.github.io/2016/05/11/history-of-web/ 传统后台架构 上古时代 上古时代 123456789101112131415161718192 ...
- shell 脚本启动spring boot的jar 包
#!/bin/bash # kill java进程 java_sso_prod_pid=`ps aux|grep sso.jar|grep -v "grep"|awk '{prin ...
- ios 中sqlite的用法
#import <sqlite3.h> @interface ViewController () { sqlite3 *_sqldb; } @end @implementation Vie ...