Java Code Examples for com.google.cloud.datastore.Datastore#put()

The following examples show how to use com.google.cloud.datastore.Datastore#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void save(Message message) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(keyFactory.newKey());

  Entity.Builder messageEntityBuilder = Entity.newBuilder(key)
      .set("messageId", message.getMessageId());

  if (message.getData() != null) {
    messageEntityBuilder = messageEntityBuilder.set("data", message.getData());
  }

  if (message.getPublishTime() != null) {
    messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime());
  }
  datastore.put(messageEntityBuilder.build());
}
 
Example 2
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void save(Message message) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(keyFactory.newKey());

  Entity.Builder messageEntityBuilder =
      Entity.newBuilder(key).set("messageId", message.getMessageId());

  if (message.getData() != null) {
    messageEntityBuilder = messageEntityBuilder.set("data", message.getData());
  }

  if (message.getPublishTime() != null) {
    messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime());
  }
  datastore.put(messageEntityBuilder.build());
}
 
Example 3
Source File: DatastoreEmulatorTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
@Parameters(method = "clients")
public void testEmulator(Datastore client) {
  var key = client.newKeyFactory().setKind("foo").newKey("bar");
  var entity = Entity.newBuilder(key).set("baz", "quux").build();
  client.put(entity);
  var readEntity = client.get(key);
  assertThat(readEntity, is(entity));
  datastoreEmulator.reset();
  var readEntityAfterReset = client.get(key);
  assertThat(readEntityAfterReset, is(nullValue()));
}
 
Example 4
Source File: QuickstartSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  // Instantiates a client
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

  // The kind for the new entity
  String kind = "Task";
  // The name/ID for the new entity
  String name = "sampletask1";
  // The Cloud Datastore key for the new entity
  Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);

  // Prepares the new entity
  Entity task = Entity.newBuilder(taskKey)
      .set("description", "Buy milk")
      .build();

  // Saves the entity
  datastore.put(task);

  System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description"));

  //Retrieve entity
  Entity retrieved = datastore.get(taskKey);

  System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description"));

}
 
Example 5
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void saveClaim(String claim) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(claimsKindKeyFactory.newKey());

  Entity.Builder claimEntityBuilder = Entity.newBuilder(key).set("claim", claim);

  datastore.put(claimEntityBuilder.build());
}
 
Example 6
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void saveToken(String token) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(tokensKindKeyFactory.newKey());

  Entity.Builder tokenEntityBuilder = Entity.newBuilder(key).set("token", token);

  datastore.put(tokenEntityBuilder.build());
}
 
Example 7
Source File: MessageRepositoryImpl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void save(Message message) {
  // Save message to "messages"
  Datastore datastore = getDatastoreInstance();
  Key key = datastore.allocateId(keyFactory.newKey());

  Entity.Builder messageEntityBuilder =
      Entity.newBuilder(key).set("messageId", message.getMessageId());

  String translated = message.getTranslated();
  if (translated != null) {
    messageEntityBuilder = messageEntityBuilder.set("data", translated);
  }

  if (message.getPublishTime() != null) {
    messageEntityBuilder = messageEntityBuilder.set("publishTime", message.getPublishTime());
  }

  if (message.getSourceLang() != null) {
    messageEntityBuilder = messageEntityBuilder.set("sourceLang", message.getSourceLang());
  }

  if (message.getTargetLang() != null) {
    messageEntityBuilder = messageEntityBuilder.set("targetLang", message.getTargetLang());
  }
  datastore.put(messageEntityBuilder.build());
}
 
Example 8
Source File: CreateEntity.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) {
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
  Key key = keyFactory.newKey("keyName");
  Entity entity =
      Entity.newBuilder(key)
          .set("name", "John Doe")
          .set("age", 30)
          .set("access_time", Timestamp.now())
          .build();
  datastore.put(entity);
}
 
Example 9
Source File: AddEntitiesAndRunQuery.java    From google-cloud-java with Apache License 2.0 4 votes vote down vote up
public static void main(String... args) {
  // Create datastore service object.
  // By default, credentials are inferred from the runtime environment.
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

  // Add an entity to Datastore
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("Person");
  Key key = keyFactory.newKey("[email protected]");
  Entity entity =
      Entity.newBuilder(key)
          .set("name", "John Doe")
          .set("age", 51)
          .set("favorite_food", "pizza")
          .build();
  datastore.put(entity);

  // Get an entity from Datastore
  Entity johnEntity = datastore.get(key);

  // Add a couple more entities to make the query results more interesting
  Key janeKey = keyFactory.newKey("[email protected]");
  Entity janeEntity =
      Entity.newBuilder(janeKey)
          .set("name", "Jane Doe")
          .set("age", 44)
          .set("favorite_food", "pizza")
          .build();
  Key joeKey = keyFactory.newKey("[email protected]");
  Entity joeEntity =
      Entity.newBuilder(joeKey)
          .set("name", "Joe Shmoe")
          .set("age", 27)
          .set("favorite_food", "sushi")
          .build();
  datastore.put(janeEntity, joeEntity);

  // Run a query
  Query<Entity> query =
      Query.newEntityQueryBuilder()
          .setKind("Person")
          .setFilter(PropertyFilter.eq("favorite_food", "pizza"))
          .build();
  QueryResults<Entity> results = datastore.run(query);
  while (results.hasNext()) {
    Entity currentEntity = results.next();
    System.out.println(currentEntity.getString("name") + ", you're invited to a pizza party!");
  }
}