Search in sources :

Example 76 with Value

use of org.apache.accumulo.core.data.Value in project gora by apache.

the class AccumuloStore method deleteByQuery.

@Override
public long deleteByQuery(Query<K, T> query) {
    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 (TableNotFoundException e) {
        // TODO return 0?
        LOG.error(e.getMessage(), e);
        return 0;
    } catch (MutationsRejectedException e) {
        LOG.error(e.getMessage(), e);
        return 0;
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        return 0;
    }
}
Also used : IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) IOException(java.io.IOException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) Entry(java.util.Map.Entry) 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) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 77 with Value

use of org.apache.accumulo.core.data.Value in project gora by apache.

the class AccumuloStore method put.

@Override
public void put(K key, T val) {
    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 (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : Schema(org.apache.avro.Schema) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) 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 78 with Value

use of org.apache.accumulo.core.data.Value in project gora by apache.

the class AccumuloStore method putArray.

private int putArray(Mutation m, int count, Object o, Pair<Text, Text> col, String fieldName) {
    // First of all we delete array field on accumulo store
    Text rowKey = new Text(m.getRow());
    Query<K, T> query = newQuery();
    query.setFields(fieldName);
    query.setStartKey((K) rowKey.toString());
    query.setEndKey((K) rowKey.toString());
    deleteByQuery(query);
    flush();
    if (o == null) {
        return 0;
    }
    // both GenericArray and DirtyListWrapper
    List<?> array = (List<?>) o;
    int j = 0;
    for (Object item : array) {
        m.put(col.getFirst(), new Text(toBytes(j++)), new Value(toBytes(item)));
        count++;
    }
    return count;
}
Also used : Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList)

Example 79 with Value

use of org.apache.accumulo.core.data.Value in project gora by apache.

the class AccumuloStore method putMap.

private int putMap(Mutation m, int count, Schema valueType, Object o, Pair<Text, Text> col, String fieldName) throws GoraException {
    // First of all we delete map field on accumulo store
    Text rowKey = new Text(m.getRow());
    Query<K, T> query = newQuery();
    query.setFields(fieldName);
    query.setStartKey((K) rowKey.toString());
    query.setEndKey((K) rowKey.toString());
    deleteByQuery(query);
    flush();
    if (o == null) {
        return 0;
    }
    Set<?> es = ((Map<?, ?>) o).entrySet();
    for (Object entry : es) {
        Object mapKey = ((Entry<?, ?>) entry).getKey();
        Object mapVal = ((Entry<?, ?>) entry).getValue();
        if ((o instanceof DirtyMapWrapper && ((DirtyMapWrapper<?, ?>) o).isDirty()) || !(o instanceof DirtyMapWrapper)) {
            m.put(col.getFirst(), new Text(toBytes(mapKey)), new Value(toBytes(valueType, mapVal)));
            count++;
        }
    // TODO map value deletion
    }
    return count;
}
Also used : Entry(java.util.Map.Entry) DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Value (org.apache.accumulo.core.data.Value)79 Key (org.apache.accumulo.core.data.Key)64 Test (org.junit.Test)38 Edge (uk.gov.gchq.gaffer.data.element.Edge)30 HashMap (java.util.HashMap)23 Element (uk.gov.gchq.gaffer.data.element.Element)23 Mutation (org.apache.accumulo.core.data.Mutation)21 Text (org.apache.hadoop.io.Text)21 Authorizations (org.apache.accumulo.core.security.Authorizations)18 Entry (java.util.Map.Entry)15 Scanner (org.apache.accumulo.core.client.Scanner)15 BatchWriter (org.apache.accumulo.core.client.BatchWriter)12 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)11 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)11 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)11 AccumuloException (org.apache.accumulo.core.client.AccumuloException)10 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)10 Connector (org.apache.accumulo.core.client.Connector)9 Properties (uk.gov.gchq.gaffer.data.element.Properties)9 PasswordToken (org.apache.accumulo.core.client.security.tokens.PasswordToken)8