Search in sources :

Example 16 with GoraException

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

the class IgniteStore method schemaExists.

@Override
public boolean schemaExists() throws GoraException {
    try (Statement stmt = connection.createStatement()) {
        String tableExistsSQL = IgniteSQLBuilder.tableExists(igniteMapping.getTableName());
        ResultSet executeQuery = stmt.executeQuery(tableExistsSQL);
        executeQuery.close();
        return true;
    } catch (SQLException ex) {
        if (ex.getSQLState() != null && ex.getSQLState().equals(IgniteBackendConstants.DEFAULT_IGNITE_TABLE_NOT_EXISTS_CODE)) {
            return false;
        } else {
            throw new GoraException(ex);
        }
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 17 with GoraException

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

the class IgniteStore method createSchema.

@Override
public void createSchema() throws GoraException {
    if (connection == null) {
        throw new GoraException("Impossible to create the schema as no connection has been initiated.");
    }
    if (schemaExists()) {
        return;
    }
    try (Statement stmt = connection.createStatement()) {
        String createTableSQL = IgniteSQLBuilder.createTable(igniteMapping);
        stmt.executeUpdate(createTableSQL);
        LOG.info("Table {} has been created for Ignite instance.", igniteMapping.getTableName());
    } catch (SQLException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement)

Example 18 with GoraException

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

the class IgniteStore method delete.

@Override
public boolean delete(K key) throws GoraException {
    String deleteQuery = null;
    Object[] keyArray = null;
    if (igniteMapping.getPrimaryKey().size() == 1) {
        deleteQuery = IgniteSQLBuilder.createDeleteQuery(igniteMapping);
        keyArray = new Object[] { key };
    } else {
    // Composite key pending
    }
    try (PreparedStatement stmt = connection.prepareStatement(deleteQuery)) {
        IgniteSQLBuilder.fillDeleteQuery(stmt, igniteMapping, keyArray);
        stmt.executeUpdate();
        return true;
    } catch (SQLException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 19 with GoraException

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

the class IgniteStore method get.

@Override
public T get(K key, String[] fields) throws GoraException {
    String[] avFields = getFieldsToQuery(fields);
    Object[] keyl = null;
    if (igniteMapping.getPrimaryKey().size() == 1) {
        keyl = new Object[] { key };
    } else {
    // Composite key pending
    }
    // Avro fields to Ignite fields
    List<String> dbFields = new ArrayList<>();
    for (String af : avFields) {
        dbFields.add(igniteMapping.getFields().get(af).getName());
    }
    String selectQuery = IgniteSQLBuilder.createSelectQueryGet(igniteMapping, dbFields);
    try (PreparedStatement stmt = connection.prepareStatement(selectQuery)) {
        IgniteSQLBuilder.fillSelectQuery(stmt, igniteMapping, keyl);
        ResultSet rs = stmt.executeQuery();
        boolean data = rs.next();
        T resp = null;
        if (data) {
            resp = newInstance(rs, fields);
            if (rs.next()) {
                LOG.warn("Multiple results for primary key {} in the schema {}, ignoring additional rows.", keyl, igniteMapping.getTableName());
            }
        }
        rs.close();
        return resp;
    } catch (SQLException | IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException) ResultSet(java.sql.ResultSet)

Example 20 with GoraException

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

the class IgniteStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    try {
        super.initialize(keyClass, persistentClass, properties);
        IgniteMappingBuilder<K, T> builder = new IgniteMappingBuilder<K, T>(this);
        InputStream mappingStream;
        if (properties.containsKey(XML_MAPPING_DEFINITION)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("{} = {}", XML_MAPPING_DEFINITION, properties.getProperty(XML_MAPPING_DEFINITION));
            }
            mappingStream = org.apache.commons.io.IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION), (Charset) null);
        } else {
            mappingStream = getClass().getClassLoader().getResourceAsStream(getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE));
        }
        builder.readMappingFile(mappingStream);
        igniteMapping = builder.getIgniteMapping();
        igniteParameters = IgniteParameters.load(properties);
        connection = acquireConnection(this.igniteParameters);
        LOG.info("Ignite store was successfully initialized");
        if (!schemaExists()) {
            createSchema();
        }
    } catch (ClassNotFoundException | SQLException ex) {
        LOG.error("Error while initializing Ignite store", ex);
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset)

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