Search in sources :

Example 11 with Field

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

the class CassandraQueryFactory method processFieldsForCreateTableQuery.

private static void processFieldsForCreateTableQuery(List<Field> fields, boolean isCommaNeeded, StringBuilder stringBuilder) {
    for (Field field : fields) {
        if (isCommaNeeded) {
            stringBuilder.append(", ");
        }
        stringBuilder.append(field.getColumnName()).append(" ").append(field.getType());
        boolean isStaticColumn = Boolean.parseBoolean(field.getProperty("static"));
        boolean isPrimaryKey = Boolean.parseBoolean(field.getProperty("primarykey"));
        if (isStaticColumn) {
            stringBuilder.append(" STATIC");
        }
        if (isPrimaryKey) {
            stringBuilder.append("  PRIMARY KEY ");
        }
        isCommaNeeded = true;
    }
}
Also used : Field(org.apache.gora.cassandra.bean.Field) ClusterKeyField(org.apache.gora.cassandra.bean.ClusterKeyField) PartitionKeyField(org.apache.gora.cassandra.bean.PartitionKeyField)

Example 12 with Field

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

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

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

the class NativeSerializer method getKey.

private K getKey(T object) {
    String keyField = null;
    for (Field field : mapping.getFieldList()) {
        boolean isPrimaryKey = Boolean.parseBoolean(field.getProperty("primarykey"));
        if (isPrimaryKey) {
            keyField = field.getFieldName();
            break;
        }
    }
    K key;
    Method keyMethod = null;
    try {
        for (Method method : this.persistentClass.getMethods()) {
            if (method.getName().equalsIgnoreCase("get" + keyField)) {
                keyMethod = method;
            }
        }
        key = (K) keyMethod.invoke(object);
    } catch (Exception e) {
        try {
            key = (K) this.persistentClass.getField(keyField).get(object);
        } catch (Exception e1) {
            throw new RuntimeException("Field" + keyField + " is not accessible in " + persistentClass + " : " + e1.getMessage());
        }
    }
    return key;
}
Also used : Field(org.apache.gora.cassandra.bean.Field) Method(java.lang.reflect.Method) GoraException(org.apache.gora.util.GoraException)

Example 15 with Field

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

the class NativeSerializer method analyzePersistent.

/**
 * {@inheritDoc}
 *
 * @throws Exception
 */
@Override
protected void analyzePersistent() throws Exception {
    userDefineTypeMaps = new HashMap<>();
    for (Field field : mapping.getFieldList()) {
        String fieldType = field.getType();
        if (fieldType.contains("frozen")) {
            String udtType = fieldType.substring(fieldType.indexOf("<") + 1, fieldType.indexOf(">"));
            String createQuery = CassandraQueryFactory.getCreateUDTTypeForNative(mapping, persistentClass, udtType, field.getFieldName());
            userDefineTypeMaps.put(udtType, createQuery);
        }
    }
}
Also used : Field(org.apache.gora.cassandra.bean.Field)

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