我正在通过 HBase 执行 MR。
reducer 中的业务逻辑大量访问两个表,比如 T1(40k 行)和 T2(90k 行)。目前,我正在执行以下步骤:
1.在reducer类的构造函数中,做这样的事情:
HBaseCRUD hbaseCRUD = new HBaseCRUD();
HTableInterface t1= hbaseCRUD.getTable("T1",
"CF1", null, "C1", "C2");
HTableInterface t2= hbaseCRUD.getTable("T2",
"CF1", null, "C1", "C2");
在减少(...)
String lowercase = ....;
/* Start : HBase code */
/*
* TRY using get(...) on the table rather than a
* Scan!
*/
Scan scan = new Scan();
scan.setStartRow(lowercase.getBytes());
scan.setStopRow(lowercase.getBytes());
/*scan will return a single row*/
ResultScanner resultScanner = t1.getScanner(scan);
for (Result result : resultScanner) {
/*business logic*/
}
虽然不确定上面的代码首先是否合理,但我有一个问题 - get(...) 会比扫描提供任何性能优势吗?
Get get = new Get(lowercase.getBytes());
Result getResult = t1.get(get);
由于 T1 和 T2 将是只读的(大部分),我认为如果保留在内存中,性能将会提高。根据 HBase 文档,我将不得不重新创建表 T1 和 T2。请验证我理解的正确性:
public void createTables(String tableName, boolean readOnly,
boolean blockCacheEnabled, boolean inMemory,
String... columnFamilyNames) throws IOException {
// TODO Auto-generated method stub
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
/* not sure !!! */
tableDesc.setReadOnly(readOnly);
HColumnDescriptor columnFamily = null;
if (!(columnFamilyNames == null || columnFamilyNames.length == 0)) {
for (String columnFamilyName : columnFamilyNames) {
columnFamily = new HColumnDescriptor(columnFamilyName);
/*
* Start : Do these steps ensure that the column
* family(actually, the column data) is in-memory???
*/
columnFamily.setBlockCacheEnabled(blockCacheEnabled);
columnFamily.setInMemory(inMemory);
/*
* End : Do these steps ensure that the column family(actually,
* the column data) is in-memory???
*/
tableDesc.addFamily(columnFamily);
}
}
hbaseAdmin.createTable(tableDesc);
hbaseAdmin.close();
}
一旦完成:
- 如何验证列是否在内存中(当然,describe 语句和浏览器反映了它)并从那里而不是磁盘访问?
- 从内存或从磁盘读取对客户端透明吗?简单来说,我需要在我的 reducer 类中更改 HTable 访问代码吗?如果是,有哪些变化?