Search in sources :

Example 16 with Field

use of org.apache.gora.cassandra.bean.Field in project gora by apache.

the class AvroSerializer method execute.

/**
 * {@inheritDoc}
 *
 * @param dataStore
 * @param query
 * @return
 */
@Override
public Result execute(DataStore dataStore, Query query) throws GoraException {
    try {
        List<Object> objectArrayList = new ArrayList<>();
        String[] fields = query.getFields();
        if (fields != null) {
            fields = (String[]) ArrayUtils.addAll(fields, mapping.getAllKeys());
        } else {
            fields = mapping.getAllFieldsIncludingKeys();
        }
        CassandraResultSet<K, T> cassandraResult = new CassandraResultSet<>(dataStore, query);
        String cqlQuery = CassandraQueryFactory.getExecuteQuery(mapping, query, objectArrayList, fields);
        ResultSet results;
        SimpleStatement statement;
        if (objectArrayList.size() == 0) {
            statement = new SimpleStatement(cqlQuery);
        } else {
            statement = new SimpleStatement(cqlQuery, objectArrayList.toArray());
        }
        if (readConsistencyLevel != null) {
            statement.setConsistencyLevel(ConsistencyLevel.valueOf(readConsistencyLevel));
        }
        results = client.getSession().execute(statement);
        Iterator<Row> iterator = results.iterator();
        ColumnDefinitions definitions = results.getColumnDefinitions();
        T obj;
        K keyObject;
        CassandraKey cassandraKey = mapping.getCassandraKey();
        while (iterator.hasNext()) {
            AbstractGettableData row = (AbstractGettableData) iterator.next();
            obj = cassandraDataStore.newPersistent();
            keyObject = cassandraDataStore.newKey();
            populateValuesToPersistent(row, definitions, obj, fields);
            if (cassandraKey != null) {
                populateValuesToPersistent(row, definitions, (PersistentBase) keyObject, cassandraKey.getFieldNames());
            } else {
                Field key = mapping.getInlinedDefinedPartitionKey();
                keyObject = (K) getValue(row, definitions.getType(key.getColumnName()), key.getColumnName(), null);
            }
            cassandraResult.addResultElement(keyObject, obj);
        }
        return cassandraResult;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) AbstractGettableData(com.datastax.driver.core.AbstractGettableData) SimpleStatement(com.datastax.driver.core.SimpleStatement) ArrayList(java.util.ArrayList) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey) GoraException(org.apache.gora.util.GoraException) Field(org.apache.gora.cassandra.bean.Field) GoraException(org.apache.gora.util.GoraException) ResultSet(com.datastax.driver.core.ResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet) CassandraResultSet(org.apache.gora.cassandra.query.CassandraResultSet) Row(com.datastax.driver.core.Row)

Example 17 with Field

use of org.apache.gora.cassandra.bean.Field in project gora by apache.

the class AvroSerializer method populateValuesToPersistent.

/**
 * This method wraps result set data in to DataEntry and creates a list of DataEntry.
 */
private void populateValuesToPersistent(AbstractGettableData row, ColumnDefinitions columnDefinitions, PersistentBase base, String[] fields) {
    Object paramValue;
    for (String fieldName : fields) {
        Schema.Field avroField = base.getSchema().getField(fieldName);
        Field field = mapping.getFieldFromFieldName(fieldName);
        // to ignore unspecified fields in the mapping
        if (field == null || avroField == null) {
            continue;
        }
        Schema fieldSchema = avroField.schema();
        String columnName = field.getColumnName();
        paramValue = getValue(row, columnDefinitions.getType(columnName), columnName, fieldSchema);
        Object value = AvroCassandraUtils.getAvroFieldValue(paramValue, fieldSchema);
        base.put(avroField.pos(), value);
    }
}
Also used : Field(org.apache.gora.cassandra.bean.Field) Schema(org.apache.avro.Schema)

Example 18 with Field

use of org.apache.gora.cassandra.bean.Field in project gora by apache.

the class CassandraMapping method getAllKeys.

/**
 * This method return all the fields which involves with partition keys, Including composite Keys
 * @return field Names
 */
public String[] getAllKeys() {
    List<String> fieldNames = new ArrayList<>();
    Field keyField = getInlinedDefinedPartitionKey();
    if (cassandraKey != null) {
        for (Field field : cassandraKey.getFieldList()) {
            fieldNames.add(field.getFieldName());
        }
    } else {
        fieldNames.add(keyField.getFieldName());
    }
    String[] fieldNameArray = new String[fieldNames.size()];
    return fieldNames.toArray(fieldNameArray);
}
Also used : Field(org.apache.gora.cassandra.bean.Field) ArrayList(java.util.ArrayList)

Example 19 with Field

use of org.apache.gora.cassandra.bean.Field in project gora by apache.

the class CassandraMappingBuilder method processClass.

private void processClass(CassandraMapping cassandraMapping, Element classElement) {
    String tableName = classElement.getAttributeValue("table");
    cassandraMapping.setCoreName(tableName);
    List classAttributes = classElement.getAttributes();
    for (Object anAttributeList : classAttributes) {
        Attribute attribute = (Attribute) anAttributeList;
        String attributeName = attribute.getName();
        String attributeValue = attribute.getValue();
        cassandraMapping.addProperty(attributeName, attributeValue);
    }
    List<Element> fields = classElement.getChildren("field");
    for (Element field : fields) {
        Field cassandraField = new Field();
        List fieldAttributes = field.getAttributes();
        processAttributes(fieldAttributes, cassandraField);
        cassandraMapping.addCassandraField(cassandraField);
    }
}
Also used : Field(org.apache.gora.cassandra.bean.Field) ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField) Attribute(org.jdom.Attribute) Element(org.jdom.Element) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Field (org.apache.gora.cassandra.bean.Field)19 ArrayList (java.util.ArrayList)7 ClusterKeyField (org.apache.gora.cassandra.bean.ClusterKeyField)6 PartitionKeyField (org.apache.gora.cassandra.bean.PartitionKeyField)6 Schema (org.apache.avro.Schema)5 CassandraKey (org.apache.gora.cassandra.bean.CassandraKey)5 GoraException (org.apache.gora.util.GoraException)3 SimpleStatement (com.datastax.driver.core.SimpleStatement)2 List (java.util.List)2 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)2 AbstractGettableData (com.datastax.driver.core.AbstractGettableData)1 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)1 ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 UDTValue (com.datastax.driver.core.UDTValue)1 UserType (com.datastax.driver.core.UserType)1 Select (com.datastax.driver.core.querybuilder.Select)1 Update (com.datastax.driver.core.querybuilder.Update)1 DoubleArrayCodec (com.datastax.driver.extras.codecs.arrays.DoubleArrayCodec)1 FloatArrayCodec (com.datastax.driver.extras.codecs.arrays.FloatArrayCodec)1