Search in sources :

Example 1 with Mapper

use of com.datastax.driver.mapping.Mapper in project storm by apache.

the class ObjectMapperCqlStatementMapper method map.

@Override
public List<Statement> map(Map<String, Object> map, Session session, ITuple tuple) {
    final ObjectMapperOperation operation = (ObjectMapperOperation) tuple.getValueByField(operationField);
    Preconditions.checkNotNull(operation, "Operation must not be null");
    final Object value = tuple.getValueByField(valueField);
    final Object timestampObject = timestampField != null ? tuple.getValueByField(timestampField) : null;
    final Object ttlObject = ttlField != null ? tuple.getValueByField(ttlField) : null;
    final ConsistencyLevel consistencyLevel = consistencyLevelField != null ? (ConsistencyLevel) tuple.getValueByField(consistencyLevelField) : null;
    final Class<?> valueClass = value.getClass();
    final Mapper mapper = getMappingManager(session).mapper(valueClass);
    Collection<Option> options = new ArrayList<>();
    if (timestampObject != null) {
        if (timestampObject instanceof Number) {
            options.add(Option.timestamp(((Number) timestampObject).longValue()));
        } else if (timestampObject instanceof Instant) {
            Instant timestamp = (Instant) timestampObject;
            options.add(Option.timestamp(timestamp.getEpochSecond() * 1000_0000L + timestamp.getNano() / 1000L));
        }
    }
    if (ttlObject != null) {
        if (ttlObject instanceof Number) {
            options.add(Option.ttl(((Number) ttlObject).intValue()));
        } else if (ttlObject instanceof Duration) {
            Duration ttl = (Duration) ttlObject;
            options.add(Option.ttl((int) ttl.getSeconds()));
        }
    }
    if (consistencyLevel != null) {
        options.add(Option.consistencyLevel(consistencyLevel));
    }
    if (operation == ObjectMapperOperation.SAVE) {
        options.add(Option.saveNullFields(true));
        return Arrays.asList(mapper.saveQuery(value, options.toArray(new Option[options.size()])));
    } else if (operation == ObjectMapperOperation.SAVE_IGNORE_NULLS) {
        options.add(Option.saveNullFields(false));
        return Arrays.asList(mapper.saveQuery(value, options.toArray(new Option[options.size()])));
    } else if (operation == ObjectMapperOperation.DELETE) {
        return Arrays.asList(mapper.deleteQuery(value, options.toArray(new Option[options.size()])));
    } else {
        throw new UnsupportedOperationException("Unknown operation: " + operation);
    }
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) CQLStatementTupleMapper(org.apache.storm.cassandra.query.CQLStatementTupleMapper) Mapper(com.datastax.driver.mapping.Mapper) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Option(com.datastax.driver.mapping.Mapper.Option) Duration(java.time.Duration) ObjectMapperOperation(org.apache.storm.cassandra.query.ObjectMapperOperation)

Aggregations

ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 Mapper (com.datastax.driver.mapping.Mapper)1 Option (com.datastax.driver.mapping.Mapper.Option)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 CQLStatementTupleMapper (org.apache.storm.cassandra.query.CQLStatementTupleMapper)1 ObjectMapperOperation (org.apache.storm.cassandra.query.ObjectMapperOperation)1