@Override public Status delete(String table, String key) { try { // firstly, retrieve the entity to be deleted TableOperation retrieveOp = TableOperation.retrieve(partitionKey, key, TableServiceEntity.class); TableServiceEntity entity = cloudTable.execute(retrieveOp).getResultAsType(); // secondly, delete the entity TableOperation deleteOp = TableOperation.delete(entity); cloudTable.execute(deleteOp); return Status.OK; } catch (Exception e) { return Status.ERROR; } }
private void create(String rowKey, String tableName, String state, Object type) throws IOException { try { CloudTable table = tableClient.getTableReference(tableName); String serializedJob = configuration.getMapper().writeValueAsString(type); DataWrapper wrapper = new DataWrapper( configuration.getPartitionKey(), rowKey, state, serializedJob); // job id used as key TableOperation insert = TableOperation.insert(wrapper); table.execute(insert); } catch (JsonProcessingException | StorageException | URISyntaxException e) { throw new IOException("Error creating data for rowKey: " + rowKey, e); } }
private Status insertOrUpdate(String key, Map<String, ByteIterator> values) { HashMap<String, EntityProperty> properties = new HashMap<String, EntityProperty>(); for (Entry<String, ByteIterator> entry : values.entrySet()) { String fieldName = entry.getKey(); byte[] fieldVal = entry.getValue().toArray(); properties.put(fieldName, new EntityProperty(fieldVal)); } DynamicTableEntity entity = new DynamicTableEntity(partitionKey, key, properties); TableOperation insertOrReplace = TableOperation.insertOrReplace(entity); try { cloudTable.execute(insertOrReplace); return Status.OK; } catch (Exception e) { return Status.ERROR; } }
private TableOperation getTableOperation(DynamicTableEntity entity) { TableOperation tableOpe = null; switch (actionData) { case Insert: tableOpe = TableOperation.insert(entity); break; case Insert_Or_Merge: tableOpe = TableOperation.insertOrMerge(entity); break; case Insert_Or_Replace: tableOpe = TableOperation.insertOrReplace(entity); break; case Merge: tableOpe = TableOperation.merge(entity); break; case Replace: tableOpe = TableOperation.replace(entity); break; case Delete: tableOpe = TableOperation.delete(entity); break; default: LOGGER.error("No specified operation for table"); } return tableOpe; }
ref.setRowKey(UUID.randomUUID().toString()); this.table.execute(TableOperation.insert(ref), options, null); String oldEtag = ref.getEtag(); this.table.execute(TableOperation.replace(ref), options, null); this.table.execute(TableOperation.delete(ref), options, null); fail(); } catch (TableServiceException ex) { TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), Class1.class), options, null); this.table.execute(TableOperation.delete(ref), options, null); this.table.execute(TableOperation.delete(ref), options, null); fail(); } catch (TableServiceException ex) {
@Test public void testBatchAddAllWithRetrieveShouldThrow() throws StorageException { ArrayList<TableOperation> ops = allOpsList(); TableBatchOperation batch = new TableBatchOperation(); // Insert entity to retrieve Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(baseEntity), null, null); ops.add(TableOperation.retrieve(baseEntity.getPartitionKey(), baseEntity.getRowKey(), Class1.class)); try { batch.addAll(ops); fail(SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH); } catch (IllegalArgumentException e) { assertEquals(SR.RETRIEVE_MUST_BE_ONLY_OPERATION_IN_BATCH, e.getMessage()); } }
private void testDelete(TableRequestOptions options) throws StorageException { Class1 ref = new Class1(); ref.setA("foo_A"); ref.setB("foo_B"); ref.setC("foo_C"); ref.setD(new byte[]{0, 1, 2}); ref.setPartitionKey("jxscl_odata"); ref.setRowKey(UUID.randomUUID().toString()); TableOperation op = TableOperation.insert(ref); this.table.execute(op, options, null); this.table.execute(TableOperation.delete(ref), options, null); TableResult res2 = this.table.execute( TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), Class1.class), options, null); assertTrue(res2.getResult() == null); }
private Status readEntity(String key, Map<String, ByteIterator> result) { try { // firstly, retrieve the entity to be deleted TableOperation retrieveOp = TableOperation.retrieve(partitionKey, key, DynamicTableEntity.class); DynamicTableEntity entity = cloudTable.execute(retrieveOp).getResultAsType(); HashMap<String, EntityProperty> properties = entity.getProperties(); for (Entry<String, EntityProperty> entry: properties.entrySet()) { String fieldName = entry.getKey(); ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray()); result.put(fieldName, fieldVal); } return Status.OK; } catch (Exception e) { return Status.ERROR; } }
this.table.execute(TableOperation.insert(ref)); TableResult res = this.table.execute( TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null); ref = res.getResultAsType(); this.table.execute(TableOperation.replace(ref), options, null); res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null); this.table.execute(TableOperation.replace(ref), options, null); res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null); this.table.execute(TableOperation.replace(ref), options, null); res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null); this.table.execute(TableOperation.replace(ref), options, null); res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null); this.table.execute(TableOperation.replace(ref), options, null); res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null);
protected void delete(String partitionKey, String rowKey) throws StorageException { table.execute(TableOperation.delete(new DynamicTableEntity(partitionKey, rowKey))); }
TableOperation put = TableOperation.insertOrReplace(entity); this.table.execute(put); TableOperation get = TableOperation.retrieve(partitionKey, rowKey, DateTestEntity.class); entity = this.table.execute(get).getResultAsType(); assertEquals(date.getTime(), entity.getDate().getTime()); put = TableOperation.insertOrReplace(entity); this.table.execute(put); get = TableOperation.retrieve(partitionKey, rowKey, DateTestEntity.class); final TableRequestOptions options = new TableRequestOptions(); options.setDateBackwardCompatibility(true); put = TableOperation.insertOrReplace(dynamicEntity); this.table.execute(put); get = TableOperation.retrieve(partitionKey, rowKey, DynamicTableEntity.class); dynamicEntity = this.table.execute(get).getResultAsType(); assertEquals(date.getTime(), dynamicEntity.getProperties().get(dateKey).getValueAsDate().getTime()); put = TableOperation.insertOrReplace(dynamicEntity); this.table.execute(put); get = TableOperation.retrieve(partitionKey, rowKey, DynamicTableEntity.class); options.setDateBackwardCompatibility(true); dynamicEntity = this.table.execute(get, options, null).getResultAsType();
@Test public void testBatchMergeFail() throws StorageException { TableRequestOptions options = new TableRequestOptions(); options.setTablePayloadFormat(TablePayloadFormat.Json); TableBatchOperation batch = new TableBatchOperation(); addInsertBatch(batch); // Insert entity to merge Class1 baseEntity = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(baseEntity), options, null); Class1 updatedEntity = TableTestHelper.generateRandomEntity("jxscl_odata"); updatedEntity.setPartitionKey(baseEntity.getPartitionKey()); updatedEntity.setRowKey(baseEntity.getRowKey()); updatedEntity.setEtag(baseEntity.getEtag()); this.table.execute(TableOperation.replace(updatedEntity), options, null); // add merge to fail addMergeToBatch(baseEntity, batch); try { this.table.execute(batch, options, null); fail(); } catch (TableServiceException ex) { assertEquals(ex.getMessage(), "Precondition Failed"); String errorAfterSemiColon = ex.getExtendedErrorInformation().getErrorMessage(); errorAfterSemiColon = errorAfterSemiColon.substring(errorAfterSemiColon.indexOf(":") + 1); assertTrue(errorAfterSemiColon .startsWith("The update condition specified in the request was not satisfied.")); assertEquals(ex.getErrorCode(), StorageErrorCodeStrings.UPDATE_CONDITION_NOT_SATISFIED); } }
baseEntity.setRowKey(UUID.randomUUID().toString()); this.table.execute(TableOperation.insert(baseEntity), options, null); secondEntity.setEtag(baseEntity.getEtag()); TableResult mergeResult = this.table.execute(TableOperation.merge(secondEntity), options, null); TableResult res2 = this.table.execute(TableOperation.retrieve(secondEntity.getPartitionKey(), secondEntity.getRowKey(), DynamicTableEntity.class), options, null); DynamicTableEntity mergedEntity = (DynamicTableEntity) res2.getResult();
private void testBatchRetrieve(TableRequestOptions options) throws StorageException { // insert entity Class1 ref = TableTestHelper.generateRandomEntity("jxscl_odata"); this.table.execute(TableOperation.insert(ref), options, null); TableBatchOperation batch = new TableBatchOperation(); batch.retrieve(ref.getPartitionKey(), ref.getRowKey(), ref.getClass()); ArrayList<TableResult> results = this.table.execute(batch, options, null); assertEquals(results.size(), 1); assertEquals(results.get(0).getHttpStatusCode(), HttpURLConnection.HTTP_OK); Class1 retrievedRef = results.get(0).getResultAsType(); assertEquals(ref.getA(), retrievedRef.getA()); assertEquals(ref.getB(), retrievedRef.getB()); assertEquals(ref.getC(), retrievedRef.getC()); assertTrue(Arrays.equals(ref.getD(), retrievedRef.getD())); this.table.execute(TableOperation.delete(ref), options, null); }
TableResult insertResult = this.table.execute(TableOperation.insertOrMerge(baseEntity), options, null); this.table.execute(TableOperation.insertOrMerge(secondEntity), options, null); .execute(TableOperation.retrieve(baseEntity.getPartitionKey(), baseEntity.getRowKey(), DynamicTableEntity.class), options, null);
/** * Updates the liveness value of a particular processor with a randomly generated integer, which in turn updates the last modified since timestamp of the row. * @param jmVersion Job model version of the processor row to be updated. * @param pid Unique processor ID of the processor row to be updated. */ public void updateHeartbeat(String jmVersion, String pid) { try { TableOperation retrieveEntity = TableOperation.retrieve(jmVersion, pid, ProcessorEntity.class); ProcessorEntity entity = table.execute(retrieveEntity).getResultAsType(); entity.updateLiveness(); TableOperation update = TableOperation.replace(entity); table.execute(update); } catch (StorageException e) { LOG.error("Azure storage exception while updating heartbeat for job model version: " + jmVersion + "and pid: " + pid, e); } }
/** * Illustrates how to form and execute an upsert operation. * * @throws StorageException */ public void BasicUpsert() throws StorageException { // Retrieve the entity with partition key of "Smith" and row key of // "Jeff". TableOperation retrieveSmithJeff = TableOperation.retrieve("Smith", "Jeff", CustomerEntity.class); // Submit the operation to the table service and get the specific // entity. CustomerEntity specificEntity = table.execute(retrieveSmithJeff) .getResultAsType(); // Specify a new phone number. specificEntity.setPhoneNumber("425-555-0105"); // Create an operation to replace the entity. TableOperation replaceEntity = TableOperation.merge(specificEntity); // Submit the operation to the table service. table.execute(replaceEntity); }
/** * Adds a table operation to replace the specified entity to the batch operation. * * @param entity * The {@link TableEntity} to replace. */ public void replace(final TableEntity entity) { this.lockToPartitionKey(entity.getPartitionKey()); this.add(TableOperation.replace(entity)); }
/** * Adds a table operation to merge the specified entity to the batch operation. * * @param entity * The {@link TableEntity} to merge. */ public void merge(final TableEntity entity) { this.lockToPartitionKey(entity.getPartitionKey()); this.add(TableOperation.merge(entity)); }
/** * Adds a table operation to insert or merge the specified entity to the batch operation. * * @param entity * The {@link TableEntity} to insert if not found or to merge if it exists. */ public void insertOrMerge(final TableEntity entity) { this.lockToPartitionKey(entity.getPartitionKey()); this.add(TableOperation.insertOrMerge(entity)); }