文章目录
Hbase bulkloading
1、Hbase bulkloading优点
1)、 如果我们一次性入库hbase巨量数据,处理速度慢不说,还特别占用Region资源, 一个比较高效便捷的方法就是使用 “Bulk Loading”方法,即HBase提供的HFileOutputFormat类。
2)、它是利用hbase的数据信息按照特定格式存储在hdfs内这一原理,直接生成这种hdfs内存储的数据格式文件,然后上传至合适位置,即完成巨量数据快速入库的办法。配合mapreduce完成,高效便捷,而且不占用region资源,增添负载。
2、Hbase bulkloading缺点
1)、 仅适合初次数据导入,即表内数据为空,或者每次入库表内都无数据的情况。
2)、HBase集群与Hadoop集群为同一集群,即HBase所基于的HDFS为生成HFile的MR的集群
3、练习
1)、准备工作
在hdfs上的/创建data/dianxin目录,并将事先准备好的数据put到该目录下,在hbase上创建空表dianxin(列族为info),确保hdfs上该目录不存在(/data/hfile),准备工作结合代码即可明目。
2)、编写demo06BulkLoading.java
public class demo06BulkLoading {
public static class Map extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(",");
//设置rowkey
String rowkey = split[0] + split[1];
//构建hfile的数据格式
KeyValue kv1 = new KeyValue(rowkey.getBytes(), "info".getBytes(), "x".getBytes(), split[4].getBytes());
KeyValue kv2 = new KeyValue(rowkey.getBytes(), "info".getBytes(), "y".getBytes(), split[5].getBytes());
//提交
context.write(new ImmutableBytesWritable(rowkey.getBytes()), kv1);
context.write(new ImmutableBytesWritable(rowkey.getBytes()), kv2);
}
}
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJobName("BulkLoading");
job.setJarByClass(demo06BulkLoading.class);
job.setMapperClass(Map.class);
//排序的reduce, hbase 提供的
job.setReducerClass(KeyValueSortReducer.class);
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(KeyValue.class);
//设置reduce数量
job.setNumReduceTasks(4);
//自定义分区,实现全局有序
job.setPartitionerClass(SimpleTotalOrderPartitioner.class);
//指定表名
HTable table = new HTable(configuration, TableName.valueOf("dianxin"));
//指定输出的格式化类
HFileOutputFormat.configureIncrementalLoad(job, table);
//数据数据的路径
FileInputFormat.addInputPath(job, new Path("/data/dianxin"));
//最终生成hfile文件的路径
FileOutputFormat.setOutputPath(job, new Path("/data/hfile"));
job.waitForCompletion(true);
//将hfile文件导入到hbase表所在的目录下面
System.out.println("导入hfile到hbse表中");
LoadIncrementalHFiles loader = new LoadIncrementalHFiles(configuration);
loader.doBulkLoad(new Path("/data/hfile"), table);
}
}
3)、运行jar包
将编写的代码package上传到虚拟机上运行该jar包(下面演示同级目录下的操作)
hadoop jar hbase-1.0-jar-with-dependencies.jar com.shujia.demo06BulkLoading
补充:hbase-1.0-jar-with-dependencies.jar是jar包名,com.shujia.demo06BulkLoading是demo06BulkLoading代码的reference。