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();
}
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);
}
}
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;
}
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]);
}
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);
}
Aggregations