Search in sources :

Example 11 with GoraException

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

the class HBaseStore method execute.

@Override
public org.apache.gora.query.Result<K, T> execute(Query<K, T> query) throws GoraException {
    try {
        // check if query.fields is null
        query.setFields(getFieldsToQuery(query.getFields()));
        if (query.getStartKey() != null && query.getStartKey().equals(query.getEndKey())) {
            Get get = new Get(toBytes(query.getStartKey()));
            addFields(get, query.getFields());
            addTimeRange(get, query);
            Result result = table.get(get);
            return new HBaseGetResult<>(this, query, result);
        } else {
            ResultScanner scanner = createScanner(query);
            org.apache.gora.query.Result<K, T> result = new HBaseScannerResult<>(this, query, scanner);
            return result;
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Get(org.apache.hadoop.hbase.client.Get) HBaseScannerResult(org.apache.gora.hbase.query.HBaseScannerResult) IOException(java.io.IOException) HBaseScannerResult(org.apache.gora.hbase.query.HBaseScannerResult) Result(org.apache.hadoop.hbase.client.Result) HBaseGetResult(org.apache.gora.hbase.query.HBaseGetResult) HBaseGetResult(org.apache.gora.hbase.query.HBaseGetResult)

Example 12 with GoraException

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

the class HBaseStore method deleteSchema.

@Override
public void deleteSchema() throws GoraException {
    Admin admin = null;
    try {
        admin = table.getAdmin();
        if (!schemaExists()) {
            return;
        }
        admin.disableTable(mapping.getTable().getTableName());
        admin.deleteTable(mapping.getTable().getTableName());
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    } finally {
        try {
            if (admin != null) {
                admin.close();
            }
        } catch (IOException e) {
            LOG.error("An error occurred whilst closing HBase Admin. ", e);
        }
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) Admin(org.apache.hadoop.hbase.client.Admin) GoraException(org.apache.gora.util.GoraException) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 13 with GoraException

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

the class HBaseStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    try {
        String[] fields = getFieldsToQuery(query.getFields());
        // find whether all fields are queried, which means that complete
        // rows will be deleted
        boolean isAllFields = Arrays.equals(fields, getFields());
        org.apache.gora.query.Result<K, T> result = null;
        result = query.execute();
        ArrayList<Delete> deletes = new ArrayList<>();
        while (result.next()) {
            Delete delete = new Delete(toBytes(result.getKey()));
            deletes.add(delete);
            if (!isAllFields) {
                addFields(delete, query);
            }
        }
        table.delete(deletes);
        return deletes.size();
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) ArrayList(java.util.ArrayList) GoraException(org.apache.gora.util.GoraException) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException)

Example 14 with GoraException

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

the class HBaseStore method get.

@Override
public T get(K key, String[] fields) throws GoraException {
    try {
        fields = getFieldsToQuery(fields);
        Get get = new Get(toBytes(key));
        addFields(get, fields);
        Result result = table.get(get);
        return newInstance(result, fields);
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) Get(org.apache.hadoop.hbase.client.Get) GoraException(org.apache.gora.util.GoraException) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HBaseScannerResult(org.apache.gora.hbase.query.HBaseScannerResult) Result(org.apache.hadoop.hbase.client.Result) HBaseGetResult(org.apache.gora.hbase.query.HBaseGetResult)

Example 15 with GoraException

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

the class HBaseStore 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.
 *
 * The mapping can be passed as a configuration parameter 'gora.mapping' or taken from
 * gora-hbase-mapping.xml (in this order).
 *
 * @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 {
        this.conf = HBaseConfiguration.create(getConf());
        InputStream mappingInputStream;
        // If there is a mapping definition in the Properties, use it.
        if (properties.containsKey(XML_MAPPING_DEFINITION)) {
            if (LOG.isTraceEnabled())
                LOG.trace(XML_MAPPING_DEFINITION + " = " + properties.getProperty(XML_MAPPING_DEFINITION));
            mappingInputStream = IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION), (Charset) null);
        } else // Otherwise use the configuration from de default file gora-hbase-mapping.xml or whatever
        // configured in the key "gora.hbase.mapping.file"
        {
            String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE);
            // configurations for mapping.file key
            if (mappingFile.equals(DEFAULT_MAPPING_FILE)) {
                mappingFile = getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE);
            }
            mappingInputStream = getClass().getClassLoader().getResourceAsStream(mappingFile);
        }
        mapping = readMapping(mappingInputStream);
        filterUtil = new HBaseFilterUtil<>(this.conf);
    } catch (FileNotFoundException ex) {
        throw new GoraException("Mapping file '" + getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE) + "' not found.", ex);
    } catch (Exception e) {
        throw new GoraException(e);
    }
    // Set scanner caching option
    try {
        this.setScannerCaching(Integer.valueOf(DataStoreFactory.findProperty(this.properties, this, SCANNER_CACHING_PROPERTIES_KEY, String.valueOf(SCANNER_CACHING_PROPERTIES_DEFAULT))));
    } catch (NumberFormatException e) {
        LOG.info("Can not load {} from gora.properties. Setting to default value: {}.", SCANNER_CACHING_PROPERTIES_KEY, SCANNER_CACHING_PROPERTIES_DEFAULT);
        // Default value if something is wrong
        this.setScannerCaching(SCANNER_CACHING_PROPERTIES_DEFAULT);
    }
    try {
        boolean autoflush = Boolean.valueOf(DataStoreFactory.findProperty(this.properties, this, HBASE_CLIENT_AUTO_FLUSH_PROPERTIES_KEY, String.valueOf(HBASE_CLIENT_AUTO_FLUSH_PROPERTIES_DEFAULT)));
        table = new HBaseTableConnection(getConf(), getSchemaName(), autoflush);
    } catch (Exception e) {
        throw new GoraException(e);
    }
    if (autoCreateSchema) {
        createSchema();
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Charset(java.nio.charset.Charset) GoraException(org.apache.gora.util.GoraException) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

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