Search in sources :

Example 6 with Put

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

the class RecordPutTransformer method toPut.

public Put toPut(StructuredRecord record) {
    Schema recordSchema = record.getSchema();
    Preconditions.checkArgument(recordSchema.getType() == Schema.Type.RECORD, "input must be a record.");
    Schema.Field keyField = getKeyField(recordSchema);
    Preconditions.checkArgument(keyField != null, "Could not find key field in record.");
    Put output = createPut(record, keyField);
    for (Schema.Field field : recordSchema.getFields()) {
        if (field.getName().equals(keyField.getName())) {
            continue;
        }
        // Skip fields that are not present in the Output Schema
        if (outputSchema != null && outputSchema.getField(field.getName()) == null) {
            continue;
        }
        setField(output, field, record.get(field.getName()));
    }
    return output;
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) Put(io.cdap.cdap.api.dataset.table.Put)

Example 7 with Put

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

the class DatasetOpExecutorServiceTest method testRest.

@Test
public void testRest() throws Exception {
    // check non-existence with 404
    testAdminOp(bob, "exists", 404, null);
    // add instance, should automatically create an instance
    dsFramework.addInstance("table", bob, DatasetProperties.EMPTY);
    testAdminOp(bob, "exists", 200, true);
    testAdminOp("bob", "exists", 404, null);
    // check truncate
    final Table table = dsFramework.getDataset(bob, DatasetDefinition.NO_ARGUMENTS, null);
    Assert.assertNotNull(table);
    TransactionExecutor txExecutor = new DefaultTransactionExecutor(new InMemoryTxSystemClient(txManager), ImmutableList.of((TransactionAware) table));
    // writing smth to table
    txExecutor.execute(() -> table.put(new Put("key1", "col1", "val1")));
    // verify that we can read the data
    txExecutor.execute(() -> Assert.assertEquals("val1", table.get(new Get("key1", "col1")).getString("col1")));
    testAdminOp(bob, "truncate", 200, null);
    // verify that data is no longer there
    txExecutor.execute(() -> Assert.assertTrue(table.get(new Get("key1", "col1")).isEmpty()));
    // check upgrade
    testAdminOp(bob, "upgrade", 200, null);
    // drop and check non-existence
    dsFramework.deleteInstance(bob);
    testAdminOp(bob, "exists", 404, null);
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) TransactionAware(org.apache.tephra.TransactionAware) Get(io.cdap.cdap.api.dataset.table.Get) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) InMemoryTxSystemClient(org.apache.tephra.inmemory.InMemoryTxSystemClient) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 8 with Put

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

the class ReflectionTableTest method assertGetAndPut.

private void assertGetAndPut(final Table table, final byte[] rowKey, final User obj, final Schema schema) throws Exception {
    // TableDataset is not accessible here, but we know that's the underlying implementation...
    TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) table);
    tx.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Put put = new Put(rowKey);
            ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(schema);
            putWriter.write(obj, put);
            table.put(put);
            Row row = table.get(rowKey);
            ReflectionRowReader<User> rowReader = new ReflectionRowReader<>(schema, TypeToken.of(User.class));
            User actual = rowReader.read(row, schema);
            Assert.assertEquals(obj, actual);
        }
    });
}
Also used : ReflectionRowReader(io.cdap.cdap.internal.io.ReflectionRowReader) ReflectionPutWriter(io.cdap.cdap.internal.io.ReflectionPutWriter) TransactionExecutor(org.apache.tephra.TransactionExecutor) Row(io.cdap.cdap.api.dataset.table.Row) Put(io.cdap.cdap.api.dataset.table.Put)

Example 9 with Put

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

the class ReflectionTableTest method testStructuredRecordProjection.

@Test
public void testStructuredRecordProjection() throws Exception {
    dsFrameworkUtil.createInstance("table", users, DatasetProperties.builder().build());
    try {
        final Table usersTable = dsFrameworkUtil.getInstance(users);
        final byte[] rowKey = Bytes.toBytes(123);
        final User2 projected = new User2("Samuel L.", 123L, ((Float) 50000000.02f).doubleValue(), Double.MAX_VALUE, ByteBuffer.wrap(new byte[] { 0, 1, 2 }));
        final Schema fullSchema = new ReflectionSchemaGenerator().generate(User.class);
        final Schema projSchema = new ReflectionSchemaGenerator().generate(User2.class);
        // TableDataset is not accessible here, but we know that's the underlying implementation...
        TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor((TransactionAware) usersTable);
        tx.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Put put = new Put(rowKey);
                ReflectionPutWriter<User> putWriter = new ReflectionPutWriter<>(fullSchema);
                putWriter.write(SAMUEL, put);
                usersTable.put(put);
                Row row = usersTable.get(rowKey);
                ReflectionRowRecordReader rowReader = new ReflectionRowRecordReader(projSchema, null);
                StructuredRecord actual = rowReader.read(row, fullSchema);
                assertRecordEqualsUser(projected, actual);
            }
        });
    } finally {
        dsFrameworkUtil.deleteInstance(users);
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) Schema(io.cdap.cdap.api.data.schema.Schema) TransactionExecutor(org.apache.tephra.TransactionExecutor) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Put(io.cdap.cdap.api.dataset.table.Put) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) ReflectionPutWriter(io.cdap.cdap.internal.io.ReflectionPutWriter) Row(io.cdap.cdap.api.dataset.table.Row) ReflectionRowRecordReader(io.cdap.cdap.internal.io.ReflectionRowRecordReader) Test(org.junit.Test)

Example 10 with Put

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

the class RecordPutTransformerTest method testNullableSchema.

@Test
public void testNullableSchema() throws Exception {
    // tests that null can be passed in for the schema (in which case it will pickup the schema from the record
    Schema schema = Schema.recordOf("record", Schema.Field.of("key", Schema.of(Schema.Type.INT)), Schema.Field.of("nullable", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("non_nullable", Schema.of(Schema.Type.STRING)));
    RecordPutTransformer transformer = new RecordPutTransformer("key", null);
    // valid record
    StructuredRecord record = StructuredRecord.builder(schema).set("key", 1).set("non_nullable", "foo").build();
    Put transformed = transformer.toPut(record);
    Assert.assertEquals(1, Bytes.toInt(transformed.getRow()));
    // expect a null value for the nullable field
    Assert.assertEquals(2, transformed.getValues().size());
    Assert.assertEquals("foo", Bytes.toString(transformed.getValues().get(Bytes.toBytes("non_nullable"))));
    Assert.assertNull(transformed.getValues().get(Bytes.toBytes("nullable")));
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

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