Example usage for org.apache.cassandra.db.marshal CompositeType build

List of usage examples for org.apache.cassandra.db.marshal CompositeType build

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal CompositeType build.

Prototype

public static ByteBuffer build(ByteBuffer... buffers) 

Source Link

Usage

From source file:com.spotify.hdfs2cass.cassandra.utils.CassandraRecordUtils.java

License:Open Source License

public static ByteBuffer toByteBuffer(final Object value) {
    if (value == null) {
        return ByteBufferUtil.EMPTY_BYTE_BUFFER;
    } else if (value instanceof CharSequence) {
        return ByteBufferUtil.bytes(value.toString());
    } else if (value instanceof Double) {
        return ByteBufferUtil.bytes((Double) value);
    } else if (value instanceof Float) {
        return ByteBufferUtil.bytes((Float) value);
    } else if (value instanceof Integer) {
        return ByteBufferUtil.bytes((Integer) value);
    } else if (value instanceof Long) {
        return ByteBufferUtil.bytes((Long) value);
    } else if (value instanceof ByteBuffer) {
        return ByteBufferUtil.clone((ByteBuffer) value);
    } else if (value instanceof GenericData.Array) {
        return serializeList((GenericData.Array) value);
    } else if (value instanceof SpecificRecord) {
        List<ByteBuffer> buffers = Lists.newArrayList();
        SpecificRecord record = (SpecificRecord) value;
        for (Schema.Field field : record.getSchema().getFields()) {
            buffers.add(toByteBuffer(record.get(field.pos())));
        }//from  w  ww  .j ava 2  s.c o m
        return CompositeType.build(buffers.toArray(new ByteBuffer[0]));
    } else if (value instanceof Map) {
        return serializeMap((Map<?, ?>) value);
    } else if (value instanceof Set) {
        return serializeSet((Set<?>) value);
    } else if (value instanceof List) {
        return serializeList((List<?>) value);
    } else if (value instanceof UUID) {
        return ByteBufferUtil.bytes((UUID) value);
    }

    throw new CrunchRuntimeException("Can not transform field (class: " + value.getClass() + ") to ByteBuffer");
}

From source file:com.stratio.deep.cassandra.cql.DeepRecordReader.java

License:Apache License

/**
 * check whether current row is at the end of range
 *
 * @return the boolean//w w  w  .  ja  v a  2  s. c  o m
 */
private boolean reachEndRange() {
    // current row key
    ByteBuffer rowKey;

    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[partitionBoundColumns.size()];
        for (int i = 0; i < partitionBoundColumns.size(); i++) {
            keys[i] = partitionBoundColumns.get(i).value.duplicate();
        }

        rowKey = CompositeType.build(keys);
    } else {
        rowKey = partitionBoundColumns.get(0).value;
    }

    String endToken = String.valueOf(split.getEndToken());
    String currentToken = partitioner.getToken(rowKey).toString();

    return endToken.equals(currentToken);
}

From source file:com.stratio.deep.cassandra.cql.DeepRecordReader.java

License:Apache License

/**
 * Builds the partition key in {@link ByteBuffer} format for the given values.
 *
 * @param equalsList List of equals field and value pairs.
 * @param inValue    Value for the operator in.
 * @return with the partition key./*from  w ww  . jav a2 s. c om*/
 */
private ByteBuffer getPartitionKey(List<Pair<String, Serializable>> equalsList, Serializable inValue) {

    assert (equalsList.size() + 1) == ((CompositeType) keyValidator).componentsCount();

    ByteBuffer[] serialized = new ByteBuffer[equalsList.size() + 1];
    for (int i = 0; i < equalsList.size(); i++) {
        ByteBuffer buffer = ((AbstractType) keyValidator.getComponents().get(i))
                .decompose(equalsList.get(i).right);
        serialized[i] = buffer;
    }
    serialized[serialized.length - 1] = ((AbstractType) keyValidator.getComponents().get(serialized.length - 1))
            .decompose(inValue);

    return CompositeType.build(serialized);
}

From source file:com.stratio.deep.cassandra.util.CassandraUtils.java

License:Apache License

/**
 * Returns the partition key related to a given {@link Cells}.
 *
 * @param cells        {@link Cells} from Cassandra to extract the partition key.
 * @param keyValidator Cassandra key type.
 * @param numberOfKeys Number of keys.//from  ww w  .  j  ava 2  s.  c om
 * @return Partition key.
 */
public static ByteBuffer getPartitionKey(Cells cells, AbstractType<?> keyValidator, int numberOfKeys) {
    ByteBuffer partitionKey;
    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[numberOfKeys];

        for (int i = 0; i < cells.size(); i++) {
            Cell c = cells.getCellByIdx(i);

            if (c.isKey()) {
                keys[i] = DataType.serializeValue(c.getValue(), CassandraDeepJobConfig.PROTOCOL_VERSION);
            }
        }

        partitionKey = CompositeType.build(keys);
    } else {
        Cell cell = cells.getCellByIdx(0);
        partitionKey = DataType.serializeValue(cell.getValue(), CassandraDeepJobConfig.PROTOCOL_VERSION);
    }
    return partitionKey;
}

From source file:com.tuplejump.calliope.hadoop.cql3.CqlPagingRecordReader.java

License:Apache License

/** check whether current row is at the end of range */
private boolean reachEndRange() {
    // current row key
    ByteBuffer rowKey;/*from   w  ww . j  a  va2s  .  c  o m*/
    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[partitionBoundColumns.size()];
        for (int i = 0; i < partitionBoundColumns.size(); i++)
            keys[i] = partitionBoundColumns.get(i).value.duplicate();

        rowKey = CompositeType.build(keys);
    } else {
        rowKey = partitionBoundColumns.get(0).value;
    }

    String endToken = split.getEndToken();
    String currentToken = partitioner.getToken(rowKey).toString();
    logger.debug("End token: {}, current token: {}", endToken, currentToken);

    return endToken.equals(currentToken);
}

From source file:com.tuplejump.calliope.hadoop.cql3.CqlRecordWriter.java

License:Apache License

private ByteBuffer getPartitionKey(Map<String, ByteBuffer> keyColumns) {
    ByteBuffer partitionKey;/*from ww  w .j a v a 2  s  .c  o  m*/
    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[partitionKeyColumns.length];
        for (int i = 0; i < keys.length; i++)
            keys[i] = keyColumns.get(partitionKeyColumns[i]);

        partitionKey = CompositeType.build(keys);
    } else {
        partitionKey = keyColumns.get(partitionKeyColumns[0]);
    }
    return partitionKey;
}

From source file:kina.cql.CqlRecordWriter.java

License:Apache License

private ByteBuffer getPartitionKey(Cells cells) {
    ByteBuffer partitionKey;/*w  w  w.j a va  2s .co  m*/
    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[partitionKeyColumns.length];

        for (int i = 0; i < cells.size(); i++) {
            CassandraCell c = (CassandraCell) cells.getCellByIdx(i);

            if (c.isPartitionKey()) {
                keys[i] = c.getDecomposedCellValue();
            }
        }

        partitionKey = CompositeType.build(keys);
    } else {
        partitionKey = ((CassandraCell) cells.getCellByIdx(0)).getDecomposedCellValue();
    }
    return partitionKey;
}

From source file:org.elassandra.cluster.InternalCassandraClusterService.java

License:Apache License

@Override
public Token getToken(ByteBuffer rowKey, ColumnFamily cf) {
    IPartitioner partitioner = StorageService.getPartitioner();
    CType ctype = cf.metadata().getKeyValidatorAsCType();
    if (ctype.isCompound()) {
        Composite composite = ctype.fromByteBuffer(rowKey);
        ByteBuffer[] bb = new ByteBuffer[cf.metadata().partitionKeyColumns().size()];
        for (int i = 0; i < cf.metadata().partitionKeyColumns().size(); i++) {
            bb[i] = composite.get(i);/*w  w w .  j  a v a 2 s.co m*/
        }
        return partitioner.getToken(CompositeType.build(bb));
    }
    return partitioner.getToken(rowKey);
}

From source file:org.elasticsearch.cassandra.cluster.InternalCassandraClusterService.java

License:Apache License

@Override
public Token getToken(ByteBuffer rowKey, ColumnFamily cf) {
    IPartitioner partitioner = StorageService.instance.getPartitioner();
    CType ctype = cf.metadata().getKeyValidatorAsCType();
    if (ctype.isCompound()) {
        Composite composite = ctype.fromByteBuffer(rowKey);
        ByteBuffer[] bb = new ByteBuffer[cf.metadata().partitionKeyColumns().size()];
        for (int i = 0; i < cf.metadata().partitionKeyColumns().size(); i++) {
            bb[i] = composite.get(i);//from   w w  w.j  a  v a  2 s . c  o  m
        }
        return partitioner.getToken(CompositeType.build(bb));
    }
    return partitioner.getToken(rowKey);
}