@Override public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { try { String whereStr = String.format("(PartitionKey eq '%s') and (RowKey ge '%s')", partitionKey, startkey); TableQuery<DynamicTableEntity> scanQuery = new TableQuery<DynamicTableEntity>(DynamicTableEntity.class) .where(whereStr).take(recordcount); int cnt = 0; for (DynamicTableEntity entity : cloudTable.execute(scanQuery)) { HashMap<String, EntityProperty> properties = entity.getProperties(); HashMap<String, ByteIterator> cur = new HashMap<String, ByteIterator>(); for (Entry<String, EntityProperty> entry : properties.entrySet()) { String fieldName = entry.getKey(); ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray()); if (fields == null || fields.contains(fieldName)) { cur.put(fieldName, fieldVal); } } result.add(cur); if (++cnt == recordcount) { break; } } return Status.OK; } catch (Exception e) { return Status.ERROR; } }
@Override public UUID findFirst(JobAuthorization.State jobState) { try { String partitionFilter = generateFilterCondition( "PartitionKey", TableQuery.QueryComparisons.EQUAL, configuration.getPartitionKey()); String stateFilter = generateFilterCondition( "State", TableQuery.QueryComparisons.EQUAL, jobState.name()); // properties are converted to capitalized by the storage API String combinedFilter = TableQuery.combineFilters(partitionFilter, TableQuery.Operators.AND, stateFilter); TableQuery<DataWrapper> query = TableQuery.from(DataWrapper.class).where(combinedFilter).take(1); CloudTable table = tableClient.getTableReference(JOB_TABLE); Iterator<DataWrapper> iter = table.execute(query).iterator(); if (!iter.hasNext()) { return null; } return UUID.fromString(iter.next().getRowKey()); } catch (StorageException | URISyntaxException e) { throw new MicrosoftStorageException("Error finding first job", e); } }
@Override public Optional<User> getUserForTimLocalId(String userId) throws AuthenticationUnavailableException { String query = "(PartitionKey eq 'users') and (id eq '" + userId + "')"; Iterator<DynamicTableEntity> users = table.execute(TableQuery.from(DynamicTableEntity.class) .where(query) .take(2)).iterator(); if (users.hasNext()) { Optional<User> result = Optional.of(propsToObject(users.next())); if (users.hasNext()) { LOG.error("Multiple items found for query " + query); } return result; } else { return Optional.empty(); } } }
@Override public Schema getEndpointSchema(RuntimeContainer container, String schemaName) throws IOException { try { AzureStorageTableService tableService = new AzureStorageTableService(getAzureConnection(container)); TableQuery<DynamicTableEntity> partitionQuery; partitionQuery = TableQuery.from(DynamicTableEntity.class).take(1); Iterable<DynamicTableEntity> entities = tableService.executeQuery(schemaName, partitionQuery); if (entities.iterator().hasNext()) { DynamicTableEntity result = entities.iterator().next(); return AzureStorageAvroRegistry.get().inferSchema(result); } else { return null; } } catch (InvalidKeyException | URISyntaxException | StorageException e) { LOGGER.error(e.getLocalizedMessage()); throw new ComponentException(e); } } }
@Override public Schema getEndpointSchema(RuntimeContainer container, String schemaName) throws IOException { try { AzureStorageTableService tableService = new AzureStorageTableService(getAzureConnection(container)); TableQuery<DynamicTableEntity> partitionQuery; partitionQuery = TableQuery.from(DynamicTableEntity.class).take(1); Iterable<DynamicTableEntity> entities = tableService.executeQuery(schemaName, partitionQuery); if (entities.iterator().hasNext()) { DynamicTableEntity result = entities.iterator().next(); return AzureStorageAvroRegistry.get().inferSchema(result); } else { return null; } } catch (InvalidKeyException | URISyntaxException | StorageException e) { LOGGER.error(e.getLocalizedMessage()); throw new ComponentException(e); } } }
@Test public void testQueryWithInvalidTakeCount() { try { TableQuery.from(TableServiceEntity.class).take(0); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), "Take count must be positive and greater than 0."); } try { TableQuery.from(TableServiceEntity.class).take(-1); } catch (IllegalArgumentException ex) { assertEquals(ex.getMessage(), "Take count must be positive and greater than 0."); } }
this.generateListTablesQuery(prefix).take(maxResults), this.tableNameResolver, continuationToken, options, opContext);
modifiedOptions.clearEncryption(); return (ResultSegment<String>) this.executeQuerySegmentedImpl( this.generateListTablesQuery(prefix).take(maxResults), this.tableNameResolver, continuationToken, modifiedOptions, opContext);
final Iterable<DynamicTableEntity> result = table.execute(TableQuery.from(DynamicTableEntity.class).take(50), options, null);
private void testTableQueryWithTake(TableRequestOptions options, boolean usePropertyResolver) throws StorageException { // Create entity to check against Class1 randEnt = TableTestHelper.generateRandomEntity(null); if (usePropertyResolver) { options.setPropertyResolver(randEnt); } final ResultSegment<Class1> result = table.executeSegmented( TableQuery.from(Class1.class).select(new String[] { "A", "C" }).take(25), null, options, null); int count = 0; // Validate results for (Class1 ent : result.getResults()) { count++; assertEquals(ent.getA(), randEnt.getA()); assertEquals(ent.getB(), null); assertEquals(ent.getC(), randEnt.getC()); assertEquals(ent.getD(), null); } assertEquals(count, 25); }