Search in sources :

Example 1 with CassandraKey

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

the class CassandraQueryFactory method getCreateTableQuery.

/**
 * This method returns the CQL query to table.
 * refer : http://docs.datastax.com/en/cql/3.1/cql/cql_reference/create_table_r.html
 * <p>
 * Trick : To have a consistency of the order of the columns, first we append partition keys, second cluster keys and finally other columns.
 * It's very much needed to follow the same order in other CRUD operations as well.
 *
 * @param mapping Cassandra mapping {@link CassandraMapping}
 * @return CQL Query
 */
static String getCreateTableQuery(CassandraMapping mapping) {
    StringBuilder stringBuffer = new StringBuilder();
    stringBuffer.append("CREATE TABLE IF NOT EXISTS ").append(mapping.getKeySpace().getName()).append(".").append(mapping.getCoreName()).append(" (");
    CassandraKey cassandraKey = mapping.getCassandraKey();
    // appending Cassandra Persistent columns into db schema
    processFieldsForCreateTableQuery(mapping.getFieldList(), false, stringBuffer);
    if (cassandraKey != null) {
        processFieldsForCreateTableQuery(cassandraKey.getFieldList(), true, stringBuffer);
        List<PartitionKeyField> partitionKeys = cassandraKey.getPartitionKeyFields();
        if (partitionKeys != null) {
            stringBuffer.append(", PRIMARY KEY (");
            boolean isCommaNeededToApply = false;
            for (PartitionKeyField keyField : partitionKeys) {
                if (isCommaNeededToApply) {
                    stringBuffer.append(",");
                }
                if (keyField.isComposite()) {
                    stringBuffer.append("(");
                    boolean isCommaNeededHere = false;
                    for (Field field : keyField.getFields()) {
                        if (isCommaNeededHere) {
                            stringBuffer.append(", ");
                        }
                        stringBuffer.append(field.getColumnName());
                        isCommaNeededHere = true;
                    }
                    stringBuffer.append(")");
                } else {
                    stringBuffer.append(keyField.getColumnName());
                }
                isCommaNeededToApply = true;
            }
            stringBuffer.append(")");
        }
    }
    stringBuffer.append(")");
    boolean isWithNeeded = true;
    if (Boolean.parseBoolean(mapping.getProperty("compactStorage"))) {
        stringBuffer.append(" WITH COMPACT STORAGE ");
        isWithNeeded = false;
    }
    String id = mapping.getProperty("id");
    if (id != null) {
        if (isWithNeeded) {
            stringBuffer.append(" WITH ");
        } else {
            stringBuffer.append(" AND ");
        }
        stringBuffer.append("ID = '").append(id).append("'");
        isWithNeeded = false;
    }
    if (cassandraKey != null) {
        List<ClusterKeyField> clusterKeyFields = cassandraKey.getClusterKeyFields();
        if (clusterKeyFields != null) {
            if (isWithNeeded) {
                stringBuffer.append(" WITH ");
            } else {
                stringBuffer.append(" AND ");
            }
            stringBuffer.append(" CLUSTERING ORDER BY (");
            boolean isCommaNeededToApply = false;
            for (ClusterKeyField keyField : clusterKeyFields) {
                if (isCommaNeededToApply) {
                    stringBuffer.append(", ");
                }
                stringBuffer.append(keyField.getColumnName()).append(" ");
                if (keyField.getOrder() != null) {
                    stringBuffer.append(keyField.getOrder());
                }
                isCommaNeededToApply = true;
            }
            stringBuffer.append(")");
        }
    }
    return stringBuffer.toString();
}
Also used : Field(org.apache.gora.cassandra.bean.Field) ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField) ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey)

Example 2 with CassandraKey

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

the class AvroCassandraUtils method processKeys.

static void processKeys(CassandraMapping cassandraMapping, Object key, List<String> keys, List<Object> values) {
    CassandraKey cassandraKey = cassandraMapping.getCassandraKey();
    if (cassandraKey != null) {
        if (key instanceof PersistentBase) {
            PersistentBase keyBase = (PersistentBase) key;
            for (Schema.Field field : keyBase.getSchema().getFields()) {
                Field mappedField = cassandraKey.getFieldFromFieldName(field.name());
                if (mappedField != null) {
                    keys.add(field.name());
                    Object value = keyBase.get(field.pos());
                    value = getFieldValueFromAvroBean(field.schema(), field.schema().getType(), value, mappedField);
                    values.add(value);
                } else {
                    LOG.debug("Ignoring field {}, Since field couldn't find in the {} mapping", new Object[] { field.name(), cassandraMapping.getPersistentClass() });
                }
            }
        } else {
            LOG.error("Key bean isn't extended by {} .", new Object[] { cassandraMapping.getKeyClass(), PersistentBase.class });
        }
    } else {
        keys.add(cassandraMapping.getInlinedDefinedPartitionKey().getFieldName());
        values.add(key);
    }
}
Also used : Field(org.apache.gora.cassandra.bean.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey)

Example 3 with CassandraKey

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

the class CassandraQueryFactory method getSelectObjectWithFieldsQuery.

/**
 * This method returns CQL Select query to retrieve data from the table with given fields.
 * This method is used for Native Serialization
 * refer: http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlSelect.html
 *
 * @param mapping Cassandra Mapping {@link CassandraMapping}
 * @param fields  Given fields to retrieve
 * @return CQL Query
 */
static String getSelectObjectWithFieldsQuery(CassandraMapping mapping, String[] fields) {
    String cqlQuery = null;
    String[] columnNames = getColumnNames(mapping, Arrays.asList(fields));
    Select select = QueryBuilder.select(columnNames).from(mapping.getKeySpace().getName(), mapping.getCoreName());
    if (Boolean.parseBoolean(mapping.getProperty("allowFiltering"))) {
        select.allowFiltering();
    }
    CassandraKey cKey = mapping.getCassandraKey();
    if (cKey != null) {
        Select.Where query = null;
        boolean isWhereNeeded = true;
        for (Field field : cKey.getFieldList()) {
            if (isWhereNeeded) {
                query = select.where(QueryBuilder.eq(field.getColumnName(), "?"));
                isWhereNeeded = false;
            } else {
                query = query.and(QueryBuilder.eq(field.getColumnName(), "?"));
            }
        }
        cqlQuery = query != null ? query.getQueryString() : null;
    } else {
        for (Field field : mapping.getFieldList()) {
            boolean isPrimaryKey = Boolean.parseBoolean(field.getProperty("primarykey"));
            if (isPrimaryKey) {
                cqlQuery = select.where(QueryBuilder.eq(field.getColumnName(), "?")).getQueryString();
                break;
            }
        }
    }
    return cqlQuery;
}
Also used : Field(org.apache.gora.cassandra.bean.Field) ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField) Select(com.datastax.driver.core.querybuilder.Select) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey)

Example 4 with CassandraKey

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

the class CassandraQueryFactory method getColumnNames.

private static String[] getColumnNames(CassandraMapping mapping, List<String> fields) {
    ArrayList<String> columnNames = new ArrayList<>();
    for (String field : fields) {
        Field fieldBean = mapping.getFieldFromFieldName(field);
        CassandraKey cassandraKey = mapping.getCassandraKey();
        Field keyBean = null;
        if (cassandraKey != null) {
            keyBean = cassandraKey.getFieldFromFieldName(field);
        }
        if (fieldBean != null) {
            columnNames.add(fieldBean.getColumnName());
        } else if (keyBean != null) {
            columnNames.add(keyBean.getColumnName());
        } else {
            LOG.warn("{} field is ignored, couldn't find relevant field in the persistent mapping", field);
        }
    }
    return columnNames.toArray(new String[0]);
}
Also used : Field(org.apache.gora.cassandra.bean.Field) ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField) ArrayList(java.util.ArrayList) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey)

Example 5 with CassandraKey

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

the class CassandraMappingBuilder method processCassandraKeys.

private void processCassandraKeys(CassandraMapping cassandraMapping, Element key, String keyName) {
    CassandraKey cassandraKey = new CassandraKey(keyName);
    Element partitionKeys = key.getChild("partitionKey");
    Element clusterKeys = key.getChild("clusterKey");
    List<Element> partitionKeyFields = partitionKeys.getChildren("field");
    List<Element> partitionCompositeKeyFields = partitionKeys.getChildren("compositeKey");
    // process non composite partition keys
    for (Element partitionKeyField : partitionKeyFields) {
        PartitionKeyField fieldKey = new PartitionKeyField();
        List fieldAttributes = partitionKeyField.getAttributes();
        processAttributes(fieldAttributes, fieldKey);
        cassandraKey.addPartitionKeyField(fieldKey);
    }
    // process composite partitions keys
    for (Element partitionCompositeKeyField : partitionCompositeKeyFields) {
        PartitionKeyField compositeFieldKey = new PartitionKeyField();
        compositeFieldKey.setComposite(true);
        List<Element> compositeKeyFields = partitionCompositeKeyField.getChildren("field");
        for (Element partitionKeyField : compositeKeyFields) {
            PartitionKeyField fieldKey = new PartitionKeyField();
            List fieldAttributes = partitionKeyField.getAttributes();
            processAttributes(fieldAttributes, fieldKey);
            compositeFieldKey.addField(fieldKey);
        }
        cassandraKey.addPartitionKeyField(compositeFieldKey);
    }
    // process cluster keys
    List<Element> clusterKeyFields = clusterKeys.getChildren("key");
    for (Element clusterKeyField : clusterKeyFields) {
        ClusterKeyField keyField = new ClusterKeyField();
        List fieldAttributes = clusterKeyField.getAttributes();
        for (Object anAttributeList : fieldAttributes) {
            Attribute attribute = (Attribute) anAttributeList;
            String attributeName = attribute.getName();
            String attributeValue = attribute.getValue();
            switch(attributeName) {
                case "column":
                    keyField.setColumnName(attributeValue);
                    break;
                case "order":
                    keyField.setOrder(ClusterKeyField.Order.valueOf(attributeValue.toUpperCase(Locale.ENGLISH)));
                    break;
                default:
                    LOG.warn("{} attribute is Unsupported or Invalid, in {} Cassandra Key. Please configure the cassandra mapping correctly.", new Object[] { attributeName, keyName });
                    break;
            }
        }
        cassandraKey.addClusterKeyField(keyField);
    }
    cassandraMapping.setCassandraKey(cassandraKey);
}
Also used : ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) Attribute(org.jdom.Attribute) Element(org.jdom.Element) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField) ArrayList(java.util.ArrayList) List(java.util.List) CassandraKey(org.apache.gora.cassandra.bean.CassandraKey)

Aggregations

CassandraKey (org.apache.gora.cassandra.bean.CassandraKey)6 Field (org.apache.gora.cassandra.bean.Field)5 ClusterKeyField (org.apache.gora.cassandra.bean.ClusterKeyField)4 PartitionKeyField (org.apache.gora.cassandra.bean.PartitionKeyField)4 ArrayList (java.util.ArrayList)3 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 SimpleStatement (com.datastax.driver.core.SimpleStatement)1 Select (com.datastax.driver.core.querybuilder.Select)1 List (java.util.List)1 Schema (org.apache.avro.Schema)1 CassandraResultSet (org.apache.gora.cassandra.query.CassandraResultSet)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1 GoraException (org.apache.gora.util.GoraException)1 Attribute (org.jdom.Attribute)1 Element (org.jdom.Element)1