/** * Read the value for a given key. * * @param key the key to read for * @return the value for that key, or null if no value was found */ @ReadOnly @Nullable public byte[] read(String key) { return read(Bytes.toBytes(key)); }
@Override public void delete(final ProgramId serviceId) throws NotFoundException { Transactionals.execute(transactional, context -> { byte[] key = Bytes.toBytes(ServiceDiscoverable.getName(serviceId)); KeyValueTable kvTable = getRouteTable(context); if (kvTable.read(key) == null) { throw new NotFoundException(String.format("Route Config for Service %s was not found.", serviceId)); } kvTable.delete(key); }, NotFoundException.class); }
@Override public void apply() { // the table should not have initialized=true kvTable.write("initialized", "false"); } });
final DataSetManager<KeyValueTable> datasetManager = getDataset(ServiceLifecycleApp.HANDLER_TABLE_NAME); datasetManager.get().delete(Bytes.toBytes("called")); datasetManager.get().delete(Bytes.toBytes("completed")); datasetManager.flush(); byte[] value = datasetManager.get().read("called"); datasetManager.flush(); return value != null && value.length == Bytes.SIZEOF_LONG && Bytes.toLong(value) > 5; datasetManager.get().write("completed", Bytes.toBytes(true)); datasetManager.flush();
@Override public void apply() throws Exception { // write a value and read it back kvTable.write(KEY1, VAL1); Assert.assertArrayEquals(VAL1, kvTable.read(KEY1)); // update the value and read it back kvTable.write(KEY1, VAL2); Assert.assertArrayEquals(VAL2, kvTable.read(KEY1)); // attempt to swap, expecting old value Assert.assertFalse(kvTable.compareAndSwap(KEY1, VAL1, VAL3)); Assert.assertArrayEquals(VAL2, kvTable.read(KEY1)); // swap the value and read it back Assert.assertTrue(kvTable.compareAndSwap(KEY1, VAL2, VAL3)); Assert.assertArrayEquals(VAL3, kvTable.read(KEY1)); // delete the value and verify its gone kvTable.delete(KEY1); Assert.assertNull(kvTable.read(KEY1)); } });
try (CloseableIterator<KeyValue<byte[], byte[]>> filesIterable = table.scan(null, null)) { while (filesIterable.hasNext()) { KeyValue<byte[], byte[]> file = filesIterable.next(); Date processedDate = new Date(time); if (processedDate.before(expiryDate)) { table.delete(file.getKey()); } else { processedFiles.add(new String(file.getKey(), Charsets.UTF_8));
@Override public void run(DatasetContext context) throws Exception { int uniqueWordCount = 0; try (CloseableIterator<KeyValue<byte[], byte[]>> scanner = wordCount.scan(null, null)) { while (scanner.hasNext()) { scanner.next(); uniqueWordCount++; } } result.write("UniqueWordCount", String.valueOf(uniqueWordCount)); metrics.gauge("unique.words", uniqueWordCount); } });
@WriteOnly @Override public void delete(byte[] key) { kvTable.delete(key); }
@ReadOnly @Override public CloseableIterator<KeyValue<byte[], T>> scan(byte[] startRow, byte[] stopRow) { final CloseableIterator<KeyValue<byte[], byte[]>> keyValueIterator = kvTable.scan(startRow, stopRow); return new AbstractCloseableIterator<KeyValue<byte[], T>>() { boolean closed = false; @Override protected KeyValue<byte[], T> computeNext() { Preconditions.checkState(!closed); if (keyValueIterator.hasNext()) { KeyValue<byte[], byte[]> row = keyValueIterator.next(); return new KeyValue<>(row.getKey(), decode(row.getValue())); } close(); return null; } @Override public void close() { keyValueIterator.close(); endOfData(); closed = true; } }; }
@Override public void apply() throws Exception { // write a swap, this should fail Assert.assertFalse(kvTable.compareAndSwap(KEY2, VAL1, VAL3)); Assert.assertArrayEquals(VAL2, kvTable.read(KEY2)); } });
@Override public List<Split> getSplits() { return table.getSplits(); }
@Override public void initialize() throws Exception { MapReduceContext context = getContext(); Job job = context.getHadoopJob(); job.setMapperClass(TokenMapper.class); job.setNumReduceTasks(0); String inputText = getContext().getRuntimeArguments().get("input.text"); Preconditions.checkNotNull(inputText); KeyValueTable kvTable = getContext().getDataset(INPUT); kvTable.write("key", inputText); context.addInput(Input.ofDataset(INPUT, kvTable.getSplits(1, null, null))); String outputDatasets = getContext().getRuntimeArguments().get("output.datasets"); outputDatasets = outputDatasets != null ? outputDatasets : PFS; for (String outputName : outputDatasets.split(",")) { String outputPartition = getContext().getRuntimeArguments().get(outputName + ".output.partition"); PartitionKey outputPartitionKey = outputPartition == null ? null : PartitionKey.builder().addField("number", Integer.parseInt(outputPartition)).build(); Map<String, String> outputArguments = new HashMap<>(); if (outputPartitionKey != null) { PartitionedFileSetArguments.setOutputPartitionKey(outputArguments, outputPartitionKey); } else { PartitionedFileSetArguments.setDynamicPartitioner(outputArguments, KeyPartitioner.class); } context.addOutput(Output.ofDataset(outputName, outputArguments)); } }
@Override public SplitReader<byte[], byte[]> createSplitReader(Split split) { return table.createSplitReader(split); } }
Transactions.execute(datasetCache.newTransactionContext(), "countersVerify", () -> { KeyValueTable counters = datasetCache.getDataset(counterTableName); counters.delete(AppWithMapReduceUsingRuntimeDatasets.INPUT_RECORDS); counters.delete(AppWithMapReduceUsingRuntimeDatasets.REDUCE_KEYS); }); KeyValueTable counters = datasetCache.getDataset(counterTableName); Assert.assertEquals(totalInputRecords, counters.incrementAndGet(AppWithMapReduceUsingRuntimeDatasets.INPUT_RECORDS, 0L)); Assert.assertEquals(1L, counters.incrementAndGet(AppWithMapReduceUsingRuntimeDatasets.REDUCE_KEYS, 0L)); });
final DataSetManager<KeyValueTable> datasetManager = getDataset(ServiceLifecycleApp.HANDLER_TABLE_NAME); datasetManager.get().delete(Bytes.toBytes("called")); datasetManager.get().delete(Bytes.toBytes("completed")); datasetManager.flush(); byte[] value = datasetManager.get().read("called"); datasetManager.flush(); return value != null && value.length == Bytes.SIZEOF_LONG && Bytes.toLong(value) > 5; datasetManager.get().write("completed", Bytes.toBytes(true)); datasetManager.flush();
try (CloseableIterator<KeyValue<byte[], byte[]>> iterator = processedFileTrackingTable.scan(null, null)) { while (iterator.hasNext()) { KeyValue<byte[], byte[]> keyValue = iterator.next(); if (config.tableExpiryPeriod != null && config.tableExpiryPeriod > 0 && expiryDate != null && processedDate.before(expiryDate)) { processedFileTrackingTable.delete(keyValue.getKey()); } else { processedFiles.add(Bytes.toString(keyValue.getKey()));
@Path("/write/{key}/{value}/{sleep}") @GET public void handler(HttpServiceRequest request, HttpServiceResponder responder, @PathParam("key") String key, @PathParam("value") String value, @PathParam("sleep") int sleep) throws InterruptedException { KeyValueTable table = getContext().getDataset(TRANSACTIONS_DATASET_NAME); //Check if data written in initialize method is persisted. Preconditions.checkArgument(Bytes.toString(table.read(INIT_KEY)).equals(VALUE)); table.write(key, value); Thread.sleep(sleep); responder.sendStatus(200); }
@Override public void apply() throws Exception { // delete the value kvTable.delete(KEY2); } });