Java Code Examples for org.apache.cassandra.utils.ByteBufferUtil#string()

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil#string() . 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 File: WordCount.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public void map(ByteBuffer key, SortedMap<ByteBuffer, Cell> columns, Context context) throws IOException, InterruptedException
{
    for (Cell cell : columns.values())
    {
        String name  = ByteBufferUtil.string(cell.name().toByteBuffer());
        String value = null;
        
        if (name.contains("int"))
            value = String.valueOf(ByteBufferUtil.toInt(cell.value()));
        else
            value = ByteBufferUtil.string(cell.value());
                       
        logger.debug("read {}:{}={} from {}",
                     new Object[] {ByteBufferUtil.string(key), name, value, context.getInputSplit()});

        StringTokenizer itr = new StringTokenizer(value);
        while (itr.hasMoreTokens())
        {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
 
Example 2
Source File: WordCount.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public void map(Map<String, ByteBuffer> keys, Map<String, ByteBuffer> columns, Context context) throws IOException, InterruptedException
{
    for (Entry<String, ByteBuffer> column : columns.entrySet())
    {
        if (!"line".equalsIgnoreCase(column.getKey()))
            continue;

        String value = ByteBufferUtil.string(column.getValue());

        StringTokenizer itr = new StringTokenizer(value);
        while (itr.hasMoreTokens())
        {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
 
Example 3
Source File: NameSortTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void validateNameSort(Keyspace keyspace, int N) throws IOException
{
    for (int i = 0; i < N; ++i)
    {
        DecoratedKey key = Util.dk(Integer.toString(i));
        ColumnFamily cf;

        cf = Util.getColumnFamily(keyspace, key, "Standard1");
        Collection<Cell> cells = cf.getSortedColumns();
        for (Cell cell : cells)
        {
            String name = ByteBufferUtil.string(cell.name().toByteBuffer());
            int j = Integer.valueOf(name.substring(name.length() - 1));
            byte[] bytes = j % 2 == 0 ? "a".getBytes() : "b".getBytes();
            assertEquals(new String(bytes), ByteBufferUtil.string(cell.value()));
        }
    }
}
 
Example 4
Source File: WordCountCounters.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private String toString(Map<String, ByteBuffer> keys)
{
    String result = "";
    try
    {
        for (ByteBuffer key : keys.values())
            result = result + ByteBufferUtil.string(key) + ":";
    }
    catch (CharacterCodingException e)
    {
        logger.error("Failed to print keys", e);
    }
    return result;
}
 
Example 5
Source File: CqlRecordWriter.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/** retrieve the key validator from system.schema_columnfamilies table */
private void retrievePartitionKeyValidator(Cassandra.Client client) throws Exception
{
    String keyspace = ConfigHelper.getOutputKeyspace(conf);
    String cfName = ConfigHelper.getOutputColumnFamily(conf);
    String query = "SELECT key_validator," +
    		       "       key_aliases," +
    		       "       column_aliases " +
                   "FROM system.schema_columnfamilies " +
                   "WHERE keyspace_name='%s' and columnfamily_name='%s'";
    String formatted = String.format(query, keyspace, cfName);
    CqlResult result = client.execute_cql3_query(ByteBufferUtil.bytes(formatted), Compression.NONE, ConsistencyLevel.ONE);

    Column rawKeyValidator = result.rows.get(0).columns.get(0);
    String validator = ByteBufferUtil.string(ByteBuffer.wrap(rawKeyValidator.getValue()));
    keyValidator = parseType(validator);
    
    Column rawPartitionKeys = result.rows.get(0).columns.get(1);
    String keyString = ByteBufferUtil.string(ByteBuffer.wrap(rawPartitionKeys.getValue()));
    logger.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++;
    }

    Column rawClusterColumns = result.rows.get(0).columns.get(2);
    String clusterColumnString = ByteBufferUtil.string(ByteBuffer.wrap(rawClusterColumns.getValue()));

    logger.debug("cluster columns: {}", clusterColumnString);
    clusterColumns = FBUtilities.fromJsonList(clusterColumnString);
}
 
Example 6
Source File: OrderPreservingPartitioner.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public Token fromByteArray(ByteBuffer bytes)
{
    try
    {
        return new StringToken(ByteBufferUtil.string(bytes));
    }
    catch (CharacterCodingException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: OrderPreservingPartitioner.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public StringToken getToken(ByteBuffer key)
{
    String skey;
    try
    {
        skey = ByteBufferUtil.string(key);
    }
    catch (CharacterCodingException e)
    {
        skey = ByteBufferUtil.bytesToHex(key);
    }
    return new StringToken(skey);
}
 
Example 8
Source File: PerRowSecondaryIndex.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public String getNameForSystemKeyspace(ByteBuffer columnName)
{
    try
    {
        return getIndexName()+ByteBufferUtil.string(columnName);
    }
    catch (CharacterCodingException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: AbstractTextSerializer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public String deserialize(ByteBuffer bytes)
{
    try
    {
        return ByteBufferUtil.string(bytes, charset);
    }
    catch (CharacterCodingException e)
    {
        throw new MarshalException("Invalid " + charset + " bytes " + ByteBufferUtil.bytesToHex(bytes));
    }
}
 
Example 10
Source File: Util.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static String string(ByteBuffer bb)
{
    try
    {
        return ByteBufferUtil.string(bb);
    }
    catch (Exception e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: QueryPagerTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static String string(ByteBuffer bb)
{
    try
    {
        return ByteBufferUtil.string(bb);
    }
    catch (CharacterCodingException e)
    {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: AbstractCassandraStorage.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/** return the CfInfo 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);

    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);
        if (FBUtilities.fromJsonList(keyAliases).size() > 0)
            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;
}
 
Example 13
Source File: DynamicCompositeType.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
protected AbstractType<?> validateComparator(int i, ByteBuffer bb) throws MarshalException
{
    AbstractType<?> comparator = null;
    if (bb.remaining() < 2)
        throw new MarshalException("Not enough bytes to header of the comparator part of component " + i);
    int header = ByteBufferUtil.readShortLength(bb);
    if ((header & 0x8000) == 0)
    {
        if (bb.remaining() < header)
            throw new MarshalException("Not enough bytes to read comparator name of component " + i);

        ByteBuffer value = ByteBufferUtil.readBytes(bb, header);
        String valueStr = null;
        try
        {
            valueStr = ByteBufferUtil.string(value);
            comparator = TypeParser.parse(valueStr);
        }
        catch (CharacterCodingException ce) 
        {
            // ByteBufferUtil.string failed. 
            // Log it here and we'll further throw an exception below since comparator == null
            logger.error("Failed with [{}] when decoding the byte buffer in ByteBufferUtil.string()", 
               ce.toString());
        }
        catch (Exception e)
        {
            // parse failed. 
            // Log it here and we'll further throw an exception below since comparator == null
            logger.error("Failed to parse value string \"{}\" with exception: [{}]", 
               valueStr, e.toString());
        }
    }
    else
    {
        comparator = aliases.get((byte)(header & 0xFF));
    }

    if (comparator == null)
        throw new MarshalException("Cannot find comparator for component " + i);
    else
        return comparator;
}
 
Example 14
Source File: CassandraServer.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static String uncompress(ByteBuffer query, Compression compression) throws InvalidRequestException
{
    String queryString = null;

    // Decompress the query string.
    try
    {
        switch (compression)
        {
            case GZIP:
                DataOutputBuffer decompressed = new DataOutputBuffer();
                byte[] outBuffer = new byte[1024], inBuffer = new byte[1024];

                Inflater decompressor = new Inflater();

                int lenRead = 0;
                while (true)
                {
                    if (decompressor.needsInput())
                        lenRead = query.remaining() < 1024 ? query.remaining() : 1024;
                    query.get(inBuffer, 0, lenRead);
                    decompressor.setInput(inBuffer, 0, lenRead);

                    int lenWrite = 0;
                    while ((lenWrite = decompressor.inflate(outBuffer)) != 0)
                        decompressed.write(outBuffer, 0, lenWrite);

                    if (decompressor.finished())
                        break;
                }

                decompressor.end();

                queryString = new String(decompressed.getData(), 0, decompressed.getLength(), StandardCharsets.UTF_8);
                break;
            case NONE:
                try
                {
                    queryString = ByteBufferUtil.string(query);
                }
                catch (CharacterCodingException ex)
                {
                    throw new InvalidRequestException(ex.getMessage());
                }
                break;
        }
    }
    catch (DataFormatException e)
    {
        throw new InvalidRequestException("Error deflating query string.");
    }
    return queryString;
}