Search in sources :

Example 11 with Datastore

use of com.google.datastore.v1.client.Datastore in project beam by apache.

the class V1TestUtil method countEntities.

/**
   * Returns the total number of entities for the given datastore.
   */
static long countEntities(V1TestOptions options, String project, String ancestor) throws Exception {
    // Read from datastore.
    Datastore datastore = V1TestUtil.getDatastore(options, project);
    Query query = V1TestUtil.makeAncestorKindQuery(options.getKind(), options.getNamespace(), ancestor);
    V1TestReader reader = new V1TestReader(datastore, query, options.getNamespace());
    long numEntitiesRead = 0;
    while (reader.advance()) {
        reader.getCurrent();
        numEntitiesRead++;
    }
    return numEntitiesRead;
}
Also used : Datastore(com.google.datastore.v1.client.Datastore) Query(com.google.datastore.v1.Query)

Example 12 with Datastore

use of com.google.datastore.v1.client.Datastore in project beam by apache.

the class V1TestUtil method deleteAllEntities.

/**
   * Delete all entities with the given ancestor.
   */
static void deleteAllEntities(V1TestOptions options, String project, String ancestor) throws Exception {
    Datastore datastore = getDatastore(options, project);
    Query query = V1TestUtil.makeAncestorKindQuery(options.getKind(), options.getNamespace(), ancestor);
    V1TestReader reader = new V1TestReader(datastore, query, options.getNamespace());
    V1TestWriter writer = new V1TestWriter(datastore, new DeleteMutationBuilder());
    long numEntities = 0;
    while (reader.advance()) {
        Entity entity = reader.getCurrent();
        numEntities++;
        writer.write(entity);
    }
    writer.close();
    LOG.info("Successfully deleted {} entities", numEntities);
}
Also used : Entity(com.google.datastore.v1.Entity) Datastore(com.google.datastore.v1.client.Datastore) Query(com.google.datastore.v1.Query)

Example 13 with Datastore

use of com.google.datastore.v1.client.Datastore in project beam by apache.

the class DatastoreV1Test method testDeleteIncompleteKeys.

/**
   * Test that incomplete keys cannot be deleted.
   */
@Test
public void testDeleteIncompleteKeys() throws Exception {
    Key key = makeKey("bird").build();
    DeleteKeyFn deleteKeyFn = new DeleteKeyFn();
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Keys to be deleted from the Cloud Datastore must be complete");
    deleteKeyFn.apply(key);
}
Also used : DeleteKeyFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKeyFn) DeleteKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey) Key(com.google.datastore.v1.Key) DatastoreHelper.makeKey(com.google.datastore.v1.client.DatastoreHelper.makeKey) DatastoreV1.isValidKey(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey) Test(org.junit.Test)

Example 14 with Datastore

use of com.google.datastore.v1.client.Datastore in project beam by apache.

the class V1ReadIT method testE2EV1Read.

/**
   * An end-to-end test for {@link DatastoreV1.Read#withQuery(Query)}
   *
   * <p>Write some test entities to datastore and then run a pipeline that
   * reads and counts the total number of entities. Verify that the count matches the
   * number of entities written.
   */
@Test
public void testE2EV1Read() throws Exception {
    // Read from datastore
    Query query = V1TestUtil.makeAncestorKindQuery(options.getKind(), options.getNamespace(), ancestor);
    DatastoreV1.Read read = DatastoreIO.v1().read().withProjectId(project).withQuery(query).withNamespace(options.getNamespace());
    // Count the total number of entities
    Pipeline p = Pipeline.create(options);
    PCollection<Long> count = p.apply(read).apply(Count.<Entity>globally());
    PAssert.thatSingleton(count).isEqualTo(numEntities);
    p.run();
}
Also used : Query(com.google.datastore.v1.Query) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 15 with Datastore

use of com.google.datastore.v1.client.Datastore in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreTest method buildResponsesForQueryPagination.

private List<RunQueryResponse> buildResponsesForQueryPagination() {
    Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
    Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
    datastore.add(ENTITY3, entity4, entity5);
    List<RunQueryResponse> responses = new ArrayList<>();
    Query<Key> query = Query.newKeyQueryBuilder().build();
    RunQueryRequest.Builder requestPb = RunQueryRequest.newBuilder();
    query.populatePb(requestPb);
    QueryResultBatch queryResultBatchPb = RunQueryResponse.newBuilder().mergeFrom(((DatastoreImpl) datastore).runQuery(requestPb.build())).getBatch();
    QueryResultBatch queryResultBatchPb1 = QueryResultBatch.newBuilder().mergeFrom(queryResultBatchPb).setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED).clearEntityResults().addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(0, 1)).setEndCursor(queryResultBatchPb.getEntityResultsList().get(0).getCursor()).build();
    responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb1).build());
    QueryResultBatch queryResultBatchPb2 = QueryResultBatch.newBuilder().mergeFrom(queryResultBatchPb).setMoreResults(QueryResultBatch.MoreResultsType.NOT_FINISHED).clearEntityResults().addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(1, 3)).setEndCursor(queryResultBatchPb.getEntityResultsList().get(2).getCursor()).build();
    responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb2).build());
    QueryResultBatch queryResultBatchPb3 = QueryResultBatch.newBuilder().mergeFrom(queryResultBatchPb).setMoreResults(QueryResultBatch.MoreResultsType.NO_MORE_RESULTS).clearEntityResults().addAllEntityResults(queryResultBatchPb.getEntityResultsList().subList(3, 5)).setEndCursor(queryResultBatchPb.getEntityResultsList().get(4).getCursor()).build();
    responses.add(RunQueryResponse.newBuilder().setBatch(queryResultBatchPb3).build());
    return responses;
}
Also used : QueryResultBatch(com.google.datastore.v1.QueryResultBatch) RunQueryResponse(com.google.datastore.v1.RunQueryResponse) RunQueryRequest(com.google.datastore.v1.RunQueryRequest) ArrayList(java.util.ArrayList)

Aggregations

Test (org.junit.Test)11 Entity (com.google.datastore.v1.Entity)5 Query (com.google.datastore.v1.Query)5 Key (com.google.datastore.v1.Key)4 LookupRequest (com.google.datastore.v1.LookupRequest)4 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)4 Datastore (com.google.datastore.v1.client.Datastore)4 RunQueryResponse (com.google.datastore.v1.RunQueryResponse)3 DatastoreHelper.makeKey (com.google.datastore.v1.client.DatastoreHelper.makeKey)3 DeleteEntity (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity)3 DeleteKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteKey)3 DatastoreV1.isValidKey (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.isValidKey)3 GqlQuery (com.google.datastore.v1.GqlQuery)2 ReadOptions (com.google.datastore.v1.ReadOptions)2 DatastoreException (com.google.datastore.v1.client.DatastoreException)2 DatastoreOptions (com.google.datastore.v1.client.DatastoreOptions)2 ByteString (com.google.protobuf.ByteString)2 Status (com.yahoo.ycsb.Status)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 Credential (com.google.api.client.auth.oauth2.Credential)1