com.google.cloud.datastore.Datastore.put()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.9k)|赞(0)|评价(0)|浏览(184)

本文整理了Java中com.google.cloud.datastore.Datastore.put()方法的一些代码示例,展示了Datastore.put()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Datastore.put()方法的具体详情如下:
包路径:com.google.cloud.datastore.Datastore
类名称:Datastore
方法名:put

Datastore.put介绍

[英]Example of putting a single entity.

String keyName = "my_key_name";

[中]放置单个实体的示例。

String keyName = "my_key_name";

代码示例

代码示例来源:origin: googleapis/google-cloud-java

@Before
public void setUp() {
 DATASTORE.put(ENTITY1, ENTITY2);
}

代码示例来源: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

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

代码示例来源:origin: googleapis/google-cloud-java

/** Example of putting multiple entities. */
// [TARGET put(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchPutEntities(String keyName1, String keyName2) {
 // [START batchPutEntities]
 Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
 Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
 entityBuilder1.set("propertyName", "value1");
 Entity entity1 = entityBuilder1.build();
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
 entityBuilder2.set("propertyName", "value2");
 Entity entity2 = entityBuilder2.build();
 datastore.put(entity1, entity2);
 // [END batchPutEntities]
}

代码示例来源: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.getProperties(), entities.get(3).getProperties());
 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);
}

代码示例来源: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

.set("favorite_food", "pizza")
    .build();
datastore.put(entity);
    .set("favorite_food", "sushi")
    .build();
datastore.put(janeEntity, joeEntity);

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testGetArrayNoDeferredResults() {
 DATASTORE.put(ENTITY3);
 Iterator<Entity> result =
   DATASTORE.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator();
 assertEquals(ENTITY1, result.next());
 assertNull(result.next());
 assertEquals(ENTITY2, result.next());
 Entity entity3 = result.next();
 assertEquals(ENTITY3, entity3);
 assertTrue(entity3.isNull("null"));
 assertFalse(entity3.getBoolean("bool"));
 assertEquals(LIST_VALUE2.get(), entity3.getList("list"));
 FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1");
 FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2");
 assertEquals(PARTIAL_ENTITY2, partial1);
 assertEquals(ENTITY2, partial2);
 assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType());
 assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng"));
 assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList"));
 assertEquals(8, entity3.getNames().size());
 assertFalse(entity3.contains("bla"));
 try {
  entity3.getString("str");
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  // expected - no such property
 }
 assertFalse(result.hasNext());
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testGetArrayNoDeferredResults() {
 datastore.put(ENTITY3);
 Iterator<Entity> result =
   datastore.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator();
 assertEquals(ENTITY1, result.next());
 assertNull(result.next());
 assertEquals(ENTITY2, result.next());
 Entity entity3 = result.next();
 assertEquals(ENTITY3, entity3);
 assertTrue(entity3.isNull("null"));
 assertFalse(entity3.getBoolean("bool"));
 assertEquals(LIST_VALUE2.get(), entity3.getList("list"));
 FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1");
 FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2");
 assertEquals(PARTIAL_ENTITY2, partial1);
 assertEquals(ENTITY2, partial2);
 assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType());
 assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng"));
 assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList"));
 assertEquals(8, entity3.getNames().size());
 assertFalse(entity3.contains("bla"));
 try {
  entity3.getString("str");
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  // expected - no such property
 }
 assertFalse(result.hasNext());
}

代码示例来源:origin: googleapis/google-cloud-java

assertFalse(results1.hasNext());
DATASTORE.put(ENTITY3);
Query<? extends Entity> query2 =
  Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND2 + " order by __key__")

代码示例来源:origin: googleapis/google-cloud-java

@Test
 public void testStartStopReset() throws IOException, InterruptedException, TimeoutException {
  LocalDatastoreHelper helper = LocalDatastoreHelper.create();
  helper.start();
  Datastore datastore = helper.getOptions().getService();
  Key key = datastore.newKeyFactory().setKind("kind").newKey("name");
  datastore.put(Entity.newBuilder(key).build());
  assertNotNull(datastore.get(key));
  helper.reset();
  assertNull(datastore.get(key));
  helper.stop(Duration.ofMinutes(1));
  thrown.expect(DatastoreException.class);
  datastore.get(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

@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(ENTITY2).clear().set("from", "datastore").build());
 transaction.update(Entity.newBuilder(ENTITY2).clear().set("from", "transaction").build());
 try {
  transaction.commit();
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  assertEquals("ABORTED", expected.getReason());
 }
}

代码示例来源: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

transaction.delete(ENTITY3.getKey());
DATASTORE.put(Entity.newBuilder(ENTITY2).clear().build());
try {
 transaction.commit();

代码示例来源: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: GoogleCloudPlatform/java-docs-samples

/**
 * Adds a task entity to the Datastore.
 *
 * @param description The task description
 * @return The {@link Key} of the entity
 * @throws DatastoreException if the ID allocation or put fails
 */
Key addTask(String description) {
 Key key = datastore.allocateId(keyFactory.newKey());
 Entity task = Entity.newBuilder(key)
   .set("description", StringValue.newBuilder(description).setExcludeFromIndexes(true).build())
   .set("created", Timestamp.now())
   .set("done", false)
   .build();
 datastore.put(task);
 return key;
}
// [END datastore_add_entity]

代码示例来源:origin: googleapis/google-cloud-java

assertFalse(results1.hasNext());
datastore.put(ENTITY3);
Query<? extends Entity> query2 =
  Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND2 + " order by __key__")

代码示例来源:origin: stackoverflow.com

@Test
public void test1() throws IOException, InterruptedException {
  Datastore ds = DatastoreOptions.builder().host("http://localhost:9999").projectId("my-project").build().service();
  com.google.cloud.datastore.Key key = ds.newKeyFactory().kind("MyEntity").newKey("mykey");
  com.google.cloud.datastore.Entity entity = com.google.cloud.datastore.Entity.builder(key).set("p1", "Hello World!").build();
  entity = ds.put(entity);
  entity = ds.get(key);
  System.out.println(entity);
}

代码示例来源:origin: stackoverflow.com

public void processElement(ProcessContext c) {
  final Datastore datastore = DatastoreOptions.defaultInstance().service();
  final KeyFactory keyFactory = datastore.newKeyFactory().kind("Task");

  Key key = datastore.allocateId(keyFactory.newKey());
  Entity task = Entity.builder(key)
    .set("description", StringValue.builder(":D").excludeFromIndexes(true).build())
    .set("created", DateTime.now())
    .set("done", false)
    .build();
  datastore.put(task);
}

相关文章