bulkload的方式导入数据是hbase的一项相当好的数据导入工具,特别适合做为新系统的历史数据导入工具!hbase本身也封装了相关的类importtsv,官网有简单的介绍http://hbase.apache.org/bulk-loads.html。
这里我要说明的是如何去快速定制一些适合自己应用的bulkload。
我们一般需要运行的数据有几种格式,txt的用的最普遍,采用lzo压缩过的txt更专业一些,这里举例lzo格式的源文件。以下代码生成hfile
package com.sina.hbase.mr;
import java.io.IOException;
import java.util.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import com.hadoop.mapreduce.LzoTextInputFormat;
import com.sina.hbase.connection.ConnectionPool;
import com.sina.hbase.utils.DataOptUtil;
import com.sina.hbase.utils.Util;
public class BulkLoad {
public static class ***Mapper extends
Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// 检查并初始化数据对象
*** p = Util.checkAndBuild(value.toString());
if (p != null) {
byte[] row = Bytes.toBytes(p.getUid());
ImmutableBytesWritable k = new ImmutableBytesWritable(row);
KeyValue kv = new KeyValue(row, "c".getBytes(), "c".getBytes(),
p.toByteArray());
context.write(k, kv);
}
}
}
/**
* 通过表名决定使用哪种Mapper,如果表名不存在则返回null
*
* @param tableName
* @return
*/
@SuppressWarnings("rawtypes")
public static Class<? extends Mapper> decideMapper(String tableName) {
if (tableName.equals("***"))
return ***Mapper.class;
return null;
}
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.err
.println("Usage: BulkLoad <inputPath> <hfilePath> <tablename>");
System.exit(2);
}
Configuration conf = HBaseConfiguration.create();
ConnectionPool.init(conf, 1000);
HTable table = null;
table = ConnectionPool.getTable(args[2]);
Job job = new Job(conf, "BulkLoad-" + args[2] + "-"
+ DataOptUtil.Date2LongString(new Date()));
// 根据表的不同选择mapper
job.setMapperClass(decideMapper(args[2]));
job.setJarByClass(BulkLoad.class);
job.setInputFormatClass(LzoTextInputFormat.class);
HFileOutputFormat.configureIncrementalLoad(job, table);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job,
Util.RemoveHDFSPath(new Path(args[1]), conf));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
以上的源代码很简单,但是够用。需要做一些说明的是:
1、一定记得在建表时做region的预切分,HFileOutputFormat.configureIncrementalLoad方法会根据region的数量来觉得reduce的数量以及每个reduce覆盖的rowkey范围。否则当个reduce过大,任务处理不均衡。
2、单个rowkey下的子列不要过多,否则在reduce阶段排序的时候会造成oom,有一种办法是通过二次排序来避免reduce阶段的排序,看应用而定。
3、该代码执行完后需要将hdfs中生成好的hfile写入到hbase表中。采用hadoop jar hbase-version.jarcompletebulkload /hfilepath tablename 命令实现。
4、导入hadoop-lzo的jar包,才有LzoTextInputFormat这个类。
5、MR报google的一些包找不到时是hadoop classpath环境中没有加入hbase相关jar包,可以用-libjars xxx.jar,xxx.jar,xxx.jar来解决
分享到:
相关推荐
博客配套文件,详细演示了如何通过MR程序的方式bulkload数据到hbase,代码可直接用于生产环境。
通过使用 WAL 和缓冲的 Put 从 Hdfs 文件中摄取 HBase 记录 通过 WAL(使用 Put)将具有 PARQUET 格式的 hdfs 文件加载到 Hbase 的包。 该包基于仅使用 Mapper 加载表。 很快我将添加如何使用 reducer 以及使用 MR ...
工作中遇到的问题 即 bulkload 实现hive 到Hbase批量写入数据 工作中遇到的问题 即 bulkload 实现hive 到Hbase批量写入数据
大数据时代,海量数据的存储和分析是一个巨大的挑战,给hadoop或者hbase集群添加数据压缩的能力,是必不可少的,通过压缩不但能节约磁盘空间,而且也能节省集群间网络带宽的损耗,从而间接提高了集群任务的整体执行...
hbase 常用参数含义,默认值,调优建议(必须参数,split,compaction,blockcache,memstore flush,hlog,zookeeper,其他,等相参数名称、含义、默认值、调优建议)
hbase批量加载 从RCFile进行HBase批量加载的临时代码 这将使用LoadIncrementalFiles从HFileOutputFormat2中Mapreduce写入的数据中加载HBase表。
NULL 博文链接:https://787141854-qq-com.iteye.com/blog/2067818
基于hadoop的,java实现hive快速导数据到Hbase解决方案。
手把手视频详细讲解项目开发全过程,需要的小伙伴自行百度网盘下载,链接见附件,永久有效。 ...2. HBase批量装载——Bulk load 3. HBase的协处理器(Coprocessor) 4. HBase事务 5. HBase数据结构
java API实现hbase的bulkload,包含有hbase的工具类
7.1 bulkload介绍 19 7.2 具体使用方法 19 7.2.1 往hdfs中put文件: 19 7.2.2 用ImportTsv方法生成Hfile 20 7.2.3 利用Hfile进行加载 21 7.2.4 put方式直接加载 22 8 HBase-client 24 8.1 log4j 24 8.2 ...
│ Day1610_Hbase与MapReduce集成-Bulk Load.mp4 │ Day1611_Hbase与Sqoop集成使用.mp4 │ Day1612_Hbase完全分布式集群的部署配置.mp4 ├─03_笔记 │ Day16[Hbase 企业应用及与MapReduce集成].txt ├─04_代码 ├...
bulkload方式从hive import hbase有现成的sql可以使用 如果输入是CSV文件或parquet文件,当然也可以先建立一个hive外部表,再从hive import到hbase 也可以通过编码的方式,通过bulkload生成hfile然后再合并到hbase ...
HBase批量加载 使用 MapReduce 作业从文本文件加载 HBase
133_hbase的bulkload命令实现hbase集群之间数据的传输2 D6 d; F6 S8 x+ I/ I0 B0 @ 134_hive同hbase集成,统计hbase数据表信息% Q/ R! Z1 J3 J) k+ H! {6 D# M 135_使用TableInputFormat进行MR编程! m& C6 B/ v6 N" ...
Bulk Load 484 14. ZooKeeper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 Installing and Running ZooKeeper 488 An Example ...