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

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

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

Datastore.newKeyFactory介绍

[英]Returns a new KeyFactory for this service

Example of creating a KeyFactory.

KeyFactory keyFactory = datastore.newKeyFactory();

[中]返回此服务的新KeyFactory
创建KeyFactory的示例。

KeyFactory keyFactory = datastore.newKeyFactory();

代码示例

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

/** Example of creating a {@code KeyFactory}. */
// [TARGET newKeyFactory()]
public KeyFactory createKeyFactory() {
 // [START createKeyFactory]
 KeyFactory keyFactory = datastore.newKeyFactory();
 // [END createKeyFactory]
 return keyFactory;
}

代码示例来源:origin: google/data-transfer-project

private Key getKey(UUID jobId) {
 return datastore.newKeyFactory().setKind(KIND).newKey(jobId.toString());
}

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

/** Example of deleting multiple entities. */
// [TARGET delete(Key...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void batchDeleteEntities(String keyName1, String keyName2) {
 // [START batchDeleteEntities]
 Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 datastore.delete(key1, key2);
 // [END batchDeleteEntities]
}

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

/** Example of allocating an id. */
// [TARGET allocateId(IncompleteKey)]
public Key allocateIdSingle() {
 // [START allocateIdSingle]
 KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
 IncompleteKey incompleteKey = keyFactory.newKey();
 // let cloud datastore automatically assign an id
 Key key = datastore.allocateId(incompleteKey);
 // [END allocateIdSingle]
 return key;
}

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

/** Example of getting an entity. */
// [TARGET get(Key, ReadOption...)]
// [VARIABLE "my_key_name"]
public Entity getEntityWithKey(String keyName) {
 // [START getEntityWithKey]
 Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
 Entity entity = datastore.get(key);
 // Do something with the entity
 // [END getEntityWithKey]
 return entity;
}

代码示例来源:origin: google/data-transfer-project

private Key getDataKey(UUID jobId, String key) {
 return datastore.newKeyFactory().setKind(KIND).newKey(getDataKeyName(jobId, key));
}

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

/** Example of deleting multiple entities. */
// [TARGET delete(Key...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleDeleteEntities(String keyName1, String keyName2) {
 Datastore datastore = transaction.getDatastore();
 // [START multipleDeleteEntities]
 Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 transaction.delete(key1, key2);
 transaction.commit();
 // [END multipleDeleteEntities]
}

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

/** Example of allocating multiple ids in a single batch. */
// [TARGET allocateId(IncompleteKey...)]
public List<Key> batchAllocateId() {
 // [START batchAllocateId]
 KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
 IncompleteKey incompleteKey1 = keyFactory.newKey();
 IncompleteKey incompleteKey2 = keyFactory.newKey();
 // let cloud datastore automatically assign the ids
 List<Key> keys = datastore.allocateId(incompleteKey1, incompleteKey2);
 // [END batchAllocateId]
 return keys;
}

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

/** Example of getting an entity for a given key. */
// [TARGET get(Key)]
// [VARIABLE "my_key_name"]
public Entity get(String keyName) {
 Datastore datastore = transaction.getDatastore();
 // [START get]
 Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
 Entity entity = transaction.get(key);
 transaction.commit();
 // Do something with the entity
 // [END get]
 return entity;
}

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

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

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

/** 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

/** 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

@Test
public void testAllocateIdArray() {
 KeyFactory keyFactory = DATASTORE.newKeyFactory().setKind(KIND1);
 IncompleteKey incompleteKey1 = keyFactory.newKey();
 IncompleteKey incompleteKey2 =
   keyFactory.setKind(KIND2).addAncestors(PathElement.of(KIND1, 10)).newKey();
 List<Key> result = DATASTORE.allocateId(incompleteKey1, incompleteKey2, incompleteKey1);
 assertEquals(3, result.size());
 assertEquals(Key.newBuilder(incompleteKey1, result.get(0).getId()).build(), result.get(0));
 assertEquals(Key.newBuilder(incompleteKey1, result.get(2).getId()).build(), result.get(2));
 assertEquals(Key.newBuilder(incompleteKey2, result.get(1).getId()).build(), result.get(1));
}

代码示例来源: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 testKeyFactory() {
 KeyFactory keyFactory = datastore.newKeyFactory().setKind(KIND1);
 assertEquals(INCOMPLETE_KEY1, keyFactory.newKey());
 assertEquals(
   IncompleteKey.newBuilder(INCOMPLETE_KEY1).setKind(KIND2).build(),
   datastore.newKeyFactory().setKind(KIND2).newKey());
 assertEquals(KEY1, keyFactory.newKey("name"));
 assertEquals(Key.newBuilder(KEY1).setId(2).build(), keyFactory.newKey(2));
}

代码示例来源: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 testAllocateId() {
 KeyFactory keyFactory = DATASTORE.newKeyFactory().setKind(KIND1);
 IncompleteKey pk1 = keyFactory.newKey();
 Key key1 = DATASTORE.allocateId(pk1);
 assertEquals(key1.getProjectId(), pk1.getProjectId());
 assertEquals(key1.getNamespace(), pk1.getNamespace());
 assertEquals(key1.getAncestors(), pk1.getAncestors());
 assertEquals(key1.getKind(), pk1.getKind());
 assertTrue(key1.hasId());
 assertFalse(key1.hasName());
 assertEquals(Key.newBuilder(pk1, key1.getId()).build(), key1);
 Key key2 = DATASTORE.allocateId(pk1);
 assertNotEquals(key1, key2);
 assertEquals(Key.newBuilder(pk1, key2.getId()).build(), key2);
}

相关文章