spotify/async-datastore-client

Name: async-datastore-client

Owner: Spotify

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

Created: 2015-06-16 09:29:00.0

Updated: 2018-05-17 08:24:19.0

Pushed: 2018-04-23 16:01:25.0

Homepage: null

Size: 118

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Asynchronous Google Datastore Client

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

Features
Overview

The current implementations of Google Datastore Client and Google Cloud Java Client are synchronous, meaning they block when making HTTP calls to their backend. This client uses async-http-client and returns ListenableFutures which can be nicer to work with, especially running at scale.

Usage

Add this to your pom.xml file

endency>
roupId>com.spotify</groupId>
rtifactId>async-datastore-client</artifactId>
ersion>3.0.2</version>
pendency>

NOTE: Version 3.0.0+ depends on Guava 19 which contains breaking changes to Futures.transform. If you require support for Guava version 18 or lower then use async-datastore-client version 2.1.0.

Example: Insert an entity
rt com.spotify.asyncdatastoreclient.DatastoreConfig;
rt com.spotify.asyncdatastoreclient.Datastore;
rt com.spotify.asyncdatastoreclient.QueryBuilder;
rt com.spotify.asyncdatastoreclient.Insert;
rt com.spotify.asyncdatastoreclient.MutationResult;

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

l DatastoreConfig config = DatastoreConfig.builder()
.requestTimeout(1000)
.requestRetry(3)
.project(PROJECT_ID)
.credential(GoogleCredential
    .fromStream(credentialsInputStream)
    .createScoped(DatastoreConfig.SCOPES))
.build();

l Datastore datastore = Datastore.create(config);

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

or asynchronous call...
l ListenableFuture<MutationResult> resultAsync = datastore.executeAsync(insert);

..or for synchronous
l MutationResult result = datastore.execute(insert);
Example: Query entities
rt com.spotify.asyncdatastoreclient.QueryBuilder;
rt com.spotify.asyncdatastoreclient.Query;

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

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

all datastore.executeAsync() to get a ListenableFuture<QueryResult>
(final Entity entity : datastore.execute(query)) {
stem.out.println("Name: " + entity.getString("fullname));
.

Building
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, at least version 1.4.1 and start the emulator:

ud beta emulators datastore start --host-port localhost:8080 --consistency 1.0 --project async-test --data-dir project-test

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:

verify

Properties may also be provided to override unit test configuration:

verify -Dhost=https://www.googleapis.com -Dproject=testing -Dkeypath=./my-key.json
License

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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.