Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1b894bc
Added start of samples
michaelbausor Sep 16, 2016
73a9451
Add snippets for Transaction's commit(), rollback() and active() meth…
mziccard Sep 16, 2016
c3b7cbf
Adding datastore snippets
garrettjonesgoogle Sep 16, 2016
9c7ed1c
Merge remote-tracking branch 'upstream_mike/master' into ds-snippets-…
omaray Sep 16, 2016
99f6311
Adding 2 snippets for allocateId
omaray Sep 16, 2016
7fdc252
Added snippets and IT class
michaelbausor Sep 16, 2016
8133a5d
Merge branch 'master' of github.com:michaelbausor/gcloud-java
michaelbausor Sep 16, 2016
98bdcfe
Add BigQuery snippets for Job.isDone() (#1250)
tswast Sep 16, 2016
ffa6863
Adding name tags to ITDatastoreSnippets
garrettjonesgoogle Sep 16, 2016
94345c9
Fixes for Datastore integration tests
garrettjonesgoogle Sep 16, 2016
69e38a6
Adding integration tests for allocateIdSingle and allocateIdMultiple
omaray Sep 16, 2016
8661649
Update methods and IT
michaelbausor Sep 16, 2016
7a9b9a6
Adding testBatchPutUpdateGet
garrettjonesgoogle Sep 16, 2016
d137073
Update tags, remove unused tests
michaelbausor Sep 16, 2016
d55fed2
Fixing the target for allocateId
omaray Sep 16, 2016
b6aa820
Merge remote-tracking branch 'upstream_mike/master' into ds-snippets-…
omaray Sep 16, 2016
f87062c
Merge branch 'master' of github.com:michaelbausor/gcloud-java
michaelbausor Sep 16, 2016
bd4e872
Merge branch 'master' of github.com:michaelbausor/gcloud-java
michaelbausor Sep 16, 2016
161a7c1
Testing fetch and delete
garrettjonesgoogle Sep 16, 2016
4dffd61
Fixing build (missing constants)
garrettjonesgoogle Sep 16, 2016
22b2761
unique keys and cleanup
garrettjonesgoogle Sep 16, 2016
d9ad32a
Add runquery test
michaelbausor Sep 16, 2016
87ec825
Rename delete
michaelbausor Sep 16, 2016
0cc4740
Snippets for Datastore.runInTransaction and Datastore.newBatch.
anthmgoogle Sep 16, 2016
73400e2
Adding missing docs, fixing shaded import
garrettjonesgoogle Sep 16, 2016
d59227b
Simplify runQuery
michaelbausor Sep 16, 2016
0004817
Integration tests for Datastore.runInTransaction and Datastore.newBatch.
anthmgoogle Sep 16, 2016
c2b16c1
Key cleanup
garrettjonesgoogle Sep 16, 2016
f1a3b44
Editing for consistency
garrettjonesgoogle Sep 16, 2016
c0b6b09
add new runQuery snippet
michaelbausor Sep 16, 2016
3b9bfca
Fix variable name
michaelbausor Sep 16, 2016
46f425f
whitespace
garrettjonesgoogle Sep 16, 2016
e490d55
Merge remote-tracking branch 'origin'
garrettjonesgoogle Sep 16, 2016
7198e61
Merge remote-tracking branch 'origin'
garrettjonesgoogle Sep 16, 2016
f5a3101
Move datastore IT to correct folder
michaelbausor Sep 16, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* EDITING INSTRUCTIONS
* This file is referenced in Datastore's javadoc. Any change to this file should be reflected in
* Datastore's javadoc.
*/

package com.google.cloud.examples.datastore.snippets;

import com.google.cloud.datastore.Batch;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.Datastore.TransactionCallable;
import com.google.cloud.datastore.DatastoreReaderWriter;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.IncompleteKey;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.datastore.StructuredQuery.Filter;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import com.google.common.collect.Lists;
import com.google.datastore.v1.PropertyFilter.Operator;
import java.util.Iterator;
import java.util.List;

/**
* This class contains a number of snippets for the {@link Datastore} interface.
*/
public class DatastoreSnippets {

private final Datastore datastore;

public DatastoreSnippets(Datastore datastore) {
this.datastore = datastore;
}

/**
* Example of running in a transaction.
*/
// [TARGET runInTransaction(TransactionCallable<T> callable)]
// [VARIABLE "my_callable_result"]
public String runInTransaction(final String callableResult) {
// [START runInTransaction]
TransactionCallable<String> callable = new TransactionCallable<String>() {
@Override
public String run(DatastoreReaderWriter readerWriter) {
return callableResult;
}
};
String result = datastore.runInTransaction(callable);
return result;
// [END runInTransaction]
}

/**
* Example of starting a new batch.
*/
// [TARGET newBatch()]
// [VARIABLE "my_key_name_1"]
// [VARIABLE "my_key_name_2"]
public Batch newBatch(String keyName1, String keyName2) {
// [START newBatch]
Key key1 = datastore.newKeyFactory().kind("MyClass").newKey(keyName1);
Key key2 = datastore.newKeyFactory().kind("MyClass").newKey(keyName2);
Batch batch = datastore.newBatch();
Entity entity1 = Entity.builder(key1).set("name", "John").build();
Entity entity2 = Entity.builder(key2).set("title", "title").build();
batch.add(entity1);
batch.add(entity2);
batch.submit();
return batch;
// [END newBatch]
}

/**
* Example of allocating an id
*/
// [TARGET allocateId(IncompleteKey key)]
public Key allocateIdSingle() {
// [START allocateIdSingle]
KeyFactory keyFactory = datastore.newKeyFactory().kind("MyClass");
IncompleteKey incompleteKey = keyFactory.newKey();

// let cloud datastore automatically assign an id
Key key = datastore.allocateId(incompleteKey);
// [END allocateIdSingle]
return key;
}

/**
* Example of allocating multiple ids in a single batch
*/
// [TARGET allocateId(IncompleteKey... keys)]
public List<Key> batchAllocateId() {
// [START batchAllocateId]
KeyFactory keyFactory = datastore.newKeyFactory().kind("MyClass");
IncompleteKey incompleteKey1 = keyFactory.newKey();
IncompleteKey incompleteKey2 = keyFactory.newKey();

// let cloud datastore automatically assign the ids
List<Key> keys = datastore.allocateId(incompleteKey1, incompleteKey2);
// [END batchAllocateId]
return keys;
}

/**
* Example of updating multiple entities
*/
// [TARGET update(Entity... entities)]
// [VARIABLE "my_key_name"]
public void batchUpdateEntities(String keyName1, String keyName2) {
// [START batchUpdateEntities]
Key key1 = datastore.newKeyFactory().kind("MyClass").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.builder(key1);
entityBuilder1.set("propertyName", "updatedValue1");
Entity entity1 = entityBuilder1.build();

Key key2 = datastore.newKeyFactory().kind("MyClass").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.builder(key2);
entityBuilder2.set("propertyName", "updatedValue2");
Entity entity2 = entityBuilder2.build();

datastore.update(entity1, entity2);
// [END batchUpdateEntities]
}

/**
* Example of putting a single entity
*/
// [TARGET put(FullEntity<?> entity)]
// [VARIABLE "my_key_name"]
public void putSingleEntity(String keyName) {
// [START putSingleEntity]
Key key = datastore.newKeyFactory().kind("MyClass").newKey(keyName);
Entity.Builder entityBuilder = Entity.builder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
datastore.put(entity);
// [END putSingleEntity]
}

/**
* Example of putting multiple entities
*/
// [TARGET put(FullEntity<?>... entity)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchPutEntities(String keyName1, String keyName2) {
// [START batchPutEntities]
Key key1 = datastore.newKeyFactory().kind("MyClass").newKey(keyName1);
Entity.Builder entityBuilder1 = Entity.builder(key1);
entityBuilder1.set("propertyName", "value1");
Entity entity1 = entityBuilder1.build();

Key key2 = datastore.newKeyFactory().kind("MyClass").newKey(keyName2);
Entity.Builder entityBuilder2 = Entity.builder(key2);
entityBuilder2.set("propertyName", "value2");
Entity entity2 = entityBuilder2.build();

datastore.put(entity1, entity2);
// [END batchPutEntities]
}

/**
* Example of deleting multiplie entities
*/
// [TARGET delete(Key... keys)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchDeleteEntities(String keyName1, String keyName2) {
// [START batchDeleteEntities]
Key key1 = datastore.newKeyFactory().kind("MyClass").newKey(keyName1);
Key key2 = datastore.newKeyFactory().kind("MyClass").newKey(keyName2);
datastore.delete(key1, key2);
// [END batchDeleteEntities]
}

/**
* Example of creating a KeyFactory.
*/
// [TARGET newKeyFactory()]
public KeyFactory createKeyFactory() {
// [START createKeyFactory]
KeyFactory keyFactory = datastore.newKeyFactory();
// [END createKeyFactory]
return keyFactory;
}

/**
* Example of getting an Entity.
*/
// [TARGET get(Key key, ReadOption... options)]
// [VARIABLE "my_key_name"]
public Entity getEntityWithKey(String keyName) {
// [START getEntityWithKey]
Key key = datastore.newKeyFactory().kind("MyClass").newKey(keyName);
Entity entity = datastore.get(key);
// Do something with the entity
// [END getEntityWithKey]
return entity;
}

/**
* Example of getting multiple Entity objects.
*/
// [TARGET get(Iterable<Key> key, ReadOption... options)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> getEntitiesWithKeys(String firstKeyName, String secondKeyName) {
// [START getEntitiesWithKeys]
KeyFactory keyFactory = datastore.newKeyFactory().kind("MyClass");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
Iterator<Entity> entitiesIterator = datastore.get(Lists.newArrayList(firstKey, secondKey));
// TODO make a change so that it's not necessary to hold the entities in a list for
// integration testing
List<Entity> entities = Lists.newArrayList();
while (entitiesIterator.hasNext()) {
Entity entity = entitiesIterator.next();
// do something with the entity
entities.add(entity);
}
// [END getEntitiesWithKeys]
return entities;
}

/**
* Example of fetching a list of Entity objects.
*/
// [TARGET fetch(Iterable<Key> key, ReadOption... options)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) {
// [START fetchEntitiesWithKeys]
KeyFactory keyFactory = datastore.newKeyFactory().kind("MyClass");
Key firstKey = keyFactory.newKey(firstKeyName);
Key secondKey = keyFactory.newKey(secondKeyName);
List<Entity> entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey));
for (Entity entity : entities) {
// do something with the entity
}
// [END fetchEntitiesWithKeys]
return entities;
}

/**
* Example of running a query to find all keys of one kind.
*/
// [TARGET run(Query<T> query, ReadOption... options)]
// [VARIABLE "my_kind"]
public List<Entity> runQuery(String kind) {
// [START runQuery]
StructuredQuery<Entity> query =
Query.entityQueryBuilder()
.kind(kind)
.build();
QueryResults<Entity> results = datastore.run(query);
// TODO make a change so that it's not necessary to hold the entities in a list for
// integration testing
List<Entity> entities = Lists.newArrayList();
while (results.hasNext()) {
Entity result = results.next();
// do something with result
entities.add(result);
}
// [END runQuery]
return entities;
}

/**
* Example of running a query to find all keys with a matching property value.
*/
// [TARGET run(Query<T> query, ReadOption... options)]
// [VARIABLE "my_kind"]
// [VARIABLE "my_property"]
// [VARIABLE "my_value"]
public List<Entity> runQueryOnProperty(String kind, String property, String value) {
// [START runQueryOnProperty]
StructuredQuery<Entity> query =
Query.entityQueryBuilder()
.kind(kind)
.filter(PropertyFilter.eq(property, value))
.build();
QueryResults<Entity> results = datastore.run(query);
// TODO make a change so that it's not necessary to hold the entities in a list for
// integration testing
List<Entity> entities = Lists.newArrayList();
while (results.hasNext()) {
Entity result = results.next();
// do something with result
entities.add(result);
}
// [END runQueryOnProperty]
return entities;
}
}
Loading