Search in sources :

Example 1 with GoraException

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

the class AerospikeStore method put.

/**
 * Method to insert the persistent objects with the given key to the aerospike database server.
 * In writing the records, the policy defined in the mapping file is used to decide on the
 * behaviour of transaction handling.
 *
 * @param key        key of the object
 * @param persistent object to be persisted
 */
@Override
public void put(K key, T persistent) throws GoraException {
    try {
        Key recordKey = getAerospikeKey(key);
        List<Field> fields = persistent.getSchema().getFields();
        for (int i = 0; i < fields.size(); i++) {
            if (!persistent.isDirty(i)) {
                continue;
            }
            Object persistentValue = persistent.get(i);
            String mappingBinName = aerospikeParameters.getAerospikeMapping().getBinMapping().get(fields.get(i).name());
            if (mappingBinName == null) {
                LOG.error("Aerospike mapping for field {}#{} not found. Wrong gora-aerospike-mapping.xml?", persistent.getClass().getName(), fields.get(i).name());
                throw new RuntimeException("Aerospike mapping for field [" + persistent.getClass().getName() + "#" + fields.get(i).name() + "] not found. Wrong gora-aerospike-mapping.xml?");
            }
            Bin bin;
            if (persistentValue != null) {
                bin = new Bin(mappingBinName, getSerializableValue(persistentValue, fields.get(i).schema()));
            } else {
                bin = Bin.asNull(mappingBinName);
            }
            aerospikeClient.put(aerospikeParameters.getAerospikeMapping().getWritePolicy(), recordKey, bin);
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 2 with GoraException

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

the class AerospikeStore method initialize.

/**
 * {@inheritDoc}
 * In initializing the aerospike datastore, read the mapping file, sets the basic
 * aerospike specific parameters and creates the client with the user defined policies
 *
 * @param keyClass        key class
 * @param persistentClass persistent class
 * @param properties      properties
 */
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    super.initialize(keyClass, persistentClass, properties);
    try {
        AerospikeMappingBuilder aerospikeMappingBuilder = new AerospikeMappingBuilder();
        aerospikeMappingBuilder.readMappingFile(getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE), keyClass, persistentClass);
        aerospikeParameters = new AerospikeParameters(aerospikeMappingBuilder.getAerospikeMapping(), properties, getConf());
        ClientPolicy policy = new ClientPolicy();
        policy.writePolicyDefault = aerospikeParameters.getAerospikeMapping().getWritePolicy();
        policy.readPolicyDefault = aerospikeParameters.getAerospikeMapping().getReadPolicy();
        // 'SendKey' property is enabled by default as the key is needed in query execution
        policy.readPolicyDefault.sendKey = true;
        policy.writePolicyDefault.sendKey = true;
        // Set the credentials for servers with restricted access
        if (aerospikeParameters.getUsername() != null) {
            policy.user = aerospikeParameters.getUsername();
        }
        if (aerospikeParameters.getPassword() != null) {
            policy.password = aerospikeParameters.getPassword();
        }
        aerospikeClient = new AerospikeClient(policy, aerospikeParameters.getHost(), aerospikeParameters.getPort());
        aerospikeParameters.setServerSpecificParameters(aerospikeClient);
        aerospikeParameters.validateServerBinConfiguration(persistentClass.getFields());
        LOG.info("Aerospike Gora datastore initialized successfully.");
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : AerospikeClient(com.aerospike.client.AerospikeClient) ClientPolicy(com.aerospike.client.policy.ClientPolicy) GoraException(org.apache.gora.util.GoraException) AerospikeException(com.aerospike.client.AerospikeException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 3 with GoraException

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

the class AerospikeStore method get.

/**
 * {@inheritDoc}
 *
 * @param key    the key of the object
 * @param fields the fields required in the object. Pass null, to retrieve all fields
 * @return the Object corresponding to the key or null if it cannot be found
 */
@Override
public T get(K key, String[] fields) throws GoraException {
    try {
        Key recordKey = getAerospikeKey(key);
        fields = getFieldsToQuery(fields);
        Record record = aerospikeClient.get(aerospikeParameters.getAerospikeMapping().getReadPolicy(), recordKey, fields);
        if (record == null) {
            return null;
        }
        return createPersistentInstance(record, fields);
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) Record(com.aerospike.client.Record) AerospikeResultRecord(org.apache.gora.aerospike.query.AerospikeResultRecord) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 4 with GoraException

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

the class GoraClientTest method testCorrectness.

@Test
public void testCorrectness() {
    Status result = benchmarkClient.insert(Constants.TEST_TABLE, Constants.TEST_KEY_4, INTEGER_DATA);
    assertEquals(result, Status.OK);
    try {
        User user = readRecord(Constants.TEST_KEY_4);
        assertEquals(190, sum(user));
    } catch (GoraException e) {
        LOG.info("There is a problem reading record from the datastore", e.getMessage(), e);
    }
}
Also used : Status(site.ycsb.Status) GoraException(org.apache.gora.util.GoraException) User(org.apache.gora.benchmark.generated.User) Test(org.junit.Test)

Example 5 with GoraException

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

the class AccumuloStore method fromBytes.

public Object fromBytes(Schema schema, byte[] data) throws IOException {
    Schema fromSchema = null;
    if (schema.getType() == Type.UNION) {
        try {
            Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
            int unionIndex = decoder.readIndex();
            List<Schema> possibleTypes = schema.getTypes();
            fromSchema = possibleTypes.get(unionIndex);
            Schema effectiveSchema = possibleTypes.get(unionIndex);
            if (effectiveSchema.getType() == Type.NULL) {
                decoder.readNull();
                return null;
            } else {
                data = decoder.readBytes(null).array();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage());
            throw new GoraException("Error decoding union type: ", e);
        }
    } else {
        fromSchema = schema;
    }
    return fromBytes(encoder, fromSchema, data);
}
Also used : GoraException(org.apache.gora.util.GoraException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

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