Search in sources :

Example 46 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class DatastoreExample method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    String projectId = args.length > 0 ? args[0] : null;
    // If you want to access a local Datastore running via the Google Cloud SDK, do
    //   DatastoreOptions options = DatastoreOptions.newBuilder()
    //       .setProjectId(projectId)
    //       .setNamespace(NAMESPACE)
    //       // change 8080 to the port that the emulator listens to
    //       .setHost("http://localhost:8080")
    //       .build();
    DatastoreOptions options = DatastoreOptions.newBuilder().setProjectId(projectId).setNamespace(NAMESPACE).build();
    String name = args.length > 1 ? args[1] : System.getProperty("user.getName");
    Datastore datastore = options.getService();
    KeyFactory keyFactory = datastore.newKeyFactory().setKind(USER_KIND);
    Key key = keyFactory.newKey(name);
    String actionName = args.length > 2 ? args[2].toLowerCase() : DEFAULT_ACTION;
    DatastoreAction action = ACTIONS.get(actionName);
    if (action == null) {
        System.out.println("Unrecognized action.");
        printUsage();
        return;
    }
    args = args.length > 3 ? Arrays.copyOfRange(args, 3, args.length) : new String[] {};
    Transaction tx = datastore.newTransaction();
    Object request;
    try {
        request = action.parse(args);
    } catch (IllegalArgumentException ex) {
        System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
        System.out.printf("Expected: %s%n", action.params());
        return;
    } catch (Exception ex) {
        System.out.println("Failed to parse request.");
        ex.printStackTrace();
        return;
    }
    try {
        action.run(tx, key, request);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
}
Also used : Datastore(com.google.cloud.datastore.Datastore) Transaction(com.google.cloud.datastore.Transaction) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 47 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreSnippets method testAllocateIdSingle.

@Test
public void testAllocateIdSingle() {
    Key key = datastoreSnippets.allocateIdSingle();
    assertNotNull(key);
}
Also used : Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 48 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class ITQuerySnippets method beforeClass.

@BeforeClass
public static void beforeClass() {
    datastore = DatastoreOptions.getDefaultInstance().getService();
    Key key1 = Key.newBuilder(datastore.getOptions().getProjectId(), KIND, "key1").build();
    Key key2 = Key.newBuilder(datastore.getOptions().getProjectId(), KIND, "key2").build();
    entity1 = Entity.newBuilder(key1).set("description", "entity1").build();
    entity2 = Entity.newBuilder(key2).set("description", "entity2").build();
    datastore.put(entity1, entity2);
}
Also used : Key(com.google.cloud.datastore.Key) BeforeClass(org.junit.BeforeClass)

Example 49 with Key

use of com.google.cloud.datastore.Key in project google-cloud-java by GoogleCloudPlatform.

the class ITTransactionSnippets method testAddGetMultipleDeferredId.

@Test
public void testAddGetMultipleDeferredId() {
    Transaction transaction = datastore.newTransaction();
    TransactionSnippets transactionSnippets = new TransactionSnippets(transaction);
    List<Key> keys = transactionSnippets.multipleAddEntitiesDeferredId();
    assertEquals(2, keys.size());
    Key key1 = keys.get(0);
    registerKey(key1);
    Entity entity1 = datastore.get(key1);
    assertEquals("value1", entity1.getString("propertyName"));
    Key key2 = keys.get(1);
    registerKey(key2);
    Entity entity2 = datastore.get(key2);
    assertEquals("value2", entity2.getString("propertyName"));
}
Also used : Entity(com.google.cloud.datastore.Entity) Transaction(com.google.cloud.datastore.Transaction) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Aggregations

Key (com.google.cloud.datastore.Key)49 Entity (com.google.cloud.datastore.Entity)37 IncompleteKey (com.google.cloud.datastore.IncompleteKey)32 Datastore (com.google.cloud.datastore.Datastore)19 KeyFactory (com.google.cloud.datastore.KeyFactory)17 FullEntity (com.google.cloud.datastore.FullEntity)16 Test (org.junit.Test)15 Transaction (com.google.cloud.datastore.Transaction)9 DatastoreException (com.google.cloud.datastore.DatastoreException)4 ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)3 Batch (com.google.cloud.datastore.Batch)2 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 BeforeClass (org.junit.BeforeClass)1