Example usage for org.apache.cassandra.db.marshal TypeParser parse

List of usage examples for org.apache.cassandra.db.marshal TypeParser parse

Introduction

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

Prototype

public static AbstractType<?> parse(CharSequence compareWith) throws SyntaxException, ConfigurationException 

Source Link

Usage

From source file:com.datastax.driver.core.ColumnMetadata.java

License:Apache License

static ColumnMetadata build(TableMetadata tm, Row row) {
    try {//from  www.j a v  a  2  s  . co m
        String name = row.getString(COLUMN_NAME);

        String validator = row.getString(VALIDATOR);
        // Ugly special case for TimestampType as we don't have it yet. We should get rid of that later.
        if (validator.equals("org.apache.cassandra.db.marshal.TimestampType"))
            validator = "org.apache.cassandra.db.marshal.DateType";
        AbstractType<?> t = TypeParser.parse(validator);
        ColumnMetadata cm = new ColumnMetadata(tm, name, Codec.rawTypeToDataType(t), row);
        tm.add(cm);
        return cm;
    } catch (RequestValidationException e) {
        // The server will have validated the type
        throw new RuntimeException(e);
    }
}

From source file:com.datastax.driver.core.TableMetadata.java

License:Apache License

static TableMetadata build(KeyspaceMetadata ksm, Row row, boolean hasColumnMetadata) {
    try {//from  w w w.  j av a  2  s . co  m
        String name = row.getString(CF_NAME);

        List<ColumnMetadata> partitionKey = new ArrayList<ColumnMetadata>();
        List<ColumnMetadata> clusteringKey = new ArrayList<ColumnMetadata>();
        // We use a linked hashmap because we will keep this in the order of a 'SELECT * FROM ...'.
        LinkedHashMap<String, ColumnMetadata> columns = new LinkedHashMap<String, ColumnMetadata>();

        // First, figure out which kind of table we are
        boolean isCompact = false;
        AbstractType<?> ct = TypeParser.parse(row.getString(COMPARATOR));
        boolean isComposite = ct instanceof CompositeType;
        List<AbstractType<?>> columnTypes = isComposite ? ((CompositeType) ct).types
                : Collections.<AbstractType<?>>singletonList(ct);
        List<String> columnAliases = fromJsonList(row.getString(COLUMN_ALIASES));
        int clusteringSize;
        boolean hasValue;
        int last = columnTypes.size() - 1;
        AbstractType<?> lastType = columnTypes.get(last);
        if (isComposite) {
            if (lastType instanceof ColumnToCollectionType
                    || (columnAliases.size() == last && lastType instanceof UTF8Type)) {
                hasValue = false;
                clusteringSize = lastType instanceof ColumnToCollectionType ? last - 1 : last;
            } else {
                isCompact = true;
                hasValue = true;
                clusteringSize = columnTypes.size();
            }
        } else {
            isCompact = true;
            if (!columnAliases.isEmpty() || !hasColumnMetadata) {
                hasValue = true;
                clusteringSize = columnTypes.size();
            } else {
                hasValue = false;
                clusteringSize = 0;
            }
        }

        TableMetadata tm = new TableMetadata(ksm, name, partitionKey, clusteringKey, columns,
                new Options(row, isCompact));

        // Partition key
        AbstractType<?> kt = TypeParser.parse(row.getString(KEY_VALIDATOR));
        List<AbstractType<?>> keyTypes = kt instanceof CompositeType ? ((CompositeType) kt).types
                : Collections.<AbstractType<?>>singletonList(kt);

        // check if key_aliases is null, and set to [] due to CASSANDRA-5101
        List<String> keyAliases = row.getString(KEY_ALIASES) == null ? Collections.<String>emptyList()
                : fromJsonList(row.getString(KEY_ALIASES));
        for (int i = 0; i < keyTypes.size(); i++) {
            String cn = keyAliases.size() > i ? keyAliases.get(i)
                    : (i == 0 ? DEFAULT_KEY_ALIAS : DEFAULT_KEY_ALIAS + (i + 1));
            DataType dt = Codec.rawTypeToDataType(keyTypes.get(i));
            ColumnMetadata colMeta = new ColumnMetadata(tm, cn, dt, null);
            columns.put(cn, colMeta);
            partitionKey.add(colMeta);
        }

        // Clustering key
        for (int i = 0; i < clusteringSize; i++) {
            String cn = columnAliases.size() > i ? columnAliases.get(i) : DEFAULT_COLUMN_ALIAS + (i + 1);
            DataType dt = Codec.rawTypeToDataType(columnTypes.get(i));
            ColumnMetadata colMeta = new ColumnMetadata(tm, cn, dt, null);
            columns.put(cn, colMeta);
            clusteringKey.add(colMeta);
        }

        // Value alias (if present)
        if (hasValue) {
            AbstractType<?> vt = TypeParser.parse(row.getString(VALIDATOR));
            String valueAlias = row.isNull(KEY_ALIASES) ? DEFAULT_VALUE_ALIAS : row.getString(VALUE_ALIAS);
            ColumnMetadata vm = new ColumnMetadata(tm, valueAlias, Codec.rawTypeToDataType(vt), null);
            columns.put(valueAlias, vm);
        }

        ksm.add(tm);
        return tm;
    } catch (RequestValidationException e) {
        // The server will have validated the type
        throw new RuntimeException(e);
    }
}

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

