Hbase里的数据量一般都小不了,
因此MapReduce跟Hbase就成了天然的好搭档。
1.ZK授权表
首先一点来说, Hbase是强依赖于ZK的。博主所在的team,就经常出现ZK连接数太多被打爆然后Hbase挂了的情况。 一般在访问Hbase表之前,需要通过访问ZK得到授权:
2.thrift对象转化
本例中操作的对象为XX表,Column Family为”P”, Qualifer 为”P”与”C”,里面对应的value都是thrift对象。其中”P”对应的thrift对象为:
“C”对应的thrift对象为:
这个时候我们就需要经常将Bytes转化为thrift对象,通用的方法为:
3.Map阶段读取Hbase里的数据
在Map阶段对Hbase表扫描,得出数据
1.ZK授权表
首先一点来说, Hbase是强依赖于ZK的。博主所在的team,就经常出现ZK连接数太多被打爆然后Hbase挂了的情况。 一般在访问Hbase表之前,需要通过访问ZK得到授权:
/**
* 为hbase表授权。
*
* @param tableConfigKey 任意一个字符串。
* @param tableName 需要授权的表名, scan涉及到的表不需要额外授权。
* @param job 相关job。
* @throws IOException
*/
public static void initAuthentication(String tableConfigKey, String tableName, Job job) throws IOException {
Configuration peerConf = job.getConfiguration();
peerConf.set(tableConfigKey, tableName);
ZKUtil.applyClusterKeyToConf(peerConf, tableName);
if (User.isHBaseSecurityEnabled(peerConf)) {
LOGGER.info("Obtaining remote user authentication token with table:{}", tableName);
try {
User.getCurrent().obtainAuthTokenForJob(peerConf, job);
} catch (InterruptedException ex) {
LOGGER.info("Interrupted obtaining remote user authentication token");
LOGGER.error("Obtained remote user authentication token with table:{}, error:\n", tableName, ex);
Thread.interrupted();
}
LOGGER.info("Obtained remote user authentication token with table:{}", tableName);
}
}
2.thrift对象转化
本例中操作的对象为XX表,Column Family为”P”, Qualifer 为”P”与”C”,里面对应的value都是thrift对象。其中”P”对应的thrift对象为:
struct UserProfile {
1: optional byte sex;
2: optional i32 age;
3: optional string phoneBrand;
4: optional string locationProvince;
}
“C”对应的thrift对象为:
struct UserClickInfo {
1: required i32 totolAck;
2: required i32 totalClick;
3: optional map<i64, map<string, i32>> ackClick;
}
这个时候我们就需要经常将Bytes转化为thrift对象,通用的方法为:
/**
* convert byte array to thrift object.
*
* @param <T> type of thrift object.
* @param thriftObj an thrift object.
* @return byte array if convert succeeded, <code>null</code> if convert failed.
* @throws TException
*/
public static final <T extends TBase<T, ?>> T convertBytesToThriftObject(byte[] raw, T thriftObj) throws TException {
if (ArrayUtils.isEmpty(raw)) {
return null;
}
Validate.notNull(thriftObj, "thriftObj");
TDeserializer serializer = new TDeserializer(new TBinaryProtocol.Factory());
serializer.deserialize(thriftObj, raw);
return thriftObj;
}
3.Map阶段读取Hbase里的数据
在Map阶段对Hbase表扫描,得出数据