Skip to content

A modern and feature-rich Asynchronous Java client for Google Cloud Datastore

License

Notifications You must be signed in to change notification settings

campers/async-datastore-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quasar Fiber blocking Google Datastore Client

Forked from https://github.com/spotify/async-datastore-client

A modern, feature-rich and tunable Java client library for Google Cloud Datastore.

Requirements

Features

  • Synchronous API design using Quasar that uses light-weight fiber 'threads' for asynchronous I/O under the covers
  • Insulates the consumer from having to deal with Protobuf payloads.
  • Includes a simple QueryBuilder to construct natural looking queries.

Overview

The current implementations of Google Datastore Client and Google Cloud Java Client use blocking synchronous HTTP calls to their backend. This client uses FiberCloseableHttpAsyncClient which is Quasar fiber blocking version of the Apache Http Client

Usage

Add this to your pom.xml file

<dependency>
  <groupId>com.spotify</groupId>
  <artifactId>comsat-datastore-client</artifactId>
  <version>1.0.0</version>
</dependency>

Example: Insert an entity

import com.spotify.asyncdatastoreclient.DatastoreConfig;
import com.spotify.asyncdatastoreclient.Datastore;
import com.spotify.asyncdatastoreclient.QueryBuilder;
import com.spotify.asyncdatastoreclient.Insert;
import com.spotify.asyncdatastoreclient.MutationResult;

import com.google.api.services.datastore.client.DatastoreHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;

final DatastoreConfig config = DatastoreConfig.builder()
    .requestTimeout(1000)
    .requestRetry(3)
    .dataset(DATASET_ID);
    .credential(DatastoreHelper.getServiceAccountCredential(ACCOUNT, KEY_PATH))
    .build();

final Datastore datastore = Datastore.create(config);

final Insert insert = QueryBuilder.insert("employee", 1234567L)
    .value("fullname", "Fred Blinge")
    .value("age", 40)
    .value("workdays", ImmutableList.of("Monday", "Tuesday", "Friday"));


final MutationResult result = datastore.execute(insert);

Example: Query entities

import com.spotify.asyncdatastoreclient.QueryBuilder;
import com.spotify.asyncdatastoreclient.Query;

import static com.spotify.asyncdatastoreclient.QueryBuilder.eq;
import static com.spotify.asyncdatastoreclient.QueryBuilder.asc;

final Query query = QueryBuilder.query()
    .kindOf("employee")
    .filterBy(eq("role", "engineer"))
    .orderBy(asc("age"));

// call datastore.executeAsync() to get a ListenableFuture<QueryResult>
for (final Entity entity : datastore.execute(query)) {
  System.out.println("Name: " + entity.getString("fullname));
  ...
}

Building

mvn clean compile

Running tests

By default integration tests are executed against a Local Development Server on port 8080. To run tests, first download the Development Server and create the local datastore as follows:

gcd.sh create -d async-test test-project

This will create a project called test-project with a dataset ID of async-test. You then start the server as follows:

gcd.sh start --consistency=1.0 test-project

NOTE: The --consistency=1.0 option is sometimes necessary in order for unit tests to run successful.

All integration tests may by run with maven as follows:

mvn verify

Properties may also be provided to override unit test configuration:

mvn verify -Dhost=https://www.googleapis.com -Ddataset=testing -Daccount=abc@developer.gserviceaccount.com -Dkeypath=./my-key-8ae3ab23d37.p12

License

This software is released under the Apache License 2.0. More information in the file LICENSE distributed with this project.

About

A modern and feature-rich Asynchronous Java client for Google Cloud Datastore

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%