Search in sources :

Example 36 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class AbstractTable method write.

@WriteOnly
@Override
public void write(StructuredRecord structuredRecord) throws IOException {
    if (recordPutTransformer == null) {
        throw new IllegalStateException(String.format("Table must have both '%s' and '%s' properties set in " + "order to be used as a RecordWritable.", Table.PROPERTY_SCHEMA, Table.PROPERTY_SCHEMA_ROW_FIELD));
    }
    Put put = recordPutTransformer.toPut(structuredRecord);
    put(put);
}
Also used : Put(io.cdap.cdap.api.dataset.table.Put) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 37 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class PartitionedFileSetDataset method setMetadata.

private void setMetadata(PartitionKey key, Map<String, String> metadata, boolean allowUpdates) {
    final byte[] rowKey = generateRowKey(key, partitioning);
    Row row = partitionsTable.get(rowKey);
    if (row.isEmpty()) {
        throw new PartitionNotFoundException(key, getName());
    }
    Put put = new Put(rowKey);
    addMetadataToPut(row, metadata, put, allowUpdates);
    partitionsTable.put(put);
}
Also used : PartitionNotFoundException(io.cdap.cdap.api.dataset.PartitionNotFoundException) Row(io.cdap.cdap.api.dataset.table.Row) Put(io.cdap.cdap.api.dataset.table.Put)

Example 38 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class PartitionedFileSetDataset method addPartition.

public void addPartition(PartitionKey key, String path, Map<String, String> metadata, boolean filesCreated, boolean allowAppend) {
    byte[] rowKey = generateRowKey(key, partitioning);
    Row row = partitionsTable.get(rowKey);
    boolean appending = !row.isEmpty();
    if (appending && !allowAppend) {
        throw new PartitionAlreadyExistsException(getName(), key);
    }
    if (appending) {
        // this can happen if user originally created the partition with a custom relative path
        String existingPath = Bytes.toString(row.get(RELATIVE_PATH));
        if (!path.equals(existingPath)) {
            throw new DataSetException(String.format("Attempting to append to Dataset '%s', to partition '%s' with a " + "different path. Original path: '%s'. New path: '%s'", getName(), key.toString(), existingPath, path));
        }
    }
    LOG.debug("{} partition with key {} and path {} to dataset {}", appending ? "Appending to" : "Creating", key, path, getName());
    AddPartitionOperation operation = new AddPartitionOperation(key, path, filesCreated);
    operationsInThisTx.add(operation);
    Put put = new Put(rowKey);
    byte[] nowInMillis = Bytes.toBytes(System.currentTimeMillis());
    if (!appending) {
        put.add(RELATIVE_PATH, Bytes.toBytes(path));
        put.add(CREATION_TIME_COL, nowInMillis);
    }
    put.add(LAST_MODIFICATION_TIME_COL, nowInMillis);
    // we allow updates, because an update will only happen if its an append
    addMetadataToPut(row, metadata, put, true);
    // index each row by its transaction's write pointer
    put.add(WRITE_PTR_COL, tx.getWritePointer());
    partitionsTable.put(put);
    if (!appending) {
        addPartitionToExplore(key, path);
        operation.setExplorePartitionCreated();
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) Row(io.cdap.cdap.api.dataset.table.Row) PartitionAlreadyExistsException(io.cdap.cdap.api.dataset.lib.PartitionAlreadyExistsException) Put(io.cdap.cdap.api.dataset.table.Put)

Example 39 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class TestFrameworkTestRun method testAppRedeployKeepsData.

@Test
public void testAppRedeployKeepsData() throws Exception {
    deployApplication(testSpace, AppWithTable.class);
    DataSetManager<Table> myTableManager = getDataset(testSpace.dataset("my_table"));
    myTableManager.get().put(new Put("key1", "column1", "value1"));
    myTableManager.flush();
    // Changes should be visible to other instances of datasets
    DataSetManager<Table> myTableManager2 = getDataset(testSpace.dataset("my_table"));
    Assert.assertEquals("value1", myTableManager2.get().get(new Get("key1", "column1")).getString("column1"));
    // Even after redeploy of an app: changes should be visible to other instances of datasets
    deployApplication(AppWithTable.class);
    DataSetManager<Table> myTableManager3 = getDataset(testSpace.dataset("my_table"));
    Assert.assertEquals("value1", myTableManager3.get().get(new Get("key1", "column1")).getString("column1"));
    // Calling commit again (to test we can call it multiple times)
    myTableManager.get().put(new Put("key1", "column1", "value2"));
    myTableManager.flush();
    Assert.assertEquals("value1", myTableManager3.get().get(new Get("key1", "column1")).getString("column1"));
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Get(io.cdap.cdap.api.dataset.table.Get) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 40 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class AbstractMockSink method transform.

@Override
public void transform(StructuredRecord input, Emitter<KeyValue<byte[], Put>> emitter) throws Exception {
    byte[] rowkey = Bytes.concat(Bytes.toBytes(System.currentTimeMillis()), Bytes.toBytes(inputCounter.incrementAndGet()), Bytes.toBytes(UUID.randomUUID()));
    Put put = new Put(rowkey);
    put.add(SCHEMA_COL, input.getSchema().toString());
    put.add(RECORD_COL, StructuredRecordStringConverter.toJsonString(input));
    emitter.emit(new KeyValue<>(rowkey, put));
}
Also used : Put(io.cdap.cdap.api.dataset.table.Put)

Aggregations

Put (io.cdap.cdap.api.dataset.table.Put)53 Test (org.junit.Test)24 Table (io.cdap.cdap.api.dataset.table.Table)23 Get (io.cdap.cdap.api.dataset.table.Get)13 Row (io.cdap.cdap.api.dataset.table.Row)13 Transaction (org.apache.tephra.Transaction)13 TransactionAware (org.apache.tephra.TransactionAware)13 Schema (io.cdap.cdap.api.data.schema.Schema)10 TransactionExecutor (org.apache.tephra.TransactionExecutor)10 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)7 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)6 IOException (java.io.IOException)6 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)5 DatasetId (io.cdap.cdap.proto.id.DatasetId)5 WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)4 DataSetException (io.cdap.cdap.api.dataset.DataSetException)4 Scanner (io.cdap.cdap.api.dataset.table.Scanner)4 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)4 ReflectionPutWriter (io.cdap.cdap.internal.io.ReflectionPutWriter)4 Map (java.util.Map)4