Example usage for org.apache.cassandra.utils FBUtilities fromJsonList

List of usage examples for org.apache.cassandra.utils FBUtilities fromJsonList

Introduction

In this page you can find the example usage for org.apache.cassandra.utils FBUtilities fromJsonList.

Prototype

public static List<String> fromJsonList(String json) 

Source Link

Usage

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

License:Apache License

/** return the CfInf for the column family */
protected CfInfo getCfInfo(Cassandra.Client client) throws InvalidRequestException, UnavailableException,
        TimedOutException, SchemaDisagreementException, TException, NotFoundException,
        org.apache.cassandra.exceptions.InvalidRequestException, ConfigurationException, IOException {
    // get CF meta data
    String query = "SELECT type," + "       comparator," + "       subcomparator," + "       default_validator,"
            + "       key_validator," + "       key_aliases " + "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);// w ww  .j  av a  2s  . c o m

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

    Iterator<CqlRow> iteraRow = result.rows.iterator();
    CfDef cfDef = new CfDef();
    cfDef.keyspace = keyspace;
    cfDef.name = column_family;
    boolean cql3Table = false;
    if (iteraRow.hasNext()) {
        CqlRow cqlRow = iteraRow.next();

        cfDef.column_type = ByteBufferUtil.string(cqlRow.columns.get(0).value);
        cfDef.comparator_type = ByteBufferUtil.string(cqlRow.columns.get(1).value);
        ByteBuffer subComparator = cqlRow.columns.get(2).value;
        if (subComparator != null)
            cfDef.subcomparator_type = ByteBufferUtil.string(subComparator);
        cfDef.default_validation_class = ByteBufferUtil.string(cqlRow.columns.get(3).value);
        cfDef.key_validation_class = ByteBufferUtil.string(cqlRow.columns.get(4).value);
        String keyAliases = ByteBufferUtil.string(cqlRow.columns.get(5).value);
        List<String> keys = FBUtilities.fromJsonList(keyAliases);
        // classis thrift tables
        if (keys.size() == 0) {
            CFDefinition cfDefinition = getCfDefinition(keyspace, column_family, client);
            for (ColumnIdentifier column : cfDefinition.keys.keySet()) {
                String key = column.toString();
                String type = cfDefinition.keys.get(column).type.toString();
                logger.debug("name: {}, type: {} ", key, type);
                keys.add(key);
            }
        } else
            cql3Table = true;
    }
    cfDef.column_metadata = getColumnMetadata(client);
    CfInfo cfInfo = new CfInfo();
    cfInfo.cfDef = cfDef;
    if (cql3Table && !(parseType(cfDef.comparator_type) instanceof AbstractCompositeType))
        cfInfo.compactCqlTable = true;

    if (cql3Table)
        cfInfo.cql3Table = true;
    return cfInfo;
}

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);//from w w w .  j  a v a2 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:kina.cql.CqlRecordWriter.java

License:Apache License

/**
 * retrieve the key validator from system.schema_columnfamilies table
 *///  w  ww .  ja v a  2s. c  o  m
protected void retrievePartitionKeyValidator() throws ConfigurationException {
    Pair<Session, String> sessionWithHost = CassandraClientProvider
            .trySessionForLocation(localhost.getHostAddress(), writeConfig, false);

    String keyspace = writeConfig.getKeyspace();
    String cfName = writeConfig.getColumnFamily();

    Row row = getRowMetadata(sessionWithHost, keyspace, cfName);

    if (row == null) {
        throw new IOException(String.format("cannot find metadata for %s.%s", keyspace, cfName));
    }

    String validator = row.getString("key_validator");
    keyValidator = parseType(validator);

    String keyString = row.getString("key_aliases");
    LOG.debug("partition keys: " + keyString);

    List<String> keys = FBUtilities.fromJsonList(keyString);
    partitionKeyColumns = new String[keys.size()];
    int i = 0;
    for (String key : keys) {
        partitionKeyColumns[i] = key;
        i++;
    }

    String clusterColumnString = row.getString("column_aliases");

    LOG.debug("cluster columns: " + clusterColumnString);
}