Search in sources :

Example 6 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project jcabi-dynamo by jcabi.

the class QueryValveTest method fetchesData.

/**
 * QueryValve can fetch data from AWS.
 * @throws Exception If some problem inside
 */
@Test
@SuppressWarnings("unchecked")
public void fetchesData() throws Exception {
    final Valve valve = new QueryValve();
    final Credentials credentials = Mockito.mock(Credentials.class);
    final ImmutableMap<String, AttributeValue> item = new ImmutableMap.Builder<String, AttributeValue>().build();
    final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
    Mockito.doReturn(aws).when(credentials).aws();
    Mockito.doReturn(new QueryResult().withItems(Collections.<Map<String, AttributeValue>>singletonList(item)).withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).query(Mockito.any(QueryRequest.class));
    final Dosage dosage = valve.fetch(credentials, "table", new Conditions(), new ArrayList<String>(0));
    MatcherAssert.assertThat(dosage.hasNext(), Matchers.is(false));
    MatcherAssert.assertThat(dosage.items(), Matchers.hasItem(item));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ImmutableMap(com.google.common.collect.ImmutableMap) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) Test(org.junit.Test)

Example 7 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project jcabi-dynamo by jcabi.

the class QueryValve method fetch.

// @checkstyle ParameterNumber (5 lines)
@Override
public Dosage fetch(final Credentials credentials, final String table, final Map<String, Condition> conditions, final Collection<String> keys) throws IOException {
    final AmazonDynamoDB aws = credentials.aws();
    try {
        final Collection<String> attrs = new HashSet<String>(Arrays.asList(this.attributes));
        attrs.addAll(keys);
        QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withScanIndexForward(this.forward).withSelect(this.select).withLimit(this.limit);
        if (this.select.equals(Select.SPECIFIC_ATTRIBUTES.toString())) {
            request = request.withAttributesToGet(attrs);
        }
        if (!this.index.isEmpty()) {
            request = request.withIndexName(this.index);
        }
        final long start = System.currentTimeMillis();
        final QueryResult result = aws.query(request);
        Logger.info(this, // @checkstyle LineLength (1 line)
        "#items(): loaded %d item(s) from '%s' and stopped at %s, using %s, %s, in %[ms]s", result.getCount(), table, result.getLastEvaluatedKey(), conditions, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        return new QueryValve.NextDosage(credentials, request, result);
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to fetch from \"%s\" by %s and %s", table, conditions, keys), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 8 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project java-sdk by watson-developer-cloud.

the class DiscoveryV2Example method main.

public static void main(String[] args) throws IOException {
    Authenticator authenticator = new BearerTokenAuthenticator("{bearer_token}");
    Discovery service = new Discovery("2019-11-22", authenticator);
    service.setServiceUrl("{url}");
    // This example assumes you have a project and collection set up which can accept documents.
    // Paste those
    // IDs below.
    String projectId = "";
    String collectionId = "";
    // Add a new document to our collection. Fill in the file path with the file you want to send.
    InputStream file = new FileInputStream("");
    AddDocumentOptions addDocumentOptions = new AddDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).file(file).filename("example-file").build();
    DocumentAccepted addResponse = service.addDocument(addDocumentOptions).execute().getResult();
    String documentId = addResponse.getDocumentId();
    // Query your collection with the new document inside.
    QueryOptions queryOptions = new QueryOptions.Builder().projectId(projectId).addCollectionIds(collectionId).naturalLanguageQuery(// Feel free to replace this to query something different.
    "Watson").build();
    QueryResponse queryResponse = service.query(queryOptions).execute().getResult();
    System.out.println(queryResponse.getMatchingResults() + " results were returned by the query!");
    // See if the added document got returned by the query.
    for (QueryResult result : queryResponse.getResults()) {
        if (result.getDocumentId().equals(documentId)) {
            System.out.println("Our new document matched the query!");
        }
    }
    // Delete our uploaded document from the collection.
    DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).documentId(documentId).build();
    service.deleteDocument(deleteDocumentOptions).execute();
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QueryOptions(com.ibm.watson.discovery.v2.model.QueryOptions) FileInputStream(java.io.FileInputStream) QueryResult(com.ibm.watson.discovery.v2.model.QueryResult) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) QueryResponse(com.ibm.watson.discovery.v2.model.QueryResponse) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) BearerTokenAuthenticator(com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator) DeleteDocumentOptions(com.ibm.watson.discovery.v2.model.DeleteDocumentOptions)

