首页 技术 正文
技术 2022年11月15日
0 收藏 683 点赞 4,517 浏览 2373 个字

只使用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表
} }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291