HBase - gwenn/hypomnemata GitHub Wiki
hbase-site.xml in classpath
Configuration hc = HBaseConfiguration.create();
UserGroupInformation.setConfiguration(hc);
UserGroupInformation.loginUserFromKeytab(principal, keytabPath);
# Threadsafe
conn = ConnectionFactory.createConnection(hc);
...
conn.close()class TableDesc {
// nom de la table HBase
final TableName tableName;
// famille de colonnes
final byte[] family;
// durabilité des lignes insérées
final Durability durability;
TableDesc(Properties props, String prefix) {
tableName = TableName.valueOf(props.getProperty(prefix + ".table.namespace"),
props.getProperty(prefix + ".table.name"));
family = props.getProperty(prefix + ".family").getBytes(US_ASCII);
durability = Durability.valueOf(props.getProperty(prefix + ".durability"));
}
}Connection conn;
TableName tableName;
# Not threadsafe
Table table = conn.getTable(tableName);
...
table.close(); final byte[] row;
Put put = new Put(row);
put.setDurability(tableDesc.durability);
put.addImmutable(tableDesc.family, ID_COLUMN, Bytes.toBytes(calcul.getId()));
...
try (Table table = hbase.getTable(tableDesc.tableName)) {
// Ne faire l'insertion que si la ligne n'existe pas déjà:
final boolean created = table.checkAndPut(put.getRow(), tableDesc.family, ID_COLUMN, null, put);
} final byte[] row;
try (Table table = hbase.getTable(tableDesc.tableName)) {
final Result r = table.get(new Get(row).addColumn(tableDesc.family, ID_COLUMN));
if (r.isEmpty()) {
return null;
}
final Cell cell = r.rawCells()[0];
final long id = Bytes.toLong(cell.getValueArray(), cell.getValueOffset());
...
} static ByteBuffer allocateRow() {
return ByteBuffer.allocate(...);
}
static byte[] array(ByteBuffer row) {
final byte[] bytes = row.array();
final byte[] copy = copy(bytes);
ArrayUtils.reverse(copy); // Reverse the Key to avoid hotspotting but prevents scanning.
return copy;
} final List<Delete> deletes = new ArrayList<>();
for (...) {
final ByteBuffer row = allocateRow();
...
setPosition(row, index, posId);
Delete delete = new Delete(array(row));
delete.addFamily(tableDesc.family);
deletes.add(delete);
}
try (Table table = hbase.getTable(tableDesc.tableName)) {
table.delete(deletes);
} final byte[] row;
final long count = table.incrementColumnValue(array(row), tableDesc.family, COUNT_COLUMN, 1/*, tableDesc.durability*/); private static List<Result> extract(Result r, Position pos) {
if (r.isEmpty()) {
return null;
}
List<Result> results = new ArrayList<>();
final List<Cell> cells = r.listCells();
for (Cell cell : cells) {
if (CellUtil.matchingQualifier(cell, QUANTITY_COLUMN)) { // toutes les colonnes sauf la quantité
continue;
}
final String codeCombi = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
final Result result = deserialize(codeCombi, pos,
cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(),
RESULT_COMBINAISON_FACTORY);
results.add(result);
}
return results;
} final List<? extends List<? extends Position>> batches = Lists.partition(ps, batchSize);
for (List<? extends Position> batch : batches) {
final List<Get> gets = new ArrayList<>(batch.size());
for (Position pos : batch) {
setPosition(row, index, pos);
//gets.add(new Get(array(row)).addColumn(tableDesc.family, Bytes.toBytes(scenario.getCode())));
gets.add(new Get(array(row)).addFamily(tableDesc.family));
}
final Result[] results = table.get(gets);
for (int i = 0; i < results.length; i++) {
extract(results[i], batch.get(i));
}
}