Example 9 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project geowave by locationtech.

the class AsyncPaginatedQuery method nextIterator.

/**
 * Get the next query data If the last request is equal to null then we have no more query
 * requests to fire
 *
 * <p> If asyncQueryResults is not empty, we have already fetched the next query data that can be
 * read immediately
 *
 * <p> If due to max async query limit, we couldn't fire async requests, we fire the request now
 */
@Override
protected Iterator<? extends Map<String, AttributeValue>> nextIterator(final int arg0) {
    synchronized (monitorLock) {
        if ((lastRequest == null) && asyncQueryResults.isEmpty()) {
            return null;
        }
        QueryResult result = null;
        if ((lastRequest != null) && (asyncRequestsInProgress == 0)) {
            makeAsyncQuery();
        }
        while (asyncQueryResults.isEmpty()) {
            try {
                monitorLock.wait();
            } catch (final InterruptedException e) {
                LOGGER.error("Exception in Async paginated query " + e);
                e.printStackTrace();
            }
        }
        result = asyncQueryResults.remove();
        return result == null ? null : result.getItems().iterator();
    }
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult)

Example 10 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-sdk-android by aws-amplify.

the class Search method getNextQueryResultSet.

private List<Document> getNextQueryResultSet() {
    final List<Document> returnValue = new ArrayList<Document>();
    final QueryRequest request = new QueryRequest();
    request.withExclusiveStartKey(nextKey).withAttributesToGet(attributesToGet).withLimit(limit).withTableName(tableName).withConsistentRead(isConsistentRead).withIndexName(this.indexName);
    if (select != null) {
        request.withSelect(select);
    }
    Expression.applyExpression(request, table, keyExpression, filterExpression);
    if (this.filter != null) {
        final Map<String, Condition> keyConditions = getKeyConditions((QueryFilter) this.filter, request.getIndexName());
        final Map<String, Condition> filterConditions = getFilterConditions((QueryFilter) this.filter, request.getIndexName());
        if (!keyConditions.isEmpty()) {
            request.withKeyConditions(keyConditions);
        }
        if (!filterConditions.isEmpty()) {
            request.withQueryFilter(filterConditions);
        }
    } else {
        request.withKeyConditions(null).withQueryFilter(null);
    }
    if (request.getQueryFilter() != null && request.getQueryFilter().size() > 1) {
        request.withConditionalOperator(this.conditionalOperator);
    } else {
        request.withConditionalOperator((String) null);
    }
    Table.appendDynamoDBDocumentUserAgentString(request);
    final QueryResult result = table.getClient().query(request);
    for (final Map<String, AttributeValue> item : result.getItems()) {
        final Document doc = Document.fromAttributeMap(item);
        returnValue.add(doc);
        if (this.collectResults) {
            this.matches.add(doc);
        }
    }
    nextKey = result.getLastEvaluatedKey();
    if (nextKey == null || nextKey.size() == 0) {
        isDone = true;
    }
    return returnValue;
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) ArrayList(java.util.ArrayList) Document(com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)

Aggregations

QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)21 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)19 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 HashMap (java.util.HashMap)9 Condition (com.amazonaws.services.dynamodbv2.model.Condition)8 Map (java.util.Map)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)4 AmazonClientException (com.amazonaws.AmazonClientException)2 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)2 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)2 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 Document (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)1 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)1 ComparisonOperator (com.amazonaws.services.dynamodbv2.model.ComparisonOperator)1 ImmutableMap (com.google.common.collect.ImmutableMap)1