use of co.cask.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"));
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class CharCountProgram method initialize.
@Override
public void initialize() throws Exception {
SparkClientContext context = getContext();
context.setSparkConf(new SparkConf().set("spark.io.compression.codec", "org.apache.spark.io.LZFCompressionCodec"));
Table totals = context.getDataset("totals");
totals.get(new Get("total").add("total")).getLong("total");
totals.put(new Put("total").add("total", 0L));
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class HBaseTableTest method testColumnFamily.
@Test
public void testColumnFamily() throws Exception {
DatasetProperties props = TableProperties.builder().setColumnFamily("t").build();
String tableName = "testcf";
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
final BufferingTable table = getTable(CONTEXT1, tableName, props);
TransactionSystemClient txClient = new DetachedTxSystemClient();
TransactionExecutor executor = new DefaultTransactionExecutor(txClient, table);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(new Put("row", "column", "testValue"));
}
});
final BufferingTable table2 = getTable(CONTEXT1, tableName, props);
executor = new DefaultTransactionExecutor(txClient, table2);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("testValue", table2.get(new Get("row", "column")).getString("column"));
}
});
// Verify the column family name
TableId hTableId = hBaseTableUtil.createHTableId(new NamespaceId(CONTEXT1.getNamespaceId()), tableName);
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(TEST_HBASE.getHBaseAdmin(), hTableId);
HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
Assert.assertNotNull(hcd);
Assert.assertEquals("t", hcd.getNameAsString());
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class HiveExploreTableTestRun method setupTable.
private void setupTable(@Nullable String dbName, @Nullable String tableName) throws Exception {
if (dbName != null) {
runCommand(NAMESPACE_ID, "create database if not exists " + dbName, false, null, null);
}
datasetFramework.addInstance(Table.class.getName(), MY_TABLE, setupTableProperties(dbName, tableName, SCHEMA));
// Accessing dataset instance to perform data operations
Table table = datasetFramework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
TransactionAware txTable = (TransactionAware) table;
Transaction tx1 = transactionManager.startShort(100);
txTable.startTx(tx1);
Put put = new Put(Bytes.toBytes("row1"));
put.add("bool_field", false);
put.add("int_field", Integer.MAX_VALUE);
put.add("long_field", Long.MAX_VALUE);
put.add("float_field", 3.14f);
put.add("double_field", 3.14);
put.add("bytes_field", new byte[] { 'A', 'B', 'C' });
table.put(put);
Assert.assertTrue(txTable.commitTx());
transactionManager.canCommit(tx1, txTable.getTxChanges());
transactionManager.commit(tx1);
txTable.postTxCommit();
Transaction tx2 = transactionManager.startShort(100);
txTable.startTx(tx2);
}
use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.
the class HiveExploreTableTestRun method testInsertFromJoin.
@Test
public void testInsertFromJoin() throws Exception {
DatasetId userTableID = NAMESPACE_ID.dataset("users");
DatasetId purchaseTableID = NAMESPACE_ID.dataset("purchases");
DatasetId expandedTableID = NAMESPACE_ID.dataset("expanded");
Schema userSchema = Schema.recordOf("user", Schema.Field.of("id", Schema.of(Schema.Type.STRING)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("email", Schema.of(Schema.Type.STRING)));
Schema purchaseSchema = Schema.recordOf("purchase", Schema.Field.of("purchaseid", Schema.of(Schema.Type.LONG)), Schema.Field.of("itemid", Schema.of(Schema.Type.STRING)), Schema.Field.of("userid", Schema.of(Schema.Type.STRING)), Schema.Field.of("ct", Schema.of(Schema.Type.INT)), Schema.Field.of("price", Schema.of(Schema.Type.DOUBLE)));
Schema expandedSchema = Schema.recordOf("expandedPurchase", Schema.Field.of("purchaseid", Schema.of(Schema.Type.LONG)), Schema.Field.of("itemid", Schema.of(Schema.Type.STRING)), Schema.Field.of("userid", Schema.of(Schema.Type.STRING)), Schema.Field.of("ct", Schema.of(Schema.Type.INT)), Schema.Field.of("price", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("username", Schema.of(Schema.Type.STRING)), Schema.Field.of("email", Schema.of(Schema.Type.STRING)));
datasetFramework.addInstance(Table.class.getName(), userTableID, TableProperties.builder().setSchema(userSchema).setRowFieldName("id").build());
datasetFramework.addInstance(Table.class.getName(), purchaseTableID, TableProperties.builder().setSchema(purchaseSchema).setRowFieldName("purchaseid").build());
datasetFramework.addInstance(Table.class.getName(), expandedTableID, TableProperties.builder().setSchema(expandedSchema).setRowFieldName("purchaseid").build());
Table userTable = datasetFramework.getDataset(userTableID, DatasetDefinition.NO_ARGUMENTS, null);
Table purchaseTable = datasetFramework.getDataset(purchaseTableID, DatasetDefinition.NO_ARGUMENTS, null);
TransactionAware txUserTable = (TransactionAware) userTable;
TransactionAware txPurchaseTable = (TransactionAware) purchaseTable;
Transaction tx1 = transactionManager.startShort(100);
txUserTable.startTx(tx1);
txPurchaseTable.startTx(tx1);
Put put = new Put(Bytes.toBytes("samuel"));
put.add("name", "Samuel Jackson");
put.add("email", "sjackson@gmail.com");
userTable.put(put);
put = new Put(Bytes.toBytes(1L));
put.add("userid", "samuel");
put.add("itemid", "scotch");
put.add("ct", 1);
put.add("price", 56.99d);
purchaseTable.put(put);
txUserTable.commitTx();
txPurchaseTable.commitTx();
List<byte[]> changes = new ArrayList<>();
changes.addAll(txUserTable.getTxChanges());
changes.addAll(txPurchaseTable.getTxChanges());
transactionManager.canCommit(tx1, changes);
transactionManager.commit(tx1);
txUserTable.postTxCommit();
txPurchaseTable.postTxCommit();
try {
String command = String.format("insert into table %s select P.purchaseid, P.itemid, P.userid, P.ct, P.price, U.name, U.email from " + "%s P join %s U on (P.userid = U.id)", getDatasetHiveName(expandedTableID), getDatasetHiveName(purchaseTableID), getDatasetHiveName(userTableID));
ExploreExecutionResult result = exploreClient.submit(NAMESPACE_ID, command).get();
Assert.assertEquals(QueryStatus.OpStatus.FINISHED, result.getStatus().getStatus());
command = String.format("select purchaseid, itemid, userid, ct, price, username, email from %s", getDatasetHiveName(expandedTableID));
runCommand(NAMESPACE_ID, command, true, Lists.newArrayList(new ColumnDesc("purchaseid", "BIGINT", 1, null), new ColumnDesc("itemid", "STRING", 2, null), new ColumnDesc("userid", "STRING", 3, null), new ColumnDesc("ct", "INT", 4, null), new ColumnDesc("price", "DOUBLE", 5, null), new ColumnDesc("username", "STRING", 6, null), new ColumnDesc("email", "STRING", 7, null)), Lists.newArrayList(new QueryResult(Lists.<Object>newArrayList(1L, "scotch", "samuel", 1, 56.99d, "Samuel Jackson", "sjackson@gmail.com"))));
} finally {
datasetFramework.deleteInstance(userTableID);
datasetFramework.deleteInstance(purchaseTableID);
datasetFramework.deleteInstance(expandedTableID);
}
}
Aggregations