Java Code Examples for org.apache.hadoop.hbase.KeyValue#Type

The following examples show how to use org.apache.hadoop.hbase.KeyValue#Type . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hbase   File: ProtobufUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 2
Source Project: hbase   File: ProtobufUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 3
Source Project: phoenix-omid   File: TestTxMgrFailover.java    License: Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 4
Source Project: Halyard   File: HalyardTableUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source Project: hbase   File: TestStoreScannerClosure.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 6
Source Project: hbase   File: CreateRandomStoreFile.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 7
Source Project: hbase   File: TestCacheOnWriteInSchema.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 8
Source Project: hbase   File: TestCacheOnWrite.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 9
Source Project: hbase   File: TestHFile.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 10
Source Project: hbase   File: TestPrefetch.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 11
Source Project: hbase   File: ProtobufUtil.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 12
Source Project: phoenix   File: GenericKeyValueBuilder.java    License: Apache License 2.0 4 votes vote down vote up
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));
}
 
Example 13
Source Project: phoenix   File: GenericKeyValueBuilder.java    License: Apache License 2.0 4 votes vote down vote up
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));
}
 
Example 14
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);
}
 
Example 15
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);
}
 
Example 16
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"));
}
 
Example 17
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));
}