Search in sources :

Example 26 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class HiveStore method deleteByQuery.

/**
 * This deletes all the matching records from hive store. But, delete queries do not support in
 * all hive servers and they require some specific server configurations.
 *
 * @param query matching records to this query will be deleted.
 * @return number of deleted entires. -1 if all entries were deleted
 */
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    if (query.getKey() == null && query.getStartKey() == null && query.getEndKey() == null) {
        deleteSchema();
        createSchema();
        return -1;
    } else {
        try {
            int deleteCount = 0;
            Result<K, T> result = query.execute();
            while (result.next()) {
                if (this.delete(result.getKey())) {
                    deleteCount++;
                }
            }
            return deleteCount;
        } catch (Exception e) {
            throw new GoraException(e);
        }
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SQLException(java.sql.SQLException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 27 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class HiveQueryBuilder method serializeUnion.

/**
 * Serialize a given object based on union types. If the union type has only two values and one of
 * them is null type, the schema is considered as a type of the not null type. However if there
 * are more than one not-null types, the value should be defined as create_union(position,
 * serialized_value). Ex : if the field is UNION<String, Float>, the query should be
 * "create_union(1, 'value')". the position is counted using only not-null types.
 *
 * @param parameterList carries the list of parameters to be injected into sql
 * @param schema the union schema
 * @param value the value object
 * @return serialized value
 * @throws GoraException throw if serialization is failed.
 */
private Object serializeUnion(List<Object> parameterList, Schema schema, Object value) throws GoraException {
    List<Schema> schemas = schema.getTypes();
    if (schemas.size() == 2 && isNullable(schema)) {
        return serializeValue(parameterList, getValidSchema(schema), value);
    }
    StringBuilder valueBuilder = new StringBuilder();
    int count = 1;
    for (Schema valueSchema : schemas) {
        if (!Type.NULL.equals(valueSchema.getType())) {
            if (isValidType(valueSchema.getType(), value)) {
                valueBuilder.append("create_union(").append(count).append(COMMA_SYMBOL).append(SPACE_SYMBOL).append(serializeValue(parameterList, valueSchema, value)).append(CLOSE_BRACKET_SYMBOL);
                return valueBuilder.toString();
            }
            count++;
        }
    }
    throw new GoraException("Serializing union value is failed");
}
Also used : GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema)

Example 28 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class HiveQueryBuilder method buildCreateQuery.

/**
 * Create hive sql query to create the schema table
 *
 * @param databaseName hive database name
 * @param fieldMap map of avro fields to their field names
 * @return hive sql query
 * @throws GoraException throw if the generation of the sql is failed
 */
public String buildCreateQuery(String databaseName, Map<String, Field> fieldMap) throws GoraException {
    // Initiate create query
    StringBuilder createQuery = new StringBuilder();
    createQuery.append("create table if not exists ").append(databaseName).append(PERIOD_SYMBOL).append(hiveStore.getSchemaName()).append(OPEN_BRACKET_SYMBOL);
    // Create an Avro schema including fields only in mappings
    Schema avroSchema = createAvroSchema(fieldMap);
    try {
        buildColumnType(createQuery, avroSchema);
    } catch (SerDeException e) {
        throw new GoraException("Schema inspection has been failed.", e);
    }
    createQuery.append(CLOSE_BRACKET_SYMBOL);
    return createQuery.toString();
}
Also used : GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 29 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class HiveQueryBuilder method createAvroSchema.

/**
 * Create an avro schema including keys and fields of the mapping.
 *
 * @param fieldMap Avro field map which maps each field to its name
 * @return Genereated avro schema
 * @throws GoraException throws if the schema generation is failed
 */
private Schema createAvroSchema(Map<String, Field> fieldMap) throws GoraException {
    Class<?> persistentClass = hiveStore.getPersistentClass();
    Schema avroSchema = Schema.createRecord(persistentClass.getSimpleName(), null, persistentClass.getPackage().getName(), false);
    List<Field> avroFieldList = new ArrayList<>();
    avroFieldList.add(new Field(HiveMapping.DEFAULT_KEY_NAME, getKeySchema(), null, 1));
    List<String> fieldNames = hiveMapping.getFields();
    for (String fieldName : fieldNames) {
        Field field = fieldMap.get(fieldName);
        if (field == null) {
            throw new GoraException("Could not find a avro field for field name : " + fieldName);
        }
        avroFieldList.add(new Field(field.name(), field.schema(), field.doc(), field.defaultVal()));
    }
    avroSchema.setFields(avroFieldList);
    return avroSchema;
}
Also used : Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList)

Example 30 with GoraException

use of org.apache.gora.util.GoraException in project gora by apache.

the class HiveQueryBuilder method serializeRecord.

/**
 * A record is handled as a struct in hive context and the sql query for structs are define as
 * named_struct(field1, value1, field2, value2,..). Moreover nested structs are not allowed to be
 * null and they have to be empty structs which all fields represent respective null values.
 *
 * @param parameterList carries the list of parameters to be injected into sql
 * @param schema the record schema
 * @param value the record object
 * @return serialized value
 * @throws GoraException throw if serialization is failed.
 */
private Object serializeRecord(List<Object> parameterList, Schema schema, Object value) throws GoraException {
    if (value == null) {
        return getNullValue(parameterList, schema);
    } else if (!(value instanceof PersistentBase)) {
        throw new GoraException("Record value is not a persistent object");
    }
    PersistentBase record = (PersistentBase) value;
    StringBuilder valueBuilder = new StringBuilder();
    valueBuilder.append("named_struct(");
    List<Field> fields = schema.getFields();
    for (int i = 0; i < fields.size(); i++) {
        Field field = fields.get(i);
        valueBuilder.append(QUOTE_SYMBOL).append(field.name()).append(QUOTE_SYMBOL).append(COMMA_SYMBOL).append(SPACE_SYMBOL);
        valueBuilder.append(serializeValue(parameterList, field.schema(), record.get(field.pos())));
        if (i < fields.size() - 1) {
            valueBuilder.append(",");
        }
    }
    return valueBuilder.append(CLOSE_BRACKET_SYMBOL).toString();
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) GoraException(org.apache.gora.util.GoraException)

Aggregations

GoraException (org.apache.gora.util.GoraException)174 IOException (java.io.IOException)119 ArrayList (java.util.ArrayList)30 Schema (org.apache.avro.Schema)25 SQLException (java.sql.SQLException)17 HashMap (java.util.HashMap)17 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 PreparedStatement (java.sql.PreparedStatement)11 Map (java.util.Map)11 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)9 SolrServerException (org.apache.solr.client.solrj.SolrServerException)9 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)8 AccumuloException (org.apache.accumulo.core.client.AccumuloException)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)8 Field (org.apache.avro.Schema.Field)8 KuduException (org.apache.kudu.client.KuduException)8 ResultSet (com.datastax.driver.core.ResultSet)7 SimpleStatement (com.datastax.driver.core.SimpleStatement)7 SAXBuilder (org.jdom.input.SAXBuilder)7 InputStream (java.io.InputStream)6