License:Apache License

/** get the validators */
protected Map<ByteBuffer, AbstractType> getValidatorMap(CfDef cfDef) throws IOException {
    Map<ByteBuffer, AbstractType> validators = new HashMap<ByteBuffer, AbstractType>();
    for (ColumnDef cd : cfDef.getColumn_metadata()) {
        if (cd.getValidation_class() != null && !cd.getValidation_class().isEmpty()) {
            AbstractType validator = null;
            try {
                validator = TypeParser.parse(cd.getValidation_class());
                if (validator instanceof CounterColumnType)
                    validator = LongType.instance;
                validators.put(cd.name, validator);
            } catch (ConfigurationException e) {
                throw new IOException(e);
            } catch (SyntaxException e) {
                throw new IOException(e);
            }//  ww  w.j  ava 2  s.c o m
        }
    }
    return validators;
}

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

License:Apache License

/** parse the string to a cassandra data type */
protected AbstractType parseType(String type) throws IOException {
    try {//from w ww . j  ava 2 s . c o  m
        // always treat counters like longs, specifically CCT.compose is not what we need
        if (type != null && type.equals("org.apache.cassandra.db.marshal.CounterColumnType"))
            return LongType.instance;
        return TypeParser.parse(type);
    } catch (ConfigurationException e) {
        throw new IOException(e);
    } catch (SyntaxException e) {
        throw new IOException(e);
    }
}

From source file:com.impetus.client.cassandra.CassandraClientBase.java

License:Apache License

/**
 * Compose column value.//from   w  ww  .  j a v  a 2s  . co  m
 * 
 * @param cqlMetadata
 *            the cql metadata
 * @param thriftColumnValue
 *            the thrift column value
 * @param thriftColumnName
 *            the thrift column name
 * @return the object
 */
private Object composeColumnValue(CqlMetadata cqlMetadata, byte[] thriftColumnValue, byte[] thriftColumnName) {
    Map<ByteBuffer, String> schemaTypes = cqlMetadata.getValue_types();
    AbstractType<?> type = null;
    try {
        type = TypeParser.parse(schemaTypes.get(ByteBuffer.wrap(thriftColumnName)));
    } catch (SyntaxException | ConfigurationException ex) {
        log.error(ex.getMessage());
        throw new KunderaException("Error while deserializing column value " + ex);
    }
    if (type.isCollection()) {
        return ((CollectionSerializer) type.getSerializer())
                .deserializeForNativeProtocol(ByteBuffer.wrap(thriftColumnValue), 2);
    }
    return type.compose(ByteBuffer.wrap(thriftColumnValue));
}

From source file:com.impetus.client.cassandra.datahandler.CassandraDataHandlerBase.java

License:Apache License

/**
 * Compose and add./*from   w w  w.  j av  a2  s.  c om*/
 * 
 * @param entity
 *            the entity
 * @param cqlMetadata
 *            the cql metadata
 * @param thriftColumnValue
 *            the thrift column value
 * @param thriftColumnName
 *            the thrift column name
 */
