Example usage for org.apache.cassandra.db.marshal AbstractType toString

List of usage examples for org.apache.cassandra.db.marshal AbstractType toString

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

This must be overriden by subclasses if necessary so that for any AbstractType, this == TypeParser.parse(toString()).

Usage

From source file:com.dse.pig.udfs.CqlStorage.java

License:Apache License

/** get keys meta data */
protected List<ColumnDef> getKeysMeta(Cassandra.Client client) throws Exception {
    String query = "SELECT key_aliases, " + "       column_aliases, " + "       key_validator, "
            + "       comparator, " + "       keyspace_name, " + "       value_alias, "
            + "       default_validator " + "FROM system.schema_columnfamilies " + "WHERE keyspace_name = '%s'"
            + "  AND columnfamily_name = '%s' ";

    CqlResult result = client.execute_cql3_query(
            ByteBufferUtil.bytes(String.format(query, keyspace, column_family)), Compression.NONE,
            ConsistencyLevel.ONE);/*ww w .  j a va 2  s  . c om*/

    if (result == null || result.rows == null || result.rows.isEmpty())
        return null;

    Iterator<CqlRow> iteraRow = result.rows.iterator();
    List<ColumnDef> keys = new ArrayList<ColumnDef>();
    if (iteraRow.hasNext()) {
        CqlRow cqlRow = iteraRow.next();
        String name = ByteBufferUtil.string(cqlRow.columns.get(4).value);
        logger.debug("Found ksDef name: {}", name);
        String keyString = ByteBufferUtil.string(ByteBuffer.wrap(cqlRow.columns.get(0).getValue()));

        logger.debug("partition keys: {}", keyString);
        List<String> keyNames = FBUtilities.fromJsonList(keyString);

        Iterator<String> iterator = keyNames.iterator();
        while (iterator.hasNext()) {
            ColumnDef cDef = new ColumnDef();
            cDef.name = ByteBufferUtil.bytes(iterator.next());
            keys.add(cDef);
        }
        // classic thrift tables
        if (keys.size() == 0) {
            CFDefinition cfDefinition = getCfDefinition(keyspace, column_family, client);
            for (ColumnIdentifier column : cfDefinition.keys.keySet()) {
                String key = column.toString();
                logger.debug("name: {} ", key);
                ColumnDef cDef = new ColumnDef();
                cDef.name = ByteBufferUtil.bytes(key);
                keys.add(cDef);
            }
            for (ColumnIdentifier column : cfDefinition.columns.keySet()) {
                String key = column.toString();
                logger.debug("name: {} ", key);
                ColumnDef cDef = new ColumnDef();
                cDef.name = ByteBufferUtil.bytes(key);
                keys.add(cDef);
            }
        }

        keyString = ByteBufferUtil.string(ByteBuffer.wrap(cqlRow.columns.get(1).getValue()));

        logger.debug("cluster keys: {}", keyString);
        keyNames = FBUtilities.fromJsonList(keyString);

        iterator = keyNames.iterator();
        while (iterator.hasNext()) {
            ColumnDef cDef = new ColumnDef();
            cDef.name = ByteBufferUtil.bytes(iterator.next());
            keys.add(cDef);
        }

        String validator = ByteBufferUtil.string(ByteBuffer.wrap(cqlRow.columns.get(2).getValue()));
        logger.debug("row key validator: {}", validator);
        AbstractType<?> keyValidator = parseType(validator);

        Iterator<ColumnDef> keyItera = keys.iterator();
        if (keyValidator instanceof CompositeType) {
            Iterator<AbstractType<?>> typeItera = ((CompositeType) keyValidator).types.iterator();
            while (typeItera.hasNext())
                keyItera.next().validation_class = typeItera.next().toString();
        } else
            keyItera.next().validation_class = keyValidator.toString();

        validator = ByteBufferUtil.string(ByteBuffer.wrap(cqlRow.columns.get(3).getValue()));
        logger.debug("cluster key validator: {}", validator);

        if (keyItera.hasNext() && validator != null && !validator.isEmpty()) {
            AbstractType<?> clusterKeyValidator = parseType(validator);

            if (clusterKeyValidator instanceof CompositeType) {
                Iterator<AbstractType<?>> typeItera = ((CompositeType) clusterKeyValidator).types.iterator();
                while (keyItera.hasNext())
                    keyItera.next().validation_class = typeItera.next().toString();
            } else
                keyItera.next().validation_class = clusterKeyValidator.toString();
        }

        // compact value_alias column
        if (cqlRow.columns.get(5).value != null) {
            try {
                String compactValidator = ByteBufferUtil
                        .string(ByteBuffer.wrap(cqlRow.columns.get(6).getValue()));
                logger.debug("default validator: {}", compactValidator);
                AbstractType<?> defaultValidator = parseType(compactValidator);

                ColumnDef cDef = new ColumnDef();
                cDef.name = cqlRow.columns.get(5).value;
                cDef.validation_class = defaultValidator.toString();
                keys.add(cDef);
                hasCompactValueAlias = true;
            } catch (Exception e) {
                // no compact column at value_alias
            }
        }

    }
    return keys;
}

From source file:com.perpetumobile.bit.orm.cassandra.CliMain.java

License:Apache License

/**
 * Used to convert value (function argument, string) into byte[]
 * @param functionCall - tree representing function call ^(FUNCTION_CALL function_name value)
 * @param columnFamily - column family definition (CfDef)
 * @param columnName   - column name as byte[] (used to update CfDef)
 * @param withUpdate   - also updates column family metadata for given column
 * @return byte[] - string value as byte[]
 *//*from   w w w  .  j  a  v a2 s.  co  m*/
private ByteBuffer convertValueByFunction(Tree functionCall, CfDef columnFamily, ByteBuffer columnName,
        boolean withUpdate) {
    String functionName = functionCall.getChild(0).getText();
    Tree argumentTree = functionCall.getChild(1);
    String functionArg = (argumentTree == null) ? "" : CliUtils.unescapeSQLString(argumentTree.getText());
    AbstractType<?> validator = getTypeByFunction(functionName);

    try {

        ByteBuffer value;

        if (functionArg.isEmpty()) {
            if (validator instanceof TimeUUIDType) {
                value = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
            } else if (validator instanceof LexicalUUIDType) {
                value = ByteBuffer.wrap(UUIDGen.decompose(UUID.randomUUID()));
            } else if (validator instanceof BytesType) {
                value = ByteBuffer.wrap(new byte[0]);
            } else {
                throw new RuntimeException(
                        String.format("Argument for '%s' could not be empty.", functionName));
            }
        } else {
            value = getBytesAccordingToType(functionArg, validator);
        }

        // performing ColumnDef local validator update
        if (withUpdate) {
            updateColumnMetaData(columnFamily, columnName, validator.toString());
        }

        return value;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}