需求背景:
公司的spark集群和hbase集群是两个互不相通的集群,所以我们的方式就是先用spark 生成hfile 然后再通过distcp 的方式把数据cp到HBase所在的集群,然后再通过bulk的方式把数据导入到HBase中
关键性的疑问点:
Q1:我生成的hfile如何和表里的各种设置想对应呢?
比如说:我的table 设置了 bf 设置了snappy 压缩算法,等等,这些在我们生成hfile的时候是应该都要完成的操作。这些东西我们是如何指定的呢?
关键角色登场:
HFileOutputFormat2
我们来看下源码:
这里就是关键了,配置信息都在这里做好了。
我么来继续跟踪一下:
static void configureCompression(Configuration conf, HTableDescriptor tableDescriptor)
throws UnsupportedEncodingException {
StringBuilder compressionConfigValue = new StringBuilder();
if(tableDescriptor == null){
// could happen with mock table instance
return;
}
// 得先拿到对应的 列族 ,可能有多个列族,所以是 families 然后会去遍历所有的列族
Collection<HColumnDescriptor> families = tableDescriptor.getFamilies();
int i = 0;
for (HColumnDescriptor familyDescriptor : families) {
if (i++ > 0) {
compressionConfigValue.append('&');
}
compressionConfigValue.append(URLEncoder.encode(
familyDescriptor.getNameAsString(), "UTF-8"));
compressionConfigValue.append('=');
compressionConfigValue.append(URLEncoder.encode(
familyDescriptor.getCompression().getName(), "UTF-8"));
}
// Get rid of the last ampersand
// 这里设置好了对应的压缩的算法
conf.set(COMPRESSION_FAMILIES_CONF_KEY, compressionConfigValue.toString());
}
并且它维护了若干个map:
上一个步骤是赋值的操作,然后在这里维护了map
我们必须说的一个角色就是:
HColumnDescriptor 因为在hbase里,很多的设置都是直接和列族挂钩的,并且上面也是通过列族来获取到的各种信息。
其实这里我们就可以看到,很多的配置信息都是在这里给配置好的。
我们在使用的过程中经常会用到
HFileOutputFormat2.configureIncrementalLoad()
public static void configureIncrementalLoad(Job job, Table table, RegionLocator regionLocator)
throws IOException {
// 实际上调用的是 这个方法 通过Table 来获取到对用的 表的描述信息
configureIncrementalLoad(job, table.getTableDescriptor(), regionLocator);
}
先写道这里~待续