Search in sources :

Example 51 with GoraException

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

the class DynamoDBNativeStore method get.

@Override
public /**
 * Gets the object with the specific key
 * @throws IOException
 */
T get(K key) throws GoraException {
    T object = null;
    try {
        Object rangeKey;
        rangeKey = getRangeKeyFromKey(key);
        Object hashKey = getHashFromKey(key);
        if (hashKey != null) {
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
            if (rangeKey != null)
                object = mapper.load(persistentClass, hashKey, rangeKey);
            else
                object = mapper.load(persistentClass, hashKey);
            return object;
        } else {
            throw new GoraException("Error while retrieving keys from object: " + key.toString());
        }
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 52 with GoraException

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

the class DynamoDBNativeStore method deleteByQuery.

/**
 * Deletes items using a specific query
 *
 * @param query matching records to this query will be deleted
 * @return
 */
@Override
@SuppressWarnings("unchecked")
public long deleteByQuery(Query<K, T> query) throws GoraException {
    // TODO verify whether or not we are deleting a whole row
    // String[] fields = getFieldsToQuery(query.getFields());
    // find whether all fields are queried, which means that complete
    // rows will be deleted
    // boolean isAllFields = Arrays.equals(fields
    // , getBeanFactory().getCachedPersistent().getFields());
    ArrayList<T> deletes = null;
    try {
        Result<K, T> result = execute(query);
        deletes = new ArrayList<T>();
        while (result.next()) {
            T resultObj = result.get();
            deletes.add(resultObj);
            @SuppressWarnings("rawtypes") DynamoDBKey dKey = new DynamoDBKey();
            dKey.setHashKey(getHashFromObj(resultObj));
            dKey.setRangeKey(getRangeKeyFromObj(resultObj));
            delete((K) dKey);
        }
    } catch (GoraException e) {
        // If it is a GoraException we assume it is already logged
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
    return deletes.size();
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 53 with GoraException

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

the class DynamoDBNativeStore method put.

/**
 * Puts an object identified by a key
 *
 * @param key
 * @param obj
 */
@Override
public void put(K key, T obj) throws GoraException {
    try {
        Object hashKey = getHashKey(key, obj);
        Object rangeKey = getRangeKey(key, obj);
        if (hashKey != null) {
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
            if (rangeKey != null) {
                mapper.load(persistentClass, hashKey, rangeKey);
            } else {
                mapper.load(persistentClass, hashKey);
            }
            mapper.save(obj);
        } else
            throw new GoraException("No HashKey found in Key nor in Object.");
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 54 with GoraException

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

the class DynamoDBStore method readMapping.

/**
 * Reads the schema file and converts it into a data structure to be used
 *
 * @return DynamoDBMapping Object containing all necessary information to
 *         create tables
 * @throws IOException
 */
@SuppressWarnings("unchecked")
private DynamoDBMapping readMapping(String filename) throws IOException {
    DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(getClass().getClassLoader().getResourceAsStream(filename));
        if (doc == null || doc.getRootElement() == null)
            throw new GoraException("Unable to load " + MAPPING_FILE + ". Please check its existance!");
        Element root = doc.getRootElement();
        List<Element> tableElements = root.getChildren("table");
        boolean keys = false;
        for (Element tableElement : tableElements) {
            String tableName = tableElement.getAttributeValue("name");
            long readCapacUnits = Long.parseLong(tableElement.getAttributeValue("readcunit"));
            long writeCapacUnits = Long.parseLong(tableElement.getAttributeValue("writecunit"));
            mappingBuilder.setProvisionedThroughput(tableName, readCapacUnits, writeCapacUnits);
            LOG.debug("Basic table properties have been set: Name, and Provisioned throughput.");
            // Retrieving attributes
            List<Element> fieldElements = tableElement.getChildren("attribute");
            for (Element fieldElement : fieldElements) {
                String key = fieldElement.getAttributeValue("key");
                String attributeName = fieldElement.getAttributeValue("name");
                String attributeType = fieldElement.getAttributeValue("type");
                mappingBuilder.addAttribute(tableName, attributeName, attributeType);
                // Retrieving key's features
                if (key != null) {
                    mappingBuilder.setKeySchema(tableName, attributeName, key);
                    keys = true;
                }
            }
            LOG.debug("Attributes for table '" + tableName + "' have been read.");
            if (!keys)
                LOG.warn("Keys for table '" + tableName + "' have NOT been set.");
        }
    } catch (IOException ex) {
        LOG.error("Error while performing xml mapping.", ex.getMessage());
        throw new IOException(ex);
    } catch (Exception ex) {
        LOG.error("Error while performing xml mapping.", ex.getMessage());
        throw new RuntimeException(ex);
    }
    return mappingBuilder.build();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) GoraException(org.apache.gora.util.GoraException) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Element(org.jdom.Element) DynamoDBMappingBuilder(org.apache.gora.dynamodb.store.DynamoDBMapping.DynamoDBMappingBuilder) IOException(java.io.IOException) Document(org.jdom.Document) GoraException(org.apache.gora.util.GoraException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 55 with GoraException

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

the class DynamoDBStore method schemaExists.

/**
 * Verifies if the specified schemas exist
 *
 * @return
 */
@Override
public boolean schemaExists() throws GoraException {
    try {
        LOG.info("Verifying schemas.");
        TableDescription success = null;
        if (getDynamoDbMapping().getTables().isEmpty())
            throw new IllegalStateException("There are not tables defined.");
        if (getPreferredSchema() == null) {
            LOG.debug("Verifying schemas");
            if (getDynamoDbMapping().getTables().isEmpty())
                throw new IllegalStateException("There are not tables defined.");
            // read the mapping object
            for (String tableName : getDynamoDbMapping().getTables().keySet()) {
                success = getTableSchema(tableName);
                if (success == null)
                    return false;
            }
        } else {
            LOG.info("Verifying schema " + preferredSchema);
            success = getTableSchema(preferredSchema);
        }
        LOG.info("Finished verifying schemas.");
        return (success != null) ? true : false;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) GoraException(org.apache.gora.util.GoraException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

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