@Test public void useTransactionTest() throws Exception { // Performing admin operations to create dataset instance // keyValueTable is a system dataset module Id.DatasetInstance myTableInstance = Id.DatasetInstance.from(namespace, "myTable"); dsFramework.addInstance("keyValueTable", myTableInstance, DatasetProperties.EMPTY); Assert.assertTrue(feedManager.createFeed(FEED1)); try { Cancellable cancellable = notificationService.subscribe( FEED1, new NotificationHandler<String>() { private int received = 0; @Override public Type getNotificationType() { return String.class; } @Override public void received( final String notification, NotificationContext notificationContext) { notificationContext.execute( new TxRunnable() { @Override public void run(DatasetContext context) throws Exception { KeyValueTable table = context.getDataset("myTable"); table.write("foo", String.format("%s-%d", notification, received++)); } }, TxRetryPolicy.maxRetries(5)); } }); TimeUnit.SECONDS.sleep(2); try { notificationService.publish(FEED1, "foobar"); // Waiting for the subscriber to receive that notification TimeUnit.SECONDS.sleep(2); KeyValueTable table = dsFramework.getDataset(myTableInstance, DatasetDefinition.NO_ARGUMENTS, null); Assert.assertNotNull(table); Transaction tx1 = txManager.startShort(100); table.startTx(tx1); Assert.assertEquals("foobar-0", Bytes.toString(table.read("foo"))); Assert.assertTrue(table.commitTx()); txManager.canCommit(tx1, table.getTxChanges()); txManager.commit(tx1); table.postTxCommit(); } finally { cancellable.cancel(); } } finally { dsFramework.deleteInstance(myTableInstance); feedManager.deleteFeed(FEED1); } }
/** * Returns the top words associated with the specified word and the number of times the words have * appeared together. * * @param word the word of interest * @param limit the number of associations to return, at most * @return a map of the top associated words to their co-occurrence count */ public Map<String, Long> readWordAssocs(String word, int limit) { // Retrieve all columns of the word’s row Row result = this.table.get(new Get(word)); TopKCollector collector = new TopKCollector(limit); if (!result.isEmpty()) { // Iterate over all columns for (Map.Entry<byte[], byte[]> entry : result.getColumns().entrySet()) { collector.add(Bytes.toLong(entry.getValue()), Bytes.toString(entry.getKey())); } } return collector.getTopK(); }
public KeyValue.Value get(String key) throws Exception { return GSON.fromJson( Bytes.toString(table.get(Bytes.toBytes(key), COL)), KeyValue.Value.class); }
@Override public KeyValue makeRecord(byte[] key, Row row) { return new KeyValue( Bytes.toInt(key), GSON.fromJson(Bytes.toString(row.get(KeyValueTable.COL)), String.class)); }
public String get(int key) throws Exception { return Bytes.toString(table.get(Bytes.toBytes(key), COL)); }