congrats Icon
New! Tabnine Pro 14-day free trial
Start a free trial
Tabnine Logo
Entity$Builder
Code IndexAdd Tabnine to your IDE (free)

How to use
Entity$Builder
in
com.google.cloud.datastore

Best Java code snippets using com.google.cloud.datastore.Entity$Builder (Showing top 20 results out of 315)

Refine searchRefine arrow

  • Entity
  • Datastore
  • KeyFactory
  • Transaction
  • Key
  • FullEntity
origin: googleapis/google-cloud-java

/** Example of putting a single entity. */
// [TARGET put(FullEntity)]
// [VARIABLE "my_key_name"]
public void putSingleEntity(String keyName) {
 Datastore datastore = transaction.getDatastore();
 // [START putSingleEntity]
 Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
 Entity.Builder entityBuilder = Entity.newBuilder(key);
 entityBuilder.set("propertyName", "value");
 Entity entity = entityBuilder.build();
 transaction.put(entity);
 transaction.commit();
 // [END putSingleEntity]
}
origin: googleapis/google-cloud-java

private Builder(Key key, FullEntity<?> entity) {
 setProperties(entity.getProperties());
 setKey(key);
}
origin: googleapis/google-cloud-java

@Test
public void testTransactionWithRead() {
 Transaction transaction = datastore.newTransaction();
 assertNull(transaction.get(KEY3));
 transaction.add(ENTITY3);
 transaction.commit();
 assertEquals(ENTITY3, datastore.get(KEY3));
 transaction = datastore.newTransaction();
 assertEquals(ENTITY3, transaction.get(KEY3));
 // update entity3 during the transaction
 datastore.put(Entity.newBuilder(ENTITY3).clear().build());
 transaction.update(ENTITY2);
 try {
  transaction.commit();
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  assertEquals("ABORTED", expected.getReason());
 }
}
origin: googleapis/google-cloud-java

/** Example of putting a single entity. */
// [TARGET put(FullEntity)]
// [VARIABLE "my_key_name"]
public void putSingleEntity(String keyName) {
 // [START putSingleEntity]
 Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
 Entity.Builder entityBuilder = Entity.newBuilder(key);
 entityBuilder.set("propertyName", "value");
 Entity entity = entityBuilder.build();
 datastore.put(entity);
 // [END putSingleEntity]
}
origin: googleapis/google-cloud-java

@Test
public void testNewTransactionCommit() {
 Transaction transaction = datastore.newTransaction();
 transaction.add(ENTITY3);
 Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
 transaction.update(entity2);
 transaction.delete(KEY1);
 transaction.commit();
 List<Entity> list = datastore.fetch(KEY1, KEY2, KEY3);
 assertNull(list.get(0));
 assertEquals(entity2, list.get(1));
 assertEquals(ENTITY3, list.get(2));
 assertEquals(3, list.size());
 try {
  transaction.commit();
  fail("Expecting a failure");
 } catch (DatastoreException ex) {
  // expected to fail
 }
 try {
  transaction.rollback();
  fail("Expecting a failure");
 } catch (DatastoreException ex) {
  // expected to fail
 }
 verifyNotUsable(transaction);
}
origin: googleapis/google-cloud-java

 public static void main(String... args) {
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
  Key key = keyFactory.newKey("keyName");
  Entity entity = datastore.get(key);
  if (entity != null) {
   System.out.println("Updating access_time for " + entity.getString("name"));
   entity = Entity.newBuilder(entity).set("access_time", Timestamp.now()).build();
   datastore.update(entity);
  }
 }
}
origin: googleapis/google-cloud-java

KeyFactory keyFactory = datastore.newKeyFactory().setKind("Person");
Key key = keyFactory.newKey("john.doe@gmail.com");
Entity entity =
  Entity.newBuilder(key)
    .set("name", "John Doe")
    .set("age", 51)
    .set("favorite_food", "pizza")
    .build();
datastore.put(entity);
Entity johnEntity = datastore.get(key);
Key janeKey = keyFactory.newKey("jane.doe@gmail.com");
Entity janeEntity =
  Entity.newBuilder(janeKey)
    .set("name", "Jane Doe")
    .set("age", 44)
    .set("favorite_food", "pizza")
    .build();
Key joeKey = keyFactory.newKey("joe.shmoe@gmail.com");
Entity joeEntity =
  Entity.newBuilder(joeKey)
    .set("name", "Joe Shmoe")
    .set("age", 27)
    .set("favorite_food", "sushi")
    .build();
datastore.put(janeEntity, joeEntity);
origin: GoogleCloudPlatform/java-docs-samples

 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"));

 }
}
origin: googleapis/google-cloud-java

@Test
public void testTransactionWithQuery() {
 Query<Entity> query =
   Query.newEntityQueryBuilder()
     .setKind(KIND2)
     .setFilter(PropertyFilter.hasAncestor(KEY2))
     .build();
 Transaction transaction = datastore.newTransaction();
 QueryResults<Entity> results = transaction.run(query);
 assertEquals(ENTITY2, results.next());
 assertFalse(results.hasNext());
 transaction.add(ENTITY3);
 transaction.commit();
 assertEquals(ENTITY3, datastore.get(KEY3));
 transaction = datastore.newTransaction();
 results = transaction.run(query);
 assertEquals(ENTITY2, results.next());
 transaction.delete(ENTITY3.getKey());
 // update entity2 during the transaction
 datastore.put(Entity.newBuilder(ENTITY2).clear().build());
 try {
  transaction.commit();
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  assertEquals("ABORTED", expected.getReason());
 }
}
origin: googleapis/google-cloud-java

/** Example of updating multiple entities. */
// [TARGET update(Entity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleUpdateEntities(String keyName1, String keyName2) {
 Datastore datastore = transaction.getDatastore();
 // [START multipleUpdateEntities]
 Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
 Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
 entityBuilder1.set("propertyName", "value3");
 Entity entity1 = entityBuilder1.build();
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
 entityBuilder2.set("propertyName", "value4");
 Entity entity2 = entityBuilder2.build();
 transaction.update(entity1, entity2);
 transaction.commit();
 // [END multipleUpdateEntities]
}
origin: googleapis/google-cloud-java

/** Example of adding a single entity. */
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
 Datastore datastore = transaction.getDatastore();
 // [START addSingleEntity]
 Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
 Entity.Builder entityBuilder = Entity.newBuilder(key);
 entityBuilder.set("propertyName", "value");
 Entity entity = entityBuilder.build();
 transaction.add(entity);
 transaction.commit();
 // [END addSingleEntity]
}
origin: google/data-transfer-project

@Override
public <T extends DataModel> void update(UUID jobId, String key, T model) {
 Transaction transaction = datastore.newTransaction();
 Key entityKey = getDataKey(jobId, key);
 try {
  Entity previousEntity = transaction.get(entityKey);
  if (previousEntity == null) {
   throw new IOException("Could not find record for data key: " + entityKey.getName());
  }
  String serialized = objectMapper.writeValueAsString(model);
  Entity entity = Entity.newBuilder(entityKey)
    .set(CREATED_FIELD, Timestamp.now())
    .set(model.getClass().getName(), serialized)
    .build();
  transaction.put(entity);
  transaction.commit();
 } catch (IOException t) {
  transaction.rollback();
  throw new RuntimeException("Failed atomic update of key: " + key, t);
 }
}
origin: GoogleCloudPlatform/java-docs-samples

/**
 * Marks a task entity as done.
 *
 * @param id The ID of the task entity as given by {@link Key#id()}
 * @return true if the task was found, false if not
 * @throws DatastoreException if the transaction fails
 */
boolean markDone(long id) {
 Transaction transaction = datastore.newTransaction();
 try {
  Entity task = transaction.get(keyFactory.newKey(id));
  if (task != null) {
   transaction.put(Entity.newBuilder(task).set("done", true).build());
  }
  transaction.commit();
  return task != null;
 } finally {
  if (transaction.isActive()) {
   transaction.rollback();
  }
 }
}
// [END datastore_update_entity]
origin: googleapis/google-cloud-java

/** Example of adding a single entity. */
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
 // [START addSingleEntity]
 Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
 Entity.Builder entityBuilder = Entity.newBuilder(key);
 entityBuilder.set("propertyName", "value");
 Entity entity = entityBuilder.build();
 try {
  datastore.add(entity);
 } catch (DatastoreException ex) {
  if ("ALREADY_EXISTS".equals(ex.getReason())) {
   // entity.getKey() already exists
  }
 }
 // [END addSingleEntity]
}
origin: googleapis/google-cloud-java

@Test
public void testNewBatch() {
 Batch batch = datastore.newBatch();
 Entity entity1 = Entity.newBuilder(ENTITY1).clear().build();
 Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
 Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
 Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
 assertEquals(PARTIAL_ENTITY2.getProperties(), entity6.getProperties());
 assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId());
 assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace());
 assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
 assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind());
 assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build());
   datastore.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
 assertEquals(entity1, entities.get(0));
 assertEquals(entity2, entities.get(1));
 assertEquals(1, generatedKeys.size());
 assertEquals(
   PARTIAL_ENTITY3.getProperties(), datastore.get(generatedKeys.get(0)).getProperties());
 assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
origin: googleapis/google-cloud-java

@Test
public void testNewBatch() {
 Batch batch = DATASTORE.newBatch();
 Entity entity1 = Entity.newBuilder(ENTITY1).clear().build();
 Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
 Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
 Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
 assertEquals(PARTIAL_ENTITY2.getNames(), entity6.getNames());
 assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId());
 assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace());
 assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
 assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind());
 assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build());
   DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
 assertEquals(entity1, entities.get(0));
 assertEquals(entity2, entities.get(1));
 List<Key> generatedKeys = response.getGeneratedKeys();
 assertEquals(1, generatedKeys.size());
 assertEquals(PARTIAL_ENTITY3.getNames(), DATASTORE.get(generatedKeys.get(0)).getNames());
 assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
origin: googleapis/google-cloud-java

/** Example of updating multiple entities. */
// [TARGET update(Entity...)]
// [VARIABLE "my_key_name_1"]
// [VARIABLE "my_key_name_2"]
public void batchUpdateEntities(String keyName1, String keyName2) {
 // [START batchUpdateEntities]
 Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
 Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
 entityBuilder1.set("propertyName", "updatedValue1");
 Entity entity1 = entityBuilder1.build();
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
 entityBuilder2.set("propertyName", "updatedValue2");
 Entity entity2 = entityBuilder2.build();
 datastore.update(entity1, entity2);
 // [END batchUpdateEntities]
}
origin: googleapis/google-cloud-java

@Test
public void testPut() {
 Entity updatedEntity = Entity.newBuilder(ENTITY1).set("new_property", 42L).build();
 assertEquals(updatedEntity, DATASTORE.put(updatedEntity));
 assertEquals(updatedEntity, DATASTORE.get(updatedEntity.getKey()));
 Entity entity2 = Entity.newBuilder(ENTITY2).clear().set("bla", new NullValue()).build();
 assertNotEquals(ENTITY2, entity2);
 List<Entity> entities = DATASTORE.put(ENTITY1, entity2, ENTITY3, PARTIAL_ENTITY1);
 assertEquals(ENTITY1, entities.get(0));
 assertEquals(entity2, entities.get(1));
 assertEquals(ENTITY3, entities.get(2));
 assertEquals(PARTIAL_ENTITY1.getNames(), entities.get(3).getNames());
 assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(3).getKey().getAncestors());
 assertEquals(ENTITY1, DATASTORE.get(ENTITY1.getKey()));
 assertEquals(entity2, DATASTORE.get(entity2.getKey()));
 assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
 Entity entity = DATASTORE.get(entities.get(3).getKey());
 assertEquals(entities.get(3), entity);
 for (Entity entityToDelete : entities) {
  DATASTORE.delete(entityToDelete.getKey());
 }
}
origin: googleapis/google-cloud-java

/** 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().setKind("MyKind").newKey(keyName1);
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 Batch batch = datastore.newBatch();
 Entity entity1 = Entity.newBuilder(key1).set("name", "John").build();
 Entity entity2 = Entity.newBuilder(key2).set("title", "title").build();
 batch.add(entity1);
 batch.add(entity2);
 batch.submit();
 // [END newBatch]
 return batch;
}
origin: googleapis/google-cloud-java

@Override
public void run(Transaction tx, Key userKey, String content) {
 Entity user = tx.get(userKey);
 if (user == null) {
  System.out.println("Adding a new user.");
  user = Entity.newBuilder(userKey).set("count", 1).build();
  tx.add(user);
 } else {
  user = Entity.newBuilder(user).set("count", user.getLong("count") + 1L).build();
  tx.update(user);
 }
 IncompleteKey commentKey = IncompleteKey.newBuilder(userKey, COMMENT_KIND).build();
 FullEntity<IncompleteKey> comment =
   FullEntity.newBuilder(commentKey)
     .set("content", content)
     .set("timestamp", Timestamp.now())
     .build();
 tx.addWithDeferredIdAllocation(comment);
 System.out.printf("Adding a comment to user '%s'.%n", userKey.getName());
}
com.google.cloud.datastoreEntity$Builder

Most used methods

  • build
  • set
  • setKey
  • <init>
  • clear
  • addProperty
  • fill
  • key
  • setNull
  • setProperties

Popular in Java

  • Reading from database using SQL prepared statement
  • findViewById (Activity)
  • getExternalFilesDir (Context)
  • getSupportFragmentManager (FragmentActivity)
  • Date (java.util)
    A specific moment in time, with millisecond precision. Values typically come from System#currentTime
  • Reference (javax.naming)
  • SSLHandshakeException (javax.net.ssl)
    The exception that is thrown when a handshake could not be completed successfully.
  • BoxLayout (javax.swing)
  • Response (javax.ws.rs.core)
    Defines the contract between a returned instance and the runtime when an application needs to provid
  • Logger (org.slf4j)
    The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that loggin
  • Top 15 Vim Plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now