com.google.cloud.datastore.Datastore类的使用及代码示例

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

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

Datastore介绍

[英]An interface for Google Cloud Datastore.
[中]谷歌云数据存储的接口。

代码示例

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

/** Deletes all entities in the namespace associated with this {@link RemoteDatastoreHelper}. */
public void deleteNamespace() {
 StructuredQuery<Key> query = Query.newKeyQueryBuilder().setNamespace(namespace).build();
 QueryResults<Key> keys = datastore.run(query);
 while (keys.hasNext()) {
  datastore.delete(keys.next());
 }
}

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

@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

KeyFactory keyFactory = datastore.newKeyFactory().setKind("Person");
Key key = keyFactory.newKey("john.doe@gmail.com");
Entity entity =
  Entity.newBuilder(key)
    .set("name", "John Doe")
    .set("age", 51)
    .set("favorite_food", "pizza")
    .build();
datastore.put(entity);
Entity johnEntity = datastore.get(key);
Key janeKey = keyFactory.newKey("jane.doe@gmail.com");
Entity janeEntity =
  Entity.newBuilder(janeKey)
  Entity.newBuilder(joeKey)
datastore.put(janeEntity, joeEntity);
  Query.newEntityQueryBuilder()
    .setKind("Person")
    .setFilter(PropertyFilter.eq("favorite_food", "pizza"))
    .build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
 Entity currentEntity = results.next();

代码示例来源: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: google/data-transfer-project

/**
 * Inserts a new {@link PortabilityJob} keyed by {@code jobId} in Datastore.
 *
 * <p>To update an existing {@link PortabilityJob} instead, use {@link JobStore#update}.
 *
 * @throws IOException if a job already exists for {@code jobId}, or if there was a different
 * problem inserting the job.
 */
@Override
public void createJob(UUID jobId, PortabilityJob job) throws IOException {
 Preconditions.checkNotNull(jobId);
 Transaction transaction = datastore.newTransaction();
 Entity shouldNotExist = transaction.get(getKey(jobId));
 if (shouldNotExist != null) {
  transaction.rollback();
  throw new IOException(
    "Record already exists for jobID: " + jobId + ". Record: " + shouldNotExist);
 }
 Entity entity = createEntity(jobId, job.toMap());
 try {
  transaction.put(entity);
 } catch (DatastoreException e) {
  transaction.rollback();
  throw new IOException(
    "Could not create initial record for jobID: " + jobId + ". Record: " + entity, e);
 }
 transaction.commit();
}

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

/** Example of running a query to find all entities with an ancestor. */
// [TARGET run(Query)]
// [VARIABLE "my_parent_key_name"]
public List<Entity> run(String parentKeyName) {
 Datastore datastore = transaction.getDatastore();
 // [START run]
 KeyFactory keyFactory = datastore.newKeyFactory().setKind("ParentKind");
 Key parentKey = keyFactory.newKey(parentKeyName);
 // Build a query
 Query<Entity> query =
   Query.newEntityQueryBuilder()
     .setKind("MyKind")
     .setFilter(PropertyFilter.hasAncestor(parentKey))
     .build();
 QueryResults<Entity> results = transaction.run(query);
 List<Entity> entities = Lists.newArrayList();
 while (results.hasNext()) {
  Entity result = results.next();
  // do something with result
  entities.add(result);
 }
 transaction.commit();
 // [END run]
 return entities;
}

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

/** Example of updating multiple entities. */
// [TARGET update(Entity...)]
// [VARIABLE "my_key_name1"]
// [VARIABLE "my_key_name2"]
public void multipleUpdateEntities(String keyName1, String keyName2) {
 Datastore datastore = transaction.getDatastore();
 // [START multipleUpdateEntities]
 Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1);
 Entity.Builder entityBuilder1 = Entity.newBuilder(key1);
 entityBuilder1.set("propertyName", "value3");
 Entity entity1 = entityBuilder1.build();
 Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2);
 Entity.Builder entityBuilder2 = Entity.newBuilder(key2);
 entityBuilder2.set("propertyName", "value4");
 Entity entity2 = entityBuilder2.build();
 transaction.update(entity1, entity2);
 transaction.commit();
 // [END multipleUpdateEntities]
}

代码示例来源: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 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 running a query to find all entities of one kind. */
// [TARGET run(Query, ReadOption...)]
// [VARIABLE "my_kind"]
public List<Entity> runQuery(String kind) {
 // TODO change so that it's not necessary to hold the entities in a list for integration testing
 // [START runQuery]
 StructuredQuery<Entity> query = Query.newEntityQueryBuilder().setKind(kind).build();
 QueryResults<Entity> results = datastore.run(query);
 List<Entity> entities = Lists.newArrayList();
 while (results.hasNext()) {
  Entity result = results.next();
  // do something with result
  entities.add(result);
 }
 // [END runQuery]
 return entities;
}

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

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

相关文章