Search in sources :

Example 1 with ClusterKeyField

use of org.apache.gora.cassandra.bean.ClusterKeyField 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 ClusterKeyField

use of org.apache.gora.cassandra.bean.ClusterKeyField 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)2 ClusterKeyField (org.apache.gora.cassandra.bean.ClusterKeyField)2 PartitionKeyField (org.apache.gora.cassandra.bean.PartitionKeyField)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Field (org.apache.gora.cassandra.bean.Field)1 Attribute (org.jdom.Attribute)1 Element (org.jdom.Element)1