下面列出了org.apache.hadoop.hbase.KeyValue#Type ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convert a delete KeyValue type to protocol buffer DeleteType.
*
* @param type
* @return protocol buffer DeleteType
* @throws IOException
*/
public static DeleteType toDeleteType(
KeyValue.Type type) throws IOException {
switch (type) {
case Delete:
return DeleteType.DELETE_ONE_VERSION;
case DeleteColumn:
return DeleteType.DELETE_MULTIPLE_VERSIONS;
case DeleteFamily:
return DeleteType.DELETE_FAMILY;
case DeleteFamilyVersion:
return DeleteType.DELETE_FAMILY_VERSION;
default:
throw new IOException("Unknown delete type: " + type);
}
}
/**
* Convert a protocol buffer DeleteType to delete KeyValue type.
*
* @param type The DeleteType
* @return The type.
* @throws IOException
*/
public static KeyValue.Type fromDeleteType(
DeleteType type) throws IOException {
switch (type) {
case DELETE_ONE_VERSION:
return KeyValue.Type.Delete;
case DELETE_MULTIPLE_VERSIONS:
return KeyValue.Type.DeleteColumn;
case DELETE_FAMILY:
return KeyValue.Type.DeleteFamily;
case DELETE_FAMILY_VERSION:
return KeyValue.Type.DeleteFamilyVersion;
default:
throw new IOException("Unknown delete type: " + type);
}
}
protected void checkOperationSuccessOnCell(Table table,
KeyValue.Type targetOp,
@Nullable byte[] expectedValue,
byte[] tableName,
byte[] row,
byte[] fam,
byte[] col) {
try {
Get get = new Get(row).setMaxVersions(1);
Result result = table.get(get);
Cell latestCell = result.getColumnLatestCell(fam, col);
switch (targetOp) {
case Put:
assertEquals(latestCell.getTypeByte(), targetOp.getCode());
assertEquals(CellUtil.cloneValue(latestCell), expectedValue);
LOG.trace("Value for " + Bytes.toString(tableName) + ":"
+ Bytes.toString(row) + ":" + Bytes.toString(fam) + ":"
+ Bytes.toString(col) + "=>" + Bytes.toString(CellUtil.cloneValue(latestCell))
+ " (" + Bytes.toString(expectedValue) + " expected)");
break;
case Delete:
LOG.trace("Value for " + Bytes.toString(tableName) + ":"
+ Bytes.toString(row) + ":" + Bytes.toString(fam)
+ Bytes.toString(col) + " deleted");
assertNull(latestCell);
break;
default:
fail();
}
} catch (IOException e) {
LOG.error("Error reading row " + Bytes.toString(tableName) + ":"
+ Bytes.toString(row) + ":" + Bytes.toString(fam)
+ Bytes.toString(col), e);
fail();
}
}
/**
* Conversion method from Subj, Pred, Obj and optional Context into an array of HBase keys
* @param subj subject Resource
* @param pred predicate IRI
* @param obj object Value
* @param context optional context Resource
* @param delete boolean switch whether to get KeyValues for deletion instead of for insertion
* @param timestamp long timestamp value for time-ordering purposes
* @return array of KeyValues
*/
public static KeyValue[] toKeyValues(Resource subj, IRI pred, Value obj, Resource context, boolean delete, long timestamp) {
byte[] sb = writeBytes(subj); // subject bytes
byte[] pb = writeBytes(pred); // predicate bytes
byte[] ob = writeBytes(obj); // object bytes
byte[] cb = context == null ? new byte[0] : writeBytes(context); // context (graph) bytes
byte[] sKey = hashKey(sb); //subject key
byte[] pKey = hashKey(pb); //predicate key
byte[] oKey = hashKey(ob); //object key
//bytes to be used for the HBase column qualifier
byte[] cq = ByteBuffer.allocate(sb.length + pb.length + ob.length + cb.length + 12).putInt(sb.length).putInt(pb.length).putInt(ob.length).put(sb).put(pb).put(ob).put(cb).array();
KeyValue kv[] = new KeyValue[context == null ? PREFIXES : 2 * PREFIXES];
KeyValue.Type type = delete ? KeyValue.Type.DeleteColumn : KeyValue.Type.Put;
//timestamp is shifted one bit left and the last bit is used to prioritize between inserts and deletes of the same time to avoid HBase ambiguity
//inserts are considered always later after deletes on a timeline
timestamp = timestamp << 1;
if (!delete) {
timestamp |= 1;
}
//generate HBase key value pairs from: row, family, qualifier, value. Permutations of SPO (and if needed CSPO) are all stored. Values are actually empty.
kv[0] = new KeyValue(concat(SPO_PREFIX, false, sKey, pKey, oKey), CF_NAME, cq, timestamp, type, EMPTY);
kv[1] = new KeyValue(concat(POS_PREFIX, false, pKey, oKey, sKey), CF_NAME, cq, timestamp, type, EMPTY);
kv[2] = new KeyValue(concat(OSP_PREFIX, false, oKey, sKey, pKey), CF_NAME, cq, timestamp, type, EMPTY);
if (context != null) {
byte[] cKey = hashKey(cb);
kv[3] = new KeyValue(concat(CSPO_PREFIX, false, cKey, sKey, pKey, oKey), CF_NAME, cq, timestamp, type, EMPTY);
kv[4] = new KeyValue(concat(CPOS_PREFIX, false, cKey, pKey, oKey, sKey), CF_NAME, cq, timestamp, type, EMPTY);
kv[5] = new KeyValue(concat(COSP_PREFIX, false, cKey, oKey, sKey, pKey), CF_NAME, cq, timestamp, type, EMPTY);
}
return kv;
}
private static KeyValue.Type generateKeyType(Random rand) {
if (rand.nextBoolean()) {
// Let's make half of KVs puts.
return KeyValue.Type.Put;
} else {
KeyValue.Type keyType = KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) {
throw new RuntimeException("Generated an invalid key type: " + keyType + ". "
+ "Probably the layout of KeyValue.Type has changed.");
}
return keyType;
}
}
public static KeyValue.Type generateKeyType(Random rand) {
if (rand.nextBoolean()) {
// Let's make half of KVs puts.
return KeyValue.Type.Put;
} else {
KeyValue.Type keyType =
KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum)
{
throw new RuntimeException("Generated an invalid key type: " + keyType
+ ". " + "Probably the layout of KeyValue.Type has changed.");
}
return keyType;
}
}
private static KeyValue.Type generateKeyType(Random rand) {
if (rand.nextBoolean()) {
// Let's make half of KVs puts.
return KeyValue.Type.Put;
} else {
KeyValue.Type keyType =
KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) {
throw new RuntimeException("Generated an invalid key type: " + keyType
+ ". " + "Probably the layout of KeyValue.Type has changed.");
}
return keyType;
}
}
public static KeyValue.Type generateKeyType(Random rand) {
if (rand.nextBoolean()) {
// Let's make half of KVs puts.
return KeyValue.Type.Put;
} else {
KeyValue.Type keyType = KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) {
throw new RuntimeException("Generated an invalid key type: " + keyType + ". "
+ "Probably the layout of KeyValue.Type has changed.");
}
return keyType;
}
}
public static KeyValue.Type generateKeyType(Random rand) {
if (rand.nextBoolean()) {
// Let's make half of KVs puts.
return KeyValue.Type.Put;
} else {
KeyValue.Type keyType = KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) {
throw new RuntimeException("Generated an invalid key type: " + keyType + ". "
+ "Probably the layout of KeyValue.Type has changed.");
}
return keyType;
}
}
public static KeyValue.Type generateKeyType(Random rand) {
if (rand.nextBoolean()) {
// Let's make half of KVs puts.
return KeyValue.Type.Put;
} else {
KeyValue.Type keyType =
KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum)
{
throw new RuntimeException("Generated an invalid key type: " + keyType
+ ". " + "Probably the layout of KeyValue.Type has changed.");
}
return keyType;
}
}
public static MutationProto toMutation(final MutationType type, final Mutation mutation,
MutationProto.Builder builder, long nonce)
throws IOException {
builder = getMutationBuilderAndSetCommonFields(type, mutation, builder);
if (nonce != HConstants.NO_NONCE) {
builder.setNonce(nonce);
}
if (type == MutationType.INCREMENT) {
builder.setTimeRange(ProtobufUtil.toTimeRange(((Increment) mutation).getTimeRange()));
}
if (type == MutationType.APPEND) {
builder.setTimeRange(ProtobufUtil.toTimeRange(((Append) mutation).getTimeRange()));
}
ColumnValue.Builder columnBuilder = ColumnValue.newBuilder();
QualifierValue.Builder valueBuilder = QualifierValue.newBuilder();
for (Map.Entry<byte[],List<Cell>> family: mutation.getFamilyCellMap().entrySet()) {
columnBuilder.clear();
columnBuilder.setFamily(UnsafeByteOperations.unsafeWrap(family.getKey()));
for (Cell cell: family.getValue()) {
valueBuilder.clear();
valueBuilder.setQualifier(UnsafeByteOperations.unsafeWrap(
cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
valueBuilder.setValue(UnsafeByteOperations.unsafeWrap(
cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
valueBuilder.setTimestamp(cell.getTimestamp());
if (type == MutationType.DELETE || (type == MutationType.PUT && CellUtil.isDelete(cell))) {
KeyValue.Type keyValueType = KeyValue.Type.codeToType(cell.getTypeByte());
valueBuilder.setDeleteType(toDeleteType(keyValueType));
}
columnBuilder.addQualifierValue(valueBuilder.build());
}
builder.addColumnValue(columnBuilder.build());
}
return builder.build();
}
private KeyValue build(ImmutableBytesWritable row, ImmutableBytesWritable family, ImmutableBytesWritable qualifier,
long ts, KeyValue.Type type, ImmutableBytesWritable value) {
return new KeyValue(copyBytesIfNecessary(row), copyBytesIfNecessary(family), copyBytesIfNecessary(qualifier),
ts, type, value == null ? null : copyBytesIfNecessary(value));
}
private KeyValue build(ImmutableBytesWritable row, ImmutableBytesWritable family, ImmutableBytesWritable qualifier,
long ts, KeyValue.Type type, ImmutableBytesWritable value) {
return new KeyValue(copyBytesIfNecessary(row), copyBytesIfNecessary(family), copyBytesIfNecessary(qualifier),
ts, type, value == null ? null : copyBytesIfNecessary(value));
}
void addCellToDelMutation(Delete del, byte[] family, byte[] column, long ts, KeyValue.Type type) throws Exception {
byte[] rowKey = del.getRow();
Cell cell = CellUtil.createCell(rowKey, family, column, ts, type.getCode(), null);
del.addDeleteMarker(cell);
}
private Cell getEmptyCell(Mutation orig, List<Cell> origList, Long ts, KeyValue.Type type,
boolean verified) {
return CellUtil.createCell(orig.getRow(), CellUtil.cloneFamily(origList.get(0)),
indexMaintainer.getEmptyKeyValueQualifier(),
ts, type.getCode(), verified ? VERIFIED_BYTES : UNVERIFIED_BYTES);
}
private Cell getNewPutCell(Mutation orig, List<Cell> origList, Long ts, KeyValue.Type type) {
return CellUtil.createCell(orig.getRow(),
CellUtil.cloneFamily(origList.get(0)), Bytes.toBytes(INCLUDED_COLUMN),
ts, type.getCode(), Bytes.toBytes("asdfg"));
}
private KeyValue build(ImmutableBytesWritable row, ImmutableBytesWritable family,
ImmutableBytesWritable qualifier, long ts, KeyValue.Type type, ImmutableBytesWritable value) {
return new KeyValue(copyBytesIfNecessary(row), copyBytesIfNecessary(family),
copyBytesIfNecessary(qualifier), ts, type, value == null? null: copyBytesIfNecessary(value));
}