本文整理了Java中com.google.cloud.datastore.Datastore.add()
方法的一些代码示例,展示了Datastore.add()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Datastore.add()
方法的具体详情如下:
包路径:com.google.cloud.datastore.Datastore
类名称:Datastore
方法名:add
[英]If an entity for entity.getKey() does not exist, entity is inserted. Otherwise, a DatastoreException is thrown with DatastoreException#getReason()equal to "ALREADY_EXISTS".
Example of adding a single entity.
String keyName = "my_key_name";catch (DatastoreException ex)
if ("ALREADY_EXISTS".equals(ex.getReason()))
// entity.getKey() already exists
}
}
}
[中]如果一个实体对应一个实体。getKey()不存在,已插入实体。否则,将抛出DatastoreException,其中DatastoreException#getReason()等于“已存在”。
添加单个实体的示例。
String keyName = "my_key_name";catch (DatastoreException ex)
if ("ALREADY_EXISTS".equals(ex.getReason()))
// entity.getKey() already exists
}
}
}
代码示例来源: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 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 adding multiple entities. */
// [TARGET add(FullEntity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchAddEntities(String keyName1, String keyName2) {
// [START batchAddEntities]
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();
try {
datastore.add(entity1, entity2);
} catch (DatastoreException ex) {
if ("ALREADY_EXISTS".equals(ex.getReason())) {
// at least one of entity1.getKey() and entity2.getKey() already exists
}
}
// [END batchAddEntities]
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testAddEntity() {
List<Entity> keys = datastore.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
datastore.add(ENTITY1);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
List<Entity> entities = datastore.add(ENTITY3, PARTIAL_ENTITY1, PARTIAL_ENTITY2);
assertEquals(ENTITY3, datastore.get(ENTITY3.getKey()));
assertEquals(ENTITY3, entities.get(0));
assertEquals(PARTIAL_ENTITY1.getProperties(), entities.get(1).getProperties());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(1).getKey().getAncestors());
assertNotNull(datastore.get(entities.get(1).getKey()));
assertEquals(PARTIAL_ENTITY2.getProperties(), entities.get(2).getProperties());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entities.get(2).getKey().getAncestors());
assertNotNull(datastore.get(entities.get(2).getKey()));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testAddEntity() {
List<Entity> keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
DATASTORE.add(ENTITY1);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
List<Entity> entities = DATASTORE.add(ENTITY3, PARTIAL_ENTITY1, PARTIAL_ENTITY2);
assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
assertEquals(ENTITY3, entities.get(0));
assertEquals(PARTIAL_ENTITY1.getNames(), entities.get(1).getNames());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(1).getKey().getAncestors());
assertNotNull(DATASTORE.get(entities.get(1).getKey()));
assertEquals(PARTIAL_ENTITY2.getNames(), entities.get(2).getNames());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entities.get(2).getKey().getAncestors());
assertNotNull(DATASTORE.get(entities.get(2).getKey()));
for (Entity entity : entities) {
DATASTORE.delete(entity.getKey());
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Before
public void setUp() {
rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
rpcMockOptions =
options
.toBuilder()
.setRetrySettings(ServiceOptions.getDefaultRetrySettings())
.setServiceRpcFactory(rpcFactoryMock)
.build();
EasyMock.expect(rpcFactoryMock.create(rpcMockOptions)).andReturn(rpcMock);
StructuredQuery<Key> query = Query.newKeyQueryBuilder().build();
QueryResults<Key> result = datastore.run(query);
datastore.delete(Iterators.toArray(result, Key.class));
datastore.add(ENTITY1, ENTITY2);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testUpdate() {
List<Entity> keys = datastore.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
datastore.update(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
datastore.add(ENTITY3);
assertEquals(ENTITY3, datastore.get(ENTITY3.getKey()));
Entity entity3 = Entity.newBuilder(ENTITY3).clear().set("bla", new NullValue()).build();
assertNotEquals(ENTITY3, entity3);
datastore.update(entity3);
assertEquals(entity3, datastore.get(ENTITY3.getKey()));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testUpdate() {
List<Entity> keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
DATASTORE.update(ENTITY3);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
DATASTORE.add(ENTITY3);
assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
Entity entity3 = Entity.newBuilder(ENTITY3).clear().set("bla", new NullValue()).build();
assertNotEquals(ENTITY3, entity3);
DATASTORE.update(entity3);
assertEquals(entity3, DATASTORE.get(ENTITY3.getKey()));
}
代码示例来源:origin: googleapis/google-cloud-java
private List<RunQueryResponse> buildResponsesForQueryPaginationWithLimit() {
Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
datastore.add(ENTITY3, entity4, entity5);
DatastoreRpc datastoreRpc = datastore.getOptions().getDatastoreRpcV1();
List<RunQueryResponse> responses = new ArrayList<>();
代码示例来源:origin: googleapis/google-cloud-java
private List<RunQueryResponse> buildResponsesForQueryPagination() {
Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
datastore.add(ENTITY3, entity4, entity5);
List<RunQueryResponse> responses = new ArrayList<>();
Query<Key> query = Query.newKeyQueryBuilder().build();
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
/**
* Create a new user and add it to Cloud Datastore.
*/
public User createUser(String name, String email) {
failIfInvalid(name, email);
User user = new User(name, email);
Key key = keyFactory.newKey(user.getId());
Entity entity = Entity.newBuilder(key)
.set("id", user.getId())
.set("name", name)
.set("email", email)
.build();
datastore.add(entity);
return user;
}
内容来源于网络,如有侵权,请联系作者删除!