Search in sources :

Example 1 with ElementSerialisation

use of uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation in project Gaffer by gchq.

the class AddElementsHandler method addElements.

private void addElements(final AddElements addElementsOperation, final HBaseStore store) throws OperationException {
    if (null == addElementsOperation.getInput()) {
        return;
    }
    try {
        final Table table = store.getTable();
        final Iterator<? extends Element> elements = addElementsOperation.getInput().iterator();
        final ElementSerialisation serialisation = new ElementSerialisation(store.getSchema());
        final int batchSize = store.getProperties().getWriteBufferSize();
        List<Put> puts = new ArrayList<>(batchSize);
        while (elements.hasNext()) {
            for (int i = 0; i < batchSize && elements.hasNext(); i++) {
                final Element element = elements.next();
                if (null == element) {
                    i--;
                    continue;
                }
                try {
                    final Pair<Put, Put> putPair = serialisation.getPuts(element);
                    puts.add(putPair.getFirst());
                    if (null != putPair.getSecond()) {
                        i++;
                        if (i >= batchSize) {
                            executePuts(table, puts);
                            puts = new ArrayList<>(batchSize);
                            i = 0;
                        }
                        puts.add(putPair.getSecond());
                    }
                } catch (final Exception e) {
                    if (addElementsOperation.isValidate() && !addElementsOperation.isSkipInvalidElements()) {
                        throw e;
                    }
                // otherwise just ignore the error
                }
            }
            executePuts(table, puts);
            puts = new ArrayList<>(batchSize);
        }
    } catch (final IOException | StoreException e) {
        throw new OperationException("Failed to add elements", e);
    }
}
Also used : HTable(org.apache.hadoop.hbase.client.HTable) Table(org.apache.hadoop.hbase.client.Table) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) StoreException(uk.gov.gchq.gaffer.store.StoreException) IOException(java.io.IOException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StoreException(uk.gov.gchq.gaffer.store.StoreException) ElementSerialisation(uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 2 with ElementSerialisation

use of uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation in project Gaffer by gchq.

the class AddElementsFromHdfsReducer method setup.

@Override
protected void setup(final Context context) {
    super.setup(context);
    serialisation = new ElementSerialisation(schema);
}
Also used : ElementSerialisation(uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation)

Example 3 with ElementSerialisation

use of uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation in project Gaffer by gchq.

the class AddElementsHandlerTest method shouldAddElements.

@Test
public void shouldAddElements() throws OperationException, StoreException, IOException {
    // Given
    final AddElementsHandler handler = new AddElementsHandler();
    final List<Element> elements = createElements();
    final List<Element> elementsWithNull = new ArrayList<>(elements);
    // null should be skipped
    elementsWithNull.add(null);
    final AddElements addElements = new AddElements.Builder().input(elementsWithNull).build();
    final Context context = mock(Context.class);
    final HBaseStore store = mock(HBaseStore.class);
    final HTable table = mock(HTable.class);
    given(store.getTable()).willReturn(table);
    final HBaseProperties properties = HBaseProperties.loadStoreProperties(StreamUtil.storeProps(getClass()));
    final int writeBufferSize = 5;
    properties.setWriteBufferSize(writeBufferSize);
    given(store.getProperties()).willReturn(properties);
    given(store.getSchema()).willReturn(SCHEMA);
    // When
    handler.doOperation(addElements, context, store);
    // Then
    final ArgumentCaptor<List<Put>> putsCaptor = (ArgumentCaptor) ArgumentCaptor.forClass(List.class);
    verify(table, times(2)).put(putsCaptor.capture());
    verify(table, times(2)).flushCommits();
    final List<List<Put>> allPuts = putsCaptor.getAllValues();
    assertThat(allPuts).hasSize(2);
    final List<Put> combinedPuts = new ArrayList<>();
    combinedPuts.addAll(allPuts.get(0));
    combinedPuts.addAll(allPuts.get(1));
    final List<Element> expectedElements = new ArrayList<>();
    for (final Element element : elements) {
        expectedElements.add(element);
        if (element instanceof Edge && !((Edge) element).getSource().equals(((Edge) element).getDestination())) {
            expectedElements.add(element);
        }
    }
    final Element[] expectedElementsArr = expectedElements.toArray(new Element[expectedElements.size()]);
    final List<Element> elementsAdded = CellUtil.getElements(combinedPuts, new ElementSerialisation(SCHEMA), false);
    assertEquals(expectedElements.size(), elementsAdded.size());
    assertThat(elementsAdded).contains(expectedElementsArr);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Context(uk.gov.gchq.gaffer.store.Context) ArgumentCaptor(org.mockito.ArgumentCaptor) HBaseStore(uk.gov.gchq.gaffer.hbasestore.HBaseStore) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) HTable(org.apache.hadoop.hbase.client.HTable) Put(org.apache.hadoop.hbase.client.Put) ElementSerialisation(uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation) ArrayList(java.util.ArrayList) List(java.util.List) HBaseProperties(uk.gov.gchq.gaffer.hbasestore.HBaseProperties) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 4 with ElementSerialisation

use of uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation in project Gaffer by gchq.

the class AddElementsFromHdfsMapper method setup.

@Override
protected void setup(final Context context) {
    super.setup(context);
    serialisation = new ElementSerialisation(schema);
    Configuration conf = context.getConfiguration();
    this.kvCreator = new CellCreator(conf);
}
Also used : CellCreator(org.apache.hadoop.hbase.mapreduce.CellCreator) Configuration(org.apache.hadoop.conf.Configuration) ElementSerialisation(uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation)

Example 5 with ElementSerialisation

use of uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation in project Gaffer by gchq.

the class GafferCoprocessor method start.

@Override
public void start(final CoprocessorEnvironment e) throws IOException {
    final String schemaJson = StringUtil.unescapeComma(e.getConfiguration().get(HBaseStoreConstants.SCHEMA));
    schema = Schema.fromJson(Bytes.toBytes(schemaJson));
    serialisation = new ElementSerialisation(schema);
}
Also used : ElementSerialisation(uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation)

Aggregations

ElementSerialisation (uk.gov.gchq.gaffer.hbasestore.serialisation.ElementSerialisation)5 ArrayList (java.util.ArrayList)2 HTable (org.apache.hadoop.hbase.client.HTable)2 Put (org.apache.hadoop.hbase.client.Put)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 IOException (java.io.IOException)1 List (java.util.List)1 Configuration (org.apache.hadoop.conf.Configuration)1 Table (org.apache.hadoop.hbase.client.Table)1 CellCreator (org.apache.hadoop.hbase.mapreduce.CellCreator)1 Test (org.junit.jupiter.api.Test)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 HBaseProperties (uk.gov.gchq.gaffer.hbasestore.HBaseProperties)1 HBaseStore (uk.gov.gchq.gaffer.hbasestore.HBaseStore)1 OperationException (uk.gov.gchq.gaffer.operation.OperationException)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 Context (uk.gov.gchq.gaffer.store.Context)1 StoreException (uk.gov.gchq.gaffer.store.StoreException)1