Search in sources :

Example 91 with GoraException

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

the class RethinkDBStore method put.

/**
 * {@inheritDoc}
 */
@Override
public void put(K key, T val) throws GoraException {
    if (val.isDirty()) {
        try {
            boolean isExists = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).getAll(key).count().run(connection, Boolean.class).first();
            if (!isExists) {
                MapObject<String, Object> document = convertAvroBeanToRethinkDBDocument(key, val);
                r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).insert(document).run(connection);
            } else {
                MapObject<String, Object> document = convertAvroBeanToRethinkDBDocument(key, val);
                r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).get(key).replace(document).run(connection);
            }
        } catch (Exception e) {
            throw new GoraException(e);
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.info("Ignored putting persistent bean {} in the store as it is neither " + "new, neither dirty.", new Object[] { val });
        }
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) MapObject(com.rethinkdb.model.MapObject) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 92 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() throws IOException {
    DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(getClass().getClassLoader().getResourceAsStream(MAPPING_FILE));
        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 93 with GoraException

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

the class AccumuloStore method put.

@Override
public void put(K key, T val) throws GoraException {
    try {
        Mutation m = new Mutation(new Text(toBytes(key)));
        Schema schema = val.getSchema();
        List<Field> fields = schema.getFields();
        int count = 0;
        for (int i = 0; i < fields.size(); i++) {
            if (!val.isDirty(i)) {
                continue;
            }
            Field field = fields.get(i);
            Object o = val.get(field.pos());
            Pair<Text, Text> col = mapping.fieldMap.get(field.name());
            if (col == null) {
                throw new GoraException("Please define the gora to accumulo mapping for field " + field.name());
            }
            switch(field.schema().getType()) {
                case MAP:
                    count = putMap(m, count, field.schema().getValueType(), o, col, field.name());
                    break;
                case ARRAY:
                    count = putArray(m, count, o, col, field.name());
                    break;
                case // default value of null acts like union with null
                UNION:
                    Schema effectiveSchema = field.schema().getTypes().get(firstNotNullSchemaTypeIndex(field.schema()));
                    // map and array need to compute qualifier
                    if (effectiveSchema.getType() == Type.ARRAY) {
                        count = putArray(m, count, o, col, field.name());
                        break;
                    } else if (effectiveSchema.getType() == Type.MAP) {
                        count = putMap(m, count, effectiveSchema.getValueType(), o, col, field.name());
                        break;
                    }
                // continue like a regular top-level union
                case RECORD:
                    final SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(field.schema());
                    final byte[] byteData = IOUtils.serialize(writer, o);
                    m.put(col.getFirst(), col.getSecond(), new Value(byteData));
                    count++;
                    break;
                default:
                    m.put(col.getFirst(), col.getSecond(), new Value(toBytes(o)));
                    count++;
            }
        }
        if (count > 0)
            try {
                getBatchWriter().addMutation(m);
            } catch (MutationsRejectedException e) {
                LOG.error(e.getMessage(), e);
            }
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Schema(org.apache.avro.Schema) Text(org.apache.hadoop.io.Text) TableOfflineException(org.apache.accumulo.core.client.TableOfflineException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) GoraException(org.apache.gora.util.GoraException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) TableDeletedException(org.apache.accumulo.core.client.TableDeletedException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) Value(org.apache.accumulo.core.data.Value) Mutation(org.apache.accumulo.core.data.Mutation) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 94 with GoraException

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

the class AccumuloStore method exists.

@Override
public boolean exists(K key) throws GoraException {
    try {
        Scanner scanner = new IsolatedScanner(conn.createScanner(mapping.tableName, Authorizations.EMPTY));
        Range rowRange = new Range(new Text(toBytes(key)));
        scanner.setRange(rowRange);
        scanner.clearColumns();
        return scanner.iterator().hasNext();
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) GoraException(org.apache.gora.util.GoraException) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) TableOfflineException(org.apache.accumulo.core.client.TableOfflineException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) GoraException(org.apache.gora.util.GoraException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) TableDeletedException(org.apache.accumulo.core.client.TableDeletedException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException)

Example 95 with GoraException

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

the class AccumuloStore method initialize.

/**
 * Initialize the data store by reading the credentials, setting the client's properties up and
 * reading the mapping file. Initialize is called when then the call to
 * {@link org.apache.gora.store.DataStoreFactory#createDataStore} is made.
 *
 * @param keyClass
 * @param persistentClass
 * @param properties
 */
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    super.initialize(keyClass, persistentClass, properties);
    try {
        String mock = DataStoreFactory.findProperty(properties, this, MOCK_PROPERTY, null);
        String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE);
        String user = DataStoreFactory.findProperty(properties, this, USERNAME_PROPERTY, null);
        String password = DataStoreFactory.findProperty(properties, this, PASSWORD_PROPERTY, null);
        mapping = readMapping(mappingFile);
        if (mapping.encoder == null || "".equals(mapping.encoder)) {
            encoder = new BinaryEncoder();
        } else {
            encoder = (Encoder) getClass().getClassLoader().loadClass(mapping.encoder).getDeclaredConstructor().newInstance();
        }
        AuthenticationToken token = new PasswordToken(password);
        if (mock == null || !mock.equals("true")) {
            String instance = DataStoreFactory.findProperty(properties, this, INSTANCE_NAME_PROPERTY, null);
            String zookeepers = DataStoreFactory.findProperty(properties, this, ZOOKEEPERS_NAME_PROPERTY, null);
            conn = new ZooKeeperInstance(instance, zookeepers).getConnector(user, token);
        } else {
            conn = new MockInstance().getConnector(user, token);
        }
        credentials = new Credentials(user, token);
        if (autoCreateSchema && !schemaExists())
            createSchema();
    } catch (IOException | InstantiationException | IllegalAccessException | ClassNotFoundException | AccumuloException | AccumuloSecurityException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
        throw new GoraException(e);
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) AuthenticationToken(org.apache.accumulo.core.client.security.tokens.AuthenticationToken) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ZooKeeperInstance(org.apache.accumulo.core.client.ZooKeeperInstance) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) GoraException(org.apache.gora.util.GoraException) BinaryEncoder(org.apache.gora.accumulo.encoders.BinaryEncoder) MockInstance(org.apache.accumulo.core.client.mock.MockInstance) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Credentials(org.apache.accumulo.core.client.impl.Credentials)

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