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

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

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

Datastore.fetch介绍

[英]Returns a list with a value for each given key (ordered by input). null values are returned for nonexistent keys. When possible prefer using #get(Key...) to avoid eagerly loading the results. ReadOptions can be specified if desired.

Example of fetching a list of Entity objects.

String firstKeyName = "my_first_key_name";}

[中]返回一个列表,其中包含每个给定键的值(按输入排序)。为不存在的键返回空值。如果可能,更喜欢使用#get(Key…)避免急于加载结果。如果需要,可以指定ReadOptions。
获取实体对象列表的示例。

String firstKeyName = "my_first_key_name";}

代码示例

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

/** Example of fetching a list of Entity objects. */
// [TARGET fetch(Iterable, ReadOption...)]
// [VARIABLE "my_first_key_name"]
// [VARIABLE "my_second_key_name"]
public List<Entity> fetchEntitiesWithKeys(String firstKeyName, String secondKeyName) {
 // [START fetchEntitiesWithKeys]
 KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
 Key firstKey = keyFactory.newKey(firstKeyName);
 Key secondKey = keyFactory.newKey(secondKeyName);
 List<Entity> entities = datastore.fetch(Lists.newArrayList(firstKey, secondKey));
 for (Entity entity : entities) {
  // do something with the entity
 }
 // [END fetchEntitiesWithKeys]
 return entities;
}

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

@Test
public void testDelete() {
 Iterator<Entity> keys =
   datastore.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
 assertEquals(ENTITY1, keys.next());
 assertEquals(ENTITY2, keys.next());
 assertNull(keys.next());
 assertFalse(keys.hasNext());
 datastore.delete(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey());
 keys = datastore.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
 assertNull(keys.next());
 assertNull(keys.next());
 assertNull(keys.next());
 assertFalse(keys.hasNext());
}

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

@Test
public void testDelete() {
 Iterator<Entity> keys =
   DATASTORE.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
 assertEquals(ENTITY1, keys.next());
 assertEquals(ENTITY2, keys.next());
 assertNull(keys.next());
 assertFalse(keys.hasNext());
 DATASTORE.delete(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey());
 keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY2.getKey(), ENTITY3.getKey()).iterator();
 assertNull(keys.next());
 assertNull(keys.next());
 assertNull(keys.next());
 assertFalse(keys.hasNext());
}

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

@Test
public void testFetchArrayDeferredResults() throws DatastoreException {
 List<Entity> foundEntities =
   createDatastoreForDeferredLookup().fetch(KEY1, KEY2, KEY3, KEY4, KEY5);
 assertEquals(foundEntities.get(0).getKey(), KEY1);
 assertEquals(foundEntities.get(1).getKey(), KEY2);
 assertEquals(foundEntities.get(2).getKey(), KEY3);
 assertEquals(foundEntities.get(3).getKey(), KEY4);
 assertEquals(foundEntities.get(4).getKey(), KEY5);
 assertEquals(foundEntities.size(), 5);
}

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

@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

@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

@Test
public void testLookupEventualConsistency() {
 ReadOptions readOption =
   ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build();
 com.google.datastore.v1.Key key =
   com.google.datastore.v1.Key.newBuilder()
     .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build())
     .addPath(
       com.google.datastore.v1.Key.PathElement.newBuilder()
         .setKind("kind1")
         .setName("name")
         .build())
     .build();
 LookupRequest lookupRequest =
   LookupRequest.newBuilder().setReadOptions(readOption).addKeys(key).build();
 EasyMock.expect(rpcMock.lookup(lookupRequest))
   .andReturn(LookupResponse.newBuilder().build())
   .times(3);
 EasyMock.replay(rpcFactoryMock, rpcMock);
 Datastore datastore = rpcMockOptions.getService();
 datastore.get(KEY1, ReadOption.eventualConsistency());
 datastore.get(ImmutableList.of(KEY1), ReadOption.eventualConsistency());
 datastore.fetch(ImmutableList.of(KEY1), ReadOption.eventualConsistency());
 EasyMock.verify(rpcFactoryMock, rpcMock);
}

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

@Test
public void testNewTransactionRollback() {
 Transaction transaction = datastore.newTransaction();
 transaction.add(ENTITY3);
 Entity entity2 =
   Entity.newBuilder(ENTITY2)
     .clear()
     .setNull("bla")
     .set("list3", StringValue.of("bla"), StringValue.newBuilder("bla").build())
     .build();
 transaction.update(entity2);
 transaction.delete(KEY1);
 transaction.rollback();
 transaction.rollback(); // should be safe to repeat rollback calls
 try {
  transaction.commit();
  fail("Expecting a failure");
 } catch (DatastoreException ex) {
  // expected to fail
 }
 verifyNotUsable(transaction);
 List<Entity> list = datastore.fetch(KEY1, KEY2, KEY3);
 assertEquals(ENTITY1, list.get(0));
 assertEquals(ENTITY2, list.get(1));
 assertNull(list.get(2));
 assertEquals(3, list.size());
}

代码示例来源: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 testNewTransactionRollback() {
 Transaction transaction = DATASTORE.newTransaction();
 transaction.add(ENTITY3);
 Entity entity2 =
   Entity.newBuilder(ENTITY2)
     .clear()
     .setNull("bla")
     .set("list3", StringValue.of("bla"), StringValue.newBuilder("bla").build())
     .build();
 transaction.update(entity2);
 transaction.delete(KEY1);
 transaction.rollback();
 transaction.rollback(); // should be safe to repeat rollback calls
 try {
  transaction.commit();
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  assertEquals("FAILED_PRECONDITION", expected.getReason());
 }
 List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3);
 assertEquals(ENTITY1, list.get(0));
 assertEquals(ENTITY2, list.get(1));
 assertNull(list.get(2));
 assertEquals(3, list.size());
}

代码示例来源: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 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();
 assertFalse(transaction.isActive());
 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 expected) {
  assertEquals("FAILED_PRECONDITION", expected.getReason());
 }
 try {
  transaction.rollback();
  fail("Expecting a failure");
 } catch (DatastoreException expected) {
  assertEquals("FAILED_PRECONDITION", expected.getReason());
 }
}

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

datastore.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
assertEquals(entity1, entities.get(0));
assertEquals(entity2, entities.get(1));
batch.update(ENTITY1, ENTITY2, ENTITY3);
batch.submit();
entities = datastore.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey());
assertEquals(ENTITY1, entities.get(0));
assertEquals(ENTITY2, entities.get(1));

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

DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
assertEquals(entity1, entities.get(0));
assertEquals(entity2, entities.get(1));
batch.submit();
entities =
  DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
assertEquals(ENTITY1, entities.get(0));
assertEquals(ENTITY2, entities.get(1));

代码示例来源:origin: spotify/styx

@Override
public List<Entity> fetch(Iterable<Key> keys, ReadOption... readOptions) {
 keys.forEach(key -> stats.recordDatastoreEntityReads(key.getKind(), 1));
 return delegate.fetch(keys, readOptions);
}

相关文章