private void composeAndAdd(HashMap entity, CqlMetadata cqlMetadata, Object thriftColumnValue,
        String thriftColumnName) {
    byte[] columnName = thriftColumnName.getBytes();

    Map<ByteBuffer, String> schemaTypes = this.clientBase.getCqlMetadata().getValue_types();
    AbstractType<?> type = null;
    try {
        type = TypeParser.parse(schemaTypes.get(ByteBuffer.wrap((byte[]) columnName)));
    } catch (SyntaxException | ConfigurationException e) {
        log.error(e.getMessage());
        throw new KunderaException("Error while parsing CQL Type " + e);
    }

    entity.put(thriftColumnName, type.compose(ByteBuffer.wrap((byte[]) thriftColumnValue)));
}

From source file:com.knewton.mapreduce.SSTableRecordReader.java

License:Apache License

/**
 * Get an instance of a comparator used for comparing keys in the sstables.
 *
 * @param conf The configuration object//www .  j a va  2 s.  c  o m
 * @return A new instance of the comparator.
 */
private AbstractType<?> getConfComparator(Configuration conf) {
    String comparatorStr = conf.get(PropertyConstants.COLUMN_COMPARATOR.txt);
    Preconditions.checkNotNull(comparatorStr,
            String.format("Property %s not set", PropertyConstants.COLUMN_COMPARATOR.txt));
    try {
        return TypeParser.parse(comparatorStr);
    } catch (SyntaxException | ConfigurationException ce) {
        String msg = String.format("Can't construct comparator from %s.", comparatorStr);
        throw new IllegalArgumentException(msg, ce);
    }
}

From source file:com.knewton.mapreduce.SSTableRecordReader.java

License:Apache License

/**
 * Get an instance of a subcomparator used for comparing keys in the sstables.
 *
 * @param conf The configuration object//from  ww  w. ja  va2s  .  c o  m
 * @return A new instance of the subcomparator.
 */
@Nullable
private AbstractType<?> getConfSubComparator(Configuration conf) {
    String subcomparatorStr = conf.get(PropertyConstants.COLUMN_SUBCOMPARATOR.txt);
    if (subcomparatorStr == null) {
        return null;
    }

    try {
        return TypeParser.parse(subcomparatorStr);
    } catch (SyntaxException | ConfigurationException ce) {
        String msg = String.format("Can't construct subcomparator from %s.", subcomparatorStr);
        throw new IllegalArgumentException(msg, ce);
    }
}

From source file:com.netflix.aegisthus.input.AegisthusInputFormat.java

License:Apache License

@SuppressWarnings("rawtypes")
private Map<String, AbstractType> initConvertors(JobContext job) throws IOException {
    Map<String, AbstractType> convertors = Maps.newHashMap();
    String conversion = job.getConfiguration().get(KEY_TYPE);
    LOG.info(KEY_TYPE + ": " + conversion);
    if (conversion != null) {
        try {/*from  ww w  .j a va 2  s.  co m*/
            convertors.put(SSTableScanner.KEY, TypeParser.parse(conversion));
        } catch (ConfigurationException e) {
            throw new IOException(e);
        } catch (SyntaxException e) {
            throw new IOException(e);
        }
    }
    conversion = job.getConfiguration().get(COLUMN_TYPE);
    LOG.info(COLUMN_TYPE + ": " + conversion);
    if (conversion != null) {
        try {
            convertors.put(SSTableScanner.COLUMN_NAME_KEY, TypeParser.parse(conversion));
        } catch (ConfigurationException e) {
            throw new IOException(e);
        } catch (SyntaxException e) {
            throw new IOException(e);
        }
    }

    if (convertors.size() == 0) {
        return null;
    }
    return convertors;
}

From source file:com.netflix.aegisthus.input.AegSplit.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    end = in.readLong();/*  w  ww.  j  a v  a 2 s.c  o  m*/
    hosts = WritableUtils.readStringArray(in);
    path = new Path(WritableUtils.readString(in));
    compressed = in.readBoolean();
    if (compressed) {
        compressedPath = new Path(WritableUtils.readString(in));
    }
    start = in.readLong();
    type = WritableUtils.readEnum(in, Type.class);
    int size = in.readInt();
    if (type == Type.sstable) {
        convertors = Maps.newHashMap();
        for (int i = 0; i < size; i++) {
            String[] parts = WritableUtils.readStringArray(in);
            try {
                convertors.put(parts[0], TypeParser.parse(parts[1]));
            } catch (ConfigurationException e) {
                throw new IOException(e);
            } catch (SyntaxException e) {
                throw new IOException(e);
            }
        }
    }
}