本文整理了Java中com.google.cloud.datastore.Entity.newBuilder()
方法的一些代码示例,展示了Entity.newBuilder()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Entity.newBuilder()
方法的具体详情如下:
包路径:com.google.cloud.datastore.Entity
类名称:Entity
方法名:newBuilder
暂无
代码示例来源: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
@Override
public void run(Transaction tx, Key userKey, Contact contact) {
Entity user = tx.get(userKey);
if (user == null) {
System.out.println("Adding a new user.");
user = Entity.newBuilder(userKey).set("count", 0L).build();
tx.add(user);
}
FullEntity<IncompleteKey> contactEntity =
FullEntity.newBuilder()
.set("email", contact.email())
.set("phone", contact.phone())
.build();
tx.update(Entity.newBuilder(user).set("contact", contactEntity).build());
System.out.printf("Setting contact for user '%s'.%n", userKey.getName());
}
代码示例来源: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
@Test
public void testAdd() throws Exception {
Datastore datastore = createStrictMock(Datastore.class);
IncompleteKey pKey = IncompleteKey.newBuilder("ds", "k").build();
Key key = Key.newBuilder(pKey, 1).build();
Entity entity = Entity.newBuilder(key).build();
expect(datastore.add(new Entity[] {entity})).andReturn(Collections.singletonList(entity));
replay(datastore);
assertEquals(entity, DatastoreHelper.add(datastore, entity));
verify(datastore);
}
代码示例来源: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
/** 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
/** 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: googleapis/google-cloud-java
@Test
public void testGetWithTransaction() throws Exception {
Transaction transaction = createStrictMock(Transaction.class);
IncompleteKey pKey1 = IncompleteKey.newBuilder("ds", "k").build();
Key key1 = Key.newBuilder(pKey1, 1).build();
Entity entity1 = Entity.newBuilder(key1).build();
Key key2 = Key.newBuilder(pKey1, 2).build();
expect(transaction.get(new Key[] {key1}))
.andReturn(Collections.singletonList(entity1).iterator());
expect(transaction.get(new Key[] {key2})).andReturn(Collections.<Entity>emptyIterator());
replay(transaction);
assertEquals(entity1, DatastoreHelper.get(transaction, key1));
assertNull(DatastoreHelper.get(transaction, key2));
verify(transaction);
}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of rolling back a transaction. */
// [TARGET rollback()]
public Key rollback() {
Datastore datastore = transaction.getDatastore();
// [START rollback]
// create an entity
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key key = datastore.allocateId(keyFactory.newKey());
Entity entity = Entity.newBuilder(key).set("description", "rollback()").build();
// add the entity and rollback
transaction.put(entity);
transaction.rollback();
// calling transaction.commit() now would fail
// [END rollback]
return key;
}
代码示例来源: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
@Test
public void testUpdateAfterAdd() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
batchWriter.add(ENTITY1);
batchWriter.update(entity);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testPutAfterAdd() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
batchWriter.add(ENTITY1);
batchWriter.put(entity);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testUpdateAfterPut() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
batchWriter.put(ENTITY1);
batchWriter.update(entity);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testCopyFromIncompleteEntity() throws Exception {
Entity.Builder builder = Entity.newBuilder(KEY2, INCOMPLETE_ENTITY);
Entity entity = builder.build();
assertNotEquals(INCOMPLETE_ENTITY, entity);
assertEquals(INCOMPLETE_ENTITY.getProperties(), entity.getProperties());
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testUpdateAfterUpdate() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpdate(entity.toPb()).build());
batchWriter.update(ENTITY1);
batchWriter.update(entity);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testPutAfterDelete() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
batchWriter.delete(KEY1);
Entity putEntity = batchWriter.put(entity);
assertEquals(entity, putEntity);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testPutAfterUpdate() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
batchWriter.update(ENTITY1);
Entity putEntity = batchWriter.put(entity);
assertEquals(entity, putEntity);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testPutAfterPut() throws Exception {
Entity entity = Entity.newBuilder(ENTITY1).set("foo", "bar").build();
List<com.google.datastore.v1.Mutation> pbs = new LinkedList<>();
pbs.add(com.google.datastore.v1.Mutation.newBuilder().setUpsert(entity.toPb()).build());
Entity putEntity1 = batchWriter.put(ENTITY1);
Entity putEntity2 = batchWriter.put(entity);
assertEquals(ENTITY1, putEntity1);
assertEquals(entity, putEntity2);
assertEquals(pbs, batchWriter.toMutationPbList());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testCopyFrom() throws Exception {
Entity.Builder builder = Entity.newBuilder(ENTITY);
assertEquals(ENTITY, builder.build());
Entity entity = builder.setKey(KEY2).build();
assertNotEquals(ENTITY, entity);
assertEquals(KEY2, entity.getKey());
assertEquals(ENTITY.getProperties(), entity.getProperties());
}
代码示例来源: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);
}
}
内容来源于网络,如有侵权,请联系作者删除!