use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class RecordPutTransformerTest method testNullableFields.
@Test
public void testNullableFields() throws Exception {
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", schema);
// 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")));
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class RecordPutTransformerTest method testTransform.
@Test
public void testTransform() throws Exception {
Schema schema = Schema.recordOf("record", Schema.Field.of("boolField", Schema.nullableOf(Schema.of(Schema.Type.BOOLEAN))), Schema.Field.of("intField", Schema.nullableOf(Schema.of(Schema.Type.INT))), Schema.Field.of("longField", Schema.nullableOf(Schema.of(Schema.Type.LONG))), Schema.Field.of("floatField", Schema.nullableOf(Schema.of(Schema.Type.FLOAT))), Schema.Field.of("doubleField", Schema.nullableOf(Schema.of(Schema.Type.DOUBLE))), Schema.Field.of("bytesField", Schema.nullableOf(Schema.of(Schema.Type.BYTES))), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
RecordPutTransformer transformer = new RecordPutTransformer("stringField", schema);
StructuredRecord record = StructuredRecord.builder(schema).set("boolField", true).set("intField", 5).set("longField", 10L).set("floatField", 3.14f).set("doubleField", 3.14).set("bytesField", Bytes.toBytes("foo")).set("stringField", "key").build();
Put transformed = transformer.toPut(record);
Assert.assertEquals("key", Bytes.toString(transformed.getRow()));
Map<byte[], byte[]> values = transformed.getValues();
Assert.assertTrue(Bytes.toBoolean(values.get(Bytes.toBytes("boolField"))));
Assert.assertEquals(5, Bytes.toInt(values.get(Bytes.toBytes("intField"))));
Assert.assertEquals(10L, Bytes.toLong(values.get(Bytes.toBytes("longField"))));
Assert.assertTrue(Math.abs(3.14f - Bytes.toFloat(values.get(Bytes.toBytes("floatField")))) < 0.000001);
Assert.assertTrue(Math.abs(3.14 - Bytes.toDouble(values.get(Bytes.toBytes("doubleField")))) < 0.000001);
Assert.assertArrayEquals(Bytes.toBytes("foo"), values.get(Bytes.toBytes("bytesField")));
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class RecordPutTransformerTest method testOutputSchemaUsage.
@Test
public void testOutputSchemaUsage() {
Schema outputSchema = Schema.recordOf("output", Schema.Field.of("id", Schema.of(Schema.Type.LONG)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)));
Schema inputSchema = Schema.recordOf("input", Schema.Field.of("id", Schema.of(Schema.Type.LONG)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("age", Schema.of(Schema.Type.INT)));
StructuredRecord record = StructuredRecord.builder(inputSchema).set("id", 123L).set("name", "ABC").set("age", 10).build();
RecordPutTransformer transformer = new RecordPutTransformer("id", outputSchema);
Put put = transformer.toPut(record);
Assert.assertEquals(1, put.getValues().size());
transformer = new RecordPutTransformer("id", inputSchema);
put = transformer.toPut(record);
Assert.assertEquals(2, put.getValues().size());
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class MetricsTableOnTable method put.
@Override
public void put(SortedMap<byte[], ? extends SortedMap<byte[], Long>> updates) {
for (Map.Entry<byte[], ? extends SortedMap<byte[], Long>> rowUpdate : updates.entrySet()) {
Put put = new Put(rowUpdate.getKey());
for (Map.Entry<byte[], Long> columnUpdate : rowUpdate.getValue().entrySet()) {
put.add(columnUpdate.getKey(), columnUpdate.getValue());
}
table.put(put);
}
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class TableTest method testMultiGetWithTx.
@Test
public void testMultiGetWithTx() throws Exception {
String testMultiGet = "testMultiGet";
DatasetAdmin admin = getTableAdmin(CONTEXT1, testMultiGet);
admin.create();
try (Table table = getTable(CONTEXT1, testMultiGet)) {
Transaction tx = txClient.startShort();
((TransactionAware) table).startTx(tx);
for (int i = 0; i < 100; i++) {
table.put(new Put(Bytes.toBytes("r" + i)).add(C1, V1).add(C2, V2));
}
txClient.canCommitOrThrow(tx, ((TransactionAware) table).getTxChanges());
Assert.assertTrue(((TransactionAware) table).commitTx());
txClient.commitOrThrow(tx);
Transaction tx2 = txClient.startShort();
((TransactionAware) table).startTx(tx2);
List<Get> gets = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < 100; i++) {
gets.add(new Get(Bytes.toBytes("r" + i)));
}
List<Row> results = table.get(gets);
txClient.commitOrThrow(tx2);
for (int i = 0; i < 100; i++) {
Row row = results.get(i);
Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
byte[] val = row.get(C1);
Assert.assertNotNull(val);
Assert.assertArrayEquals(V1, val);
byte[] val2 = row.get(C2);
Assert.assertNotNull(val2);
Assert.assertArrayEquals(V2, val2);
}
Transaction tx3 = txClient.startShort();
((TransactionAware) table).startTx(tx3);
gets = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < 100; i++) {
gets.add(new Get("r" + i).add(C1));
}
results = table.get(gets);
txClient.commitOrThrow(tx3);
for (int i = 0; i < 100; i++) {
Row row = results.get(i);
Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
byte[] val = row.get(C1);
Assert.assertNotNull(val);
Assert.assertArrayEquals(V1, val);
// should have only returned column 1
byte[] val2 = row.get(C2);
Assert.assertNull(val2);
}
// retrieve different columns per row
Transaction tx4 = txClient.startShort();
((TransactionAware) table).startTx(tx4);
gets = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < 100; i++) {
Get get = new Get("r" + i);
// evens get C1, odds get C2
get.add(i % 2 == 0 ? C1 : C2);
gets.add(get);
}
results = table.get(gets);
txClient.commitOrThrow(tx4);
for (int i = 0; i < 100; i++) {
Row row = results.get(i);
Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
byte[] val1 = row.get(C1);
byte[] val2 = row.get(C2);
if (i % 2 == 0) {
Assert.assertNotNull(val1);
Assert.assertArrayEquals(V1, val1);
Assert.assertNull(val2);
} else {
Assert.assertNull(val1);
Assert.assertNotNull(val2);
Assert.assertArrayEquals(V2, val2);
}
}
} finally {
admin.drop();
}
}
Aggregations