Search in sources :

Example 96 with GoraException

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

the class AccumuloStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
    try {
        Scanner scanner = createScanner(query);
        // add iterator that drops values on the server side
        scanner.addScanIterator(new IteratorSetting(Integer.MAX_VALUE, SortedKeyIterator.class));
        RowIterator iterator = new RowIterator(scanner.iterator());
        long count = 0;
        while (iterator.hasNext()) {
            Iterator<Entry<Key, Value>> row = iterator.next();
            Mutation m = null;
            while (row.hasNext()) {
                Entry<Key, Value> entry = row.next();
                Key key = entry.getKey();
                if (m == null)
                    m = new Mutation(key.getRow());
                // TODO optimize to avoid continually creating column vis? prob does not matter for empty
                m.putDelete(key.getColumnFamily(), key.getColumnQualifier(), new ColumnVisibility(key.getColumnVisibility()), key.getTimestamp());
            }
            getBatchWriter().addMutation(m);
            count++;
        }
        return count;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) 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) Entry(java.util.Map.Entry) GoraException(org.apache.gora.util.GoraException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) SortedKeyIterator(org.apache.accumulo.core.iterators.SortedKeyIterator) RowIterator(org.apache.accumulo.core.client.RowIterator) Value(org.apache.accumulo.core.data.Value) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Key(org.apache.accumulo.core.data.Key)

Example 97 with GoraException

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

the class AccumuloStore method getPartitions.

@Override
public List<PartitionQuery<K, T>> getPartitions(Query<K, T> query) throws GoraException {
    try {
        TabletLocator tl;
        if (conn instanceof MockConnector)
            tl = new MockTabletLocator();
        else
            tl = TabletLocator.getLocator(new ClientContext(conn.getInstance(), credentials, AccumuloConfiguration.getTableConfiguration(conn, Tables.getTableId(conn.getInstance(), mapping.tableName))), new Text(Tables.getTableId(conn.getInstance(), mapping.tableName)));
        Map<String, Map<KeyExtent, List<Range>>> binnedRanges = new HashMap<>();
        tl.invalidateCache();
        while (tl.binRanges(new ClientContext(conn.getInstance(), credentials, AccumuloConfiguration.getTableConfiguration(conn, Tables.getTableId(conn.getInstance(), mapping.tableName))), Collections.singletonList(createRange(query)), binnedRanges).size() > 0) {
            // TODO log?
            if (!Tables.exists(conn.getInstance(), Tables.getTableId(conn.getInstance(), mapping.tableName)))
                throw new TableDeletedException(Tables.getTableId(conn.getInstance(), mapping.tableName));
            else if (Tables.getTableState(conn.getInstance(), Tables.getTableId(conn.getInstance(), mapping.tableName)) == TableState.OFFLINE)
                throw new TableOfflineException(conn.getInstance(), Tables.getTableId(conn.getInstance(), mapping.tableName));
            UtilWaitThread.sleep(100);
            tl.invalidateCache();
        }
        List<PartitionQuery<K, T>> ret = new ArrayList<>();
        Text startRow = null;
        Text endRow = null;
        if (query.getStartKey() != null)
            startRow = new Text(toBytes(query.getStartKey()));
        if (query.getEndKey() != null)
            endRow = new Text(toBytes(query.getEndKey()));
        // hadoop expects hostnames, accumulo keeps track of IPs... so need to convert
        HashMap<String, String> hostNameCache = new HashMap<>();
        for (Entry<String, Map<KeyExtent, List<Range>>> entry : binnedRanges.entrySet()) {
            String ip = entry.getKey().split(":", 2)[0];
            String location = hostNameCache.get(ip);
            if (location == null) {
                InetAddress inetAddress = InetAddress.getByName(ip);
                location = inetAddress.getHostName();
                hostNameCache.put(ip, location);
            }
            Map<KeyExtent, List<Range>> tablets = entry.getValue();
            for (KeyExtent ke : tablets.keySet()) {
                K startKey = null;
                if (startRow == null || !ke.contains(startRow)) {
                    if (ke.getPrevEndRow() != null) {
                        startKey = followingKey(encoder, getKeyClass(), getBytes(ke.getPrevEndRow()));
                    }
                } else {
                    startKey = fromBytes(getKeyClass(), getBytes(startRow));
                }
                K endKey = null;
                if (endRow == null || !ke.contains(endRow)) {
                    if (ke.getEndRow() != null)
                        endKey = lastPossibleKey(encoder, getKeyClass(), getBytes(ke.getEndRow()));
                } else {
                    endKey = fromBytes(getKeyClass(), getBytes(endRow));
                }
                PartitionQueryImpl<K, T> pqi = new PartitionQueryImpl<>(query, startKey, endKey, location);
                pqi.setConf(getConf());
                ret.add(pqi);
            }
        }
        return ret;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : TableOfflineException(org.apache.accumulo.core.client.TableOfflineException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KeyExtent(org.apache.accumulo.core.data.impl.KeyExtent) TableDeletedException(org.apache.accumulo.core.client.TableDeletedException) GoraException(org.apache.gora.util.GoraException) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) MockTabletLocator(org.apache.accumulo.core.client.mock.impl.MockTabletLocator) PartitionQueryImpl(org.apache.gora.query.impl.PartitionQueryImpl) ClientContext(org.apache.accumulo.core.client.impl.ClientContext) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) 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) MockTabletLocator(org.apache.accumulo.core.client.mock.impl.MockTabletLocator) TabletLocator(org.apache.accumulo.core.client.impl.TabletLocator) MockConnector(org.apache.accumulo.core.client.mock.MockConnector) Map(java.util.Map) HashMap(java.util.HashMap) PartitionQuery(org.apache.gora.query.PartitionQuery) InetAddress(java.net.InetAddress)

Example 98 with GoraException

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

the class AccumuloStore method get.

@Override
public T get(K key, String[] fields) throws GoraException {
    try {
        // TODO make isolated scanner optional?
        Scanner scanner = new IsolatedScanner(conn.createScanner(mapping.tableName, Authorizations.EMPTY));
        Range rowRange = new Range(new Text(toBytes(key)));
        scanner.setRange(rowRange);
        setFetchColumns(scanner, fields);
        T persistent = newPersistent();
        ByteSequence row = populate(scanner.iterator(), persistent);
        if (row == null)
            return null;
        return persistent;
    } 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) ByteSequence(org.apache.accumulo.core.data.ByteSequence) 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 99 with GoraException

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

the class AccumuloStore method createSchema.

@Override
public void createSchema() throws GoraException {
    try {
        conn.tableOperations().create(mapping.tableName);
        Set<Entry<String, String>> es = mapping.tableConfig.entrySet();
        for (Entry<String, String> entry : es) {
            conn.tableOperations().setProperty(mapping.tableName, entry.getKey(), entry.getValue());
        }
    } catch (TableExistsException e) {
        LOG.debug(e.getMessage(), e);
    // Assume this is not an error
    } catch (AccumuloException | AccumuloSecurityException e) {
        throw new GoraException(e);
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) Entry(java.util.Map.Entry) GoraException(org.apache.gora.util.GoraException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException)

Example 100 with GoraException

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

the class AccumuloStore method deleteSchema.

@Override
public void deleteSchema() throws GoraException {
    try {
        if (batchWriter != null)
            batchWriter.close();
        batchWriter = null;
        conn.tableOperations().delete(mapping.tableName);
    } catch (TableNotFoundException e) {
    // Ignore. Delete a non existant schema is a success
    } catch (AccumuloException | AccumuloSecurityException e) {
        throw new GoraException(e);
    }
}
Also used : TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) GoraException(org.apache.gora.util.GoraException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException)

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