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; } }
private void remove(UUID jobId, String tableName) throws IOException { try { CloudTable table = tableClient.getTableReference(tableName); TableOperation retrieve = TableOperation.retrieve( configuration.getPartitionKey(), jobId.toString(), DataWrapper.class); TableResult result = table.execute(retrieve); DataWrapper wrapper = result.getResultAsType(); TableOperation delete = TableOperation.delete(wrapper); table.execute(delete); } catch (StorageException | URISyntaxException e) { throw new IOException("Error removing data for job: " + jobId, e); } }
@Override public PortabilityJob findJob(UUID jobId) { Preconditions.checkNotNull(jobId, "Job id is null"); try { CloudTable table = tableClient.getTableReference(JOB_TABLE); TableOperation retrieve = TableOperation.retrieve( configuration.getPartitionKey(), jobId.toString(), DataWrapper.class); TableResult result = table.execute(retrieve); DataWrapper wrapper = result.getResultAsType(); return configuration.getMapper().readValue(wrapper.getSerialized(), PortabilityJob.class); } catch (StorageException | URISyntaxException | IOException e) { throw new MicrosoftStorageException("Error finding job: " + jobId, e); } }
@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 <T> T find(Class<T> type, String rowKey, String tableName) { try { CloudTable table = tableClient.getTableReference(tableName); TableOperation retrieve = TableOperation.retrieve( configuration.getPartitionKey(), rowKey, DataWrapper.class); TableResult result = table.execute(retrieve); DataWrapper wrapper = result.getResultAsType(); return configuration.getMapper().readValue(wrapper.getSerialized(), type); } catch (StorageException | IOException | URISyntaxException e) { throw new MicrosoftStorageException("Error finding data for rowKey: " + rowKey, e); } }
protected Optional<DynamicTableEntity> retrieve(String partitionKey, String rowKey) throws StorageException { DynamicTableEntity data = table .execute(TableOperation.retrieve(partitionKey, rowKey, DynamicTableEntity.class)) .getResultAsType(); return Optional.ofNullable(data); }
public Date getBlobDataTracker(long id,String volid) throws StorageException { TableOperation tro = TableOperation.retrieve(volid, Long.toString(id), BlobDataTracker.class); BlobDataTracker btr = cloudTable.execute(tro).getResultAsType(); return btr.getTimestamp(); }
/** * Retrieve a particular row in the processor table, given the partition key and the row key. * @param jmVersion Job model version of the processor row to be retrieved. * @param pid Unique processor ID of the processor row to be retrieved. * @return An instance of required processor entity. Null if does not exist. * @throws AzureException If an Azure storage service error occurred. */ public ProcessorEntity getEntity(String jmVersion, String pid) { try { TableOperation retrieveEntity = TableOperation.retrieve(jmVersion, pid, ProcessorEntity.class); return table.execute(retrieveEntity).getResultAsType(); } catch (StorageException e) { LOG.error("Azure storage exception while retrieving processor entity with job model version: " + jmVersion + "and pid: " + pid, e); throw new AzureException(e); } }
public void removeBlobDataTracker(long id,String volid) throws StorageException { TableOperation to = TableOperation.retrieve(volid, Long.toString(id), BlobDataTracker.class); BlobDataTracker btr = cloudTable.execute(to).getResultAsType(); if(btr != null) { TableOperation deleteBtr = TableOperation.delete(btr); // Submit the delete operation to the table service. cloudTable.execute(deleteBtr); } }
public <EntityType extends TableServiceEntity> Optional<EntityType> retrieve(TableKey tableKey) { try { return Optional.ofNullable(tableRefrence(tableKey.getTable()) .execute(TableOperation.retrieve(tableKey.getPartitionKey(), tableKey.getRowKey(), tableKey.getEntityClass())) .getResultAsType()); } catch (StorageException e) { LOG.warn("Error retrieving entity from table: {}", tableKey.getTable(), e); } return Optional.empty(); }
/** * 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 entity delete operation. * * @throws StorageException */ public void BasicDeleteEntity() throws StorageException { // Create an operation to retrieve the entity with partition key of // "Smith" and row key of "Jeff". TableOperation retrieveSmithJeff = TableOperation.retrieve("Smith", "Jeff", CustomerEntity.class); // Retrieve the entity with partition key of "Smith" and row key of // "Jeff". CustomerEntity entitySmithJeff = table.execute(retrieveSmithJeff) .getResultAsType(); // Create an operation to delete the entity. TableOperation deleteSmithJeff = TableOperation.delete(entitySmithJeff); // Submit the delete operation to the table service. table.execute(deleteSmithJeff); }
/** * Updates the isLeader value when the processor starts or stops being a leader. * @param jmVersion Job model version of the processor row to be updated. * @param pid Unique processor ID of the processor row to be updated. * @param isLeader Denotes whether the processor is a leader or not. * @throws AzureException If an Azure storage service error occurred. */ public void updateIsLeader(String jmVersion, String pid, boolean isLeader) { try { TableOperation retrieveEntity = TableOperation.retrieve(jmVersion, pid, ProcessorEntity.class); ProcessorEntity entity = table.execute(retrieveEntity).getResultAsType(); entity.setIsLeader(isLeader); TableOperation update = TableOperation.replace(entity); table.execute(update); } catch (StorageException e) { LOG.error("Azure storage exception while updating isLeader value for job model version: " + jmVersion + "and pid: " + pid, e); throw new AzureException(e); } }
private void testComplexEntityInsert(TableRequestOptions options, boolean usePropertyResolver) throws StorageException { ComplexEntity ref = new ComplexEntity(); ref.setPartitionKey("jxscl_odata"); ref.setRowKey(UUID.randomUUID().toString()); ref.populateEntity(); if (usePropertyResolver) { options.setPropertyResolver(ref); } this.table.execute(TableOperation.insert(ref), options, null); TableResult res = this.table.execute( TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class), options, null); ComplexEntity retrievedComplexRef = res.getResultAsType(); ref.assertEquality(retrievedComplexRef); }
private void testDoubles(TableRequestOptions options, boolean usePropertyResolver) throws StorageException { StrangeDoubles ref = new StrangeDoubles(); ref.setPartitionKey("jxscl_odata"); ref.setRowKey(UUID.randomUUID().toString()); ref.populateEntity(); if (usePropertyResolver) { options.setPropertyResolver(ref); } // try with pojo this.table.execute(TableOperation.insert(ref), options, null); TableResult res = this.table.execute( TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), StrangeDoubles.class), options, null); StrangeDoubles retrievedComplexRef = res.getResultAsType(); ref.assertEquality(retrievedComplexRef); }
@NotNull public TableEntity updateTableEntity(@NotNull StorageAccount storageAccount, @NotNull TableEntity tableEntity) throws AzureCmdException { try { CloudTableClient client = getCloudTableClient(storageAccount); CloudTable cloudTable = client.getTableReference(tableEntity.getTableName()); DynamicTableEntity entity = getDynamicTableEntity(tableEntity); TableRequestOptions tro = new TableRequestOptions(); tro.setTablePayloadFormat(TablePayloadFormat.JsonFullMetadata); TableResult result = cloudTable.execute(TableOperation.replace(entity), tro, null); DynamicTableEntity resultEntity = result.getResultAsType(); return getTableEntity(tableEntity.getTableName(), resultEntity); } catch (Throwable t) { throw new AzureCmdException("Error updating the Table Entity", t); } }
@Test public void testInvalidStoreAsAnnotation() throws StorageException { InvalidStoreAsEntity ref = new InvalidStoreAsEntity(); ref.setPartitionKey("jxscl_odata"); ref.setRowKey(UUID.randomUUID().toString()); ref.setStoreAsString("StoreAsOverride Data"); ref.populateEntity(); this.table.execute(TableOperation.insert(ref)); TableResult res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), InvalidStoreAsEntity.class)); InvalidStoreAsEntity retrievedStoreAsRef = res.getResultAsType(); assertEquals(retrievedStoreAsRef.getStoreAsString(), null); }
@NotNull public TableEntity createTableEntity(@NotNull StorageAccount storageAccount, @NotNull String tableName, @NotNull String partitionKey, @NotNull String rowKey, @NotNull Map<String, Property> properties) throws AzureCmdException { try { CloudTableClient client = getCloudTableClient(storageAccount); CloudTable cloudTable = client.getTableReference(tableName); DynamicTableEntity entity = getDynamicTableEntity(partitionKey, rowKey, properties); TableRequestOptions tro = new TableRequestOptions(); tro.setTablePayloadFormat(TablePayloadFormat.JsonFullMetadata); TableResult result = cloudTable.execute(TableOperation.insert(entity, true), tro, null); DynamicTableEntity resultEntity = result.getResultAsType(); return getTableEntity(tableName, resultEntity); } catch (Throwable t) { throw new AzureCmdException("Error creating the Table Entity", t); } }
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); }
@Test public void testStoreAsAnnotation() throws StorageException { StoreAsEntity ref = new StoreAsEntity(); ref.setPartitionKey("jxscl_odata"); ref.setRowKey(UUID.randomUUID().toString()); ref.setStoreAsString("StoreAsOverride Data"); ref.populateEntity(); this.table.execute(TableOperation.insert(ref)); TableResult res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), StoreAsEntity.class)); StoreAsEntity retrievedStoreAsRef = res.getResultAsType(); assertEquals(retrievedStoreAsRef.getStoreAsString(), ref.getStoreAsString()); // Same query with a class without the storeAs annotation res = this.table.execute(TableOperation.retrieve(ref.getPartitionKey(), ref.getRowKey(), ComplexEntity.class)); ComplexEntity retrievedComplexRef = res.getResultAsType(); assertEquals(retrievedComplexRef.getString(), ref.getStoreAsString()); this.table.execute(TableOperation.delete(retrievedComplexRef)); }