Java idiomatic client for Google Cloud Platform services.
- [Homepage] (https://googlecloudplatform.github.io/google-cloud-java/)
- [API Documentation] (https://googlecloudplatform.github.io/google-cloud-java/apidocs)
This client supports the following Google Cloud Platform services at a Beta quality level:
- [Google Cloud BigQuery] (#google-cloud-bigquery-beta) (Beta)
- [Stackdriver Logging] (#stackdriver-logging-beta) (Beta - Not working on App Engine Standard)
- [Google Cloud Datastore] (#google-cloud-datastore-beta) (Beta)
- [Google Cloud Storage] (#google-cloud-storage-beta) (Beta)
- [Cloud Spanner] (#cloud-spanner-beta) (Beta)
This client supports the following Google Cloud Platform services at an Alpha quality level:
- [Google Cloud Compute] (#google-cloud-compute-alpha) (Alpha)
- [Google Cloud DNS] (#google-cloud-dns-alpha) (Alpha)
- [Google Cloud Pub/Sub] (#google-cloud-pubsub-alpha) (Alpha - Not working on App Engine Standard)
- [Google Cloud Resource Manager] (#google-cloud-resource-manager-alpha) (Alpha)
- [Google Cloud Translate] (#google-translate-alpha) (Alpha)
Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.
gcloud-java
lives on under a new name, google-cloud
.
Your code will behave the same, simply change your dependency (see Quickstart).
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud</artifactId>
<version>0.9.3-alpha</version>
</dependency>
If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:google-cloud:0.9.3-alpha'
If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud" % "0.9.3-alpha"
BigQueryExample
- A simple command line interface providing some of Cloud BigQuery's functionality- Read more about using this application on the
BigQueryExample
docs page.
- Read more about using this application on the
ComputeExample
- A simple command line interface providing some of Cloud Compute's functionality- Read more about using this application on the
google-cloud-examples
docs page.
- Read more about using this application on the
Bookshelf
- An App Engine app that manages a virtual bookshelf.- This app uses
google-cloud
to interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, another Google Cloud Platform service.
- This app uses
DatastoreExample
- A simple command line interface for Cloud Datastore- Read more about using this application on the
DatastoreExample
docs page.
- Read more about using this application on the
DnsExample
- A simple command line interface for Cloud DNS- Read more about using this application on the
DnsExample
docs page.
- Read more about using this application on the
Flexible Environment/Datastore example
- A simple app that uses Cloud Datastore to list the last 10 IP addresses that visited your site.- Read about how to run the application here.
Flexible Environment/Storage example
- An app that uploads files to a public Cloud Storage bucket on the App Engine Flexible Environment runtime.GuestBook
- An App Engine Standard guestbook that uses Cloud Datastore.LoggingExample
- A simple command line interface providing some of Stackdriver Logging's functionality- Read more about using this application on the
LoggingExample
docs page.
- Read more about using this application on the
PubSubExample
- A simple command line interface providing some of Cloud Pub/Sub's functionality- Read more about using this application on the
PubSubExample
docs page.
- Read more about using this application on the
ResourceManagerExample
- A simple command line interface providing some of Cloud Resource Manager's functionality- Read more about using this application on the
ResourceManagerExample
docs page.
- Read more about using this application on the
SparkDemo
- An example of usinggoogle-cloud-datastore
from within the SparkJava and App Engine Flexible Environment frameworks.- Read about how it works on the example's README page.
StorageExample
- A simple command line interface providing some of Cloud Storage's functionality- Read more about using this application on the
StorageExample
docs page.
- Read more about using this application on the
TaskList
- A command line application that uses Cloud Datastore to manage a to-do list.- Read about how to run the application on its README page.
TranslateExample
- A simple command line interface providing some of Google Translate's functionality- Read more about using this application on the
TranslateExample
docs page.
- Read more about using this application on the
Most google-cloud
libraries require a project ID. There are multiple ways to specify this project ID.
- When using
google-cloud
libraries from within Compute/App Engine, there's no need to specify a project ID. It is automatically inferred from the production environment. - When using
google-cloud
elsewhere, you can do one of the following:
- Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
Datastore datastore = DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService();
- Specify the environment variable
GOOGLE_CLOUD_PROJECT
to be your desired project ID. - Set the project ID using the Google Cloud SDK. To use the SDK, download the SDK if you haven't already, and set the project ID from the command line. For example:
gcloud config set project PROJECT_ID
google-cloud
determines the project ID from the following sources in the listed order, stopping once it finds a value:
- The project ID supplied when building the service options
- Project ID specified by the environment variable
GOOGLE_CLOUD_PROJECT
- The App Engine project ID
- The project ID specified in the JSON credentials file pointed by the
GOOGLE_APPLICATION_CREDENTIALS
environment variable - The Google Cloud SDK project ID
- The Compute Engine project ID
google-cloud-java
uses
https://github.com/google/google-auth-library-java
to authenticate requests. google-auth-library-java
supports a wide range of authentication types;
see the project's README
and javadoc for more
details.
To access Google Cloud services, you first need to ensure that the necessary Google Cloud APIs are enabled for your project. To do this, follow the instructions on the authentication document shared by all the Google Cloud language libraries.
Next, choose a method for authenticating API requests from within your project:
- When using
google-cloud
libraries from within Compute/App Engine, no additional authentication steps are necessary. For example:
Storage storage = StorageOptions.getDefaultInstance().getService();
- When using
google-cloud
libraries elsewhere, there are several options:
- Generate a JSON service account key.
After downloading that key, you must do one of the following:
- Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json
- Supply the JSON credentials file when building the service options. For example, this Storage object has the necessary permissions to interact with your Google Cloud Storage data:
Storage storage = StorageOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/my/key.json"))) .build() .getService();
- If running locally for development/testing, you can use the
Google Cloud SDK. Create Application Default Credentials with
gcloud auth application-default login
, and thengoogle-cloud
will automatically detect such credentials. - If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case, the access token will not be automatically refreshed):
Storage storage = StorageOptions.newBuilder()
.setCredentials(new GoogleCredentials(new AccessToken(accessToken, expirationTime)))
.build()
.getService();
If no credentials are provided, google-cloud
will attempt to detect them from the environment
using GoogleCredentials.getApplicationDefault()
which will search for Default Application
Credentials in the following locations (in order):
- The credentials file pointed to by the
GOOGLE_APPLICATION_CREDENTIALS
environment variable - Credentials provided by the Google Cloud SDK
gcloud auth application-default login
command - Google App Engine built-in credentials
- Google Cloud Shell built-in credentials
- Google Compute Engine built-in credentials
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere. Complete source code can be found at CreateTableAndLoadData.java.
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FormatOptions;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
TableId tableId = TableId.of("dataset", "table");
Table table = bigquery.getTable(tableId);
if (table == null) {
System.out.println("Creating table " + tableId);
Field integerField = Field.of("fieldName", Field.Type.integer());
Schema schema = Schema.of(integerField);
table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
}
System.out.println("Loading data into table " + tableId);
Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
loadJob = loadJob.waitFor();
if (loadJob.getStatus().getError() != null) {
System.out.println("Job completed with errors");
} else {
System.out.println("Job succeeded");
}
Follow the activation instructions to use the Stackdriver Logging API with your project.
Here are two code snippets showing simple usage examples from within Compute Engine/App Engine Flexible. Note that you must supply credentials and a project ID if running this snippet elsewhere.
The first snippet shows how to write and list log entries. Complete source code can be found on WriteAndListLogEntries.java.
import com.google.cloud.MonitoredResource;
import com.google.cloud.Page;
import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.EntryListOption;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Payload.StringPayload;
import java.util.Collections;
import java.util.Iterator;
LoggingOptions options = LoggingOptions.getDefaultInstance();
try(Logging logging = options.getService()) {
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
.setLogName("test-log")
.setResource(MonitoredResource.newBuilder("global")
.addLabel("project_id", options.getProjectId())
.build())
.build();
logging.write(Collections.singleton(firstEntry));
Page<LogEntry> entries = logging.listLogEntries(
EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
Iterator<LogEntry> entryIterator = entries.iterateAll();
while (entryIterator.hasNext()) {
System.out.println(entryIterator.next());
}
}
The second snippet shows how to use a java.util.logging.Logger
to write log entries to Stackdriver
Logging. The snippet installs a Stackdriver Logging handler using
LoggingHandler.addHandler(Logger, LoggingHandler)
. Notice that this could also be done through the
logging.properties
file, adding the following line:
com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler
The complete code can be found on AddLoggingHandler.java.
import com.google.cloud.logging.LoggingHandler;
import java.util.logging.Logger;
Logger logger = Logger.getLogger(AddLoggingHandler.class.getName());
LoggingHandler.addHandler(logger, new LoggingHandler());
logger.warning("test warning");
Follow the activation instructions to use the Google Cloud Datastore API with your project.
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere.
The first snippet shows how to create a Datastore entity. Complete source code can be found at CreateEntity.java.
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DateTime;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
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", DateTime.now())
.build();
datastore.put(entity);
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be found at UpdateEntity.java.
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DateTime;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
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", DateTime.now())
.build();
datastore.update(entity);
}
Follow the activation instructions to use the Google Cloud Storage API with your project.
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere.
The first snippet shows how to create a Storage blob. Complete source code can be found at CreateBlob.java.
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
Storage storage = StorageOptions.getDefaultInstance().getService();
BlobId blobId = BlobId.of("bucket", "blob_name");
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
The second snippet shows how to update a Storage blob if it exists. Complete source code can be found at UpdateBlob.java.
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
Storage storage = StorageOptions.getDefaultInstance().getService();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = storage.get(blobId);
if (blob != null) {
byte[] prevContent = blob.getContent();
System.out.println(new String(prevContent, UTF_8));
WritableByteChannel channel = blob.writer();
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
channel.close();
}
Here is a code snippet showing a simple usage example from within Compute/App Engine Flex. Note that you must supply credentials and a project ID if running this snippet elsewhere.
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;
// Instantiates a client
SpannerOptions options = SpannerOptions.newBuilder().build();
Spanner spanner = options.getService();
String instance = "my-instance";
String database = "my-database";
try {
// Creates a database client
DatabaseClient dbClient = spanner.getDatabaseClient(
DatabaseId.of(options.getProjectId(), instance, database));
// Queries the database
ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
// Prints the results
while (resultSet.next()) {
System.out.printf("%d\n", resultSet.getLong(0));
}
} finally {
// Closes the client which will free up the resources used
spanner.closeAsync().get();
}
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere.
The first snippet shows how to create a snapshot from an existing disk. Complete source code can be found at CreateSnapshot.java.
import com.google.cloud.compute.Compute;
import com.google.cloud.compute.ComputeOptions;
import com.google.cloud.compute.Disk;
import com.google.cloud.compute.DiskId;
import com.google.cloud.compute.Snapshot;
Compute compute = ComputeOptions.getDefaultInstance().getService();
DiskId diskId = DiskId.of("us-central1-a", "disk-name");
Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields());
if (disk != null) {
String snapshotName = "disk-name-snapshot";
Operation operation = disk.createSnapshot(snapshotName);
operation = operation.waitFor();
if (operation.getErrors() == null) {
// use snapshot
Snapshot snapshot = compute.getSnapshot(snapshotName);
}
}
The second snippet shows how to create a virtual machine instance. Complete source code can be found at CreateInstance.java.
import com.google.cloud.compute.AttachedDisk;
import com.google.cloud.compute.Compute;
import com.google.cloud.compute.ComputeOptions;
import com.google.cloud.compute.ImageId;
import com.google.cloud.compute.Instance;
import com.google.cloud.compute.InstanceId;
import com.google.cloud.compute.InstanceInfo;
import com.google.cloud.compute.MachineTypeId;
import com.google.cloud.compute.NetworkId;
Compute compute = ComputeOptions.getDefaultInstance().getService();
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
NetworkId networkId = NetworkId.of("default");
AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
NetworkInterface networkInterface = NetworkInterface.of(networkId);
InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name");
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
Operation operation =
compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
operation = operation.waitFor();
if (operation.getErrors() == null) {
// use instance
Instance instance = compute.getInstance(instanceId);
}
Follow the activation instructions to use the Google Cloud DNS API with your project.
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere.
The first snippet shows how to create a zone resource. Complete source code can be found on CreateZone.java.
import com.google.cloud.dns.Dns;
import com.google.cloud.dns.DnsOptions;
import com.google.cloud.dns.Zone;
import com.google.cloud.dns.ZoneInfo;
Dns dns = DnsOptions.getDefaultInstance().getService();
String zoneName = "my-unique-zone";
String domainName = "someexampledomain.com.";
String description = "This is a google-cloud-dns sample zone.";
ZoneInfo zoneInfo = ZoneInfo.of(zoneName, domainName, description);
Zone zone = dns.create(zoneInfo);
The second snippet shows how to create records inside a zone. The complete code can be found on CreateOrUpdateRecordSets.java.
import com.google.cloud.dns.ChangeRequestInfo;
import com.google.cloud.dns.Dns;
import com.google.cloud.dns.DnsOptions;
import com.google.cloud.dns.RecordSet;
import com.google.cloud.dns.Zone;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
Dns dns = DnsOptions.getDefaultInstance().getService();
String zoneName = "my-unique-zone";
Zone zone = dns.getZone(zoneName);
String ip = "12.13.14.15";
RecordSet toCreate = RecordSet.newBuilder("www.someexampledomain.com.", RecordSet.Type.A)
.setTtl(24, TimeUnit.HOURS)
.addRecord(ip)
.build();
ChangeRequestInfo.Builder changeBuilder = ChangeRequestInfo.newBuilder().add(toCreate);
// Verify that the record does not exist yet.
// If it does exist, we will overwrite it with our prepared record.
Iterator<RecordSet> recordSetIterator = zone.listRecordSets().iterateAll();
while (recordSetIterator.hasNext()) {
RecordSet current = recordSetIterator.next();
if (toCreate.getName().equals(current.getName()) &&
toCreate.getType().equals(current.getType())) {
changeBuilder.delete(current);
}
}
ChangeRequestInfo changeRequest = changeBuilder.build();
zone.applyChangeRequest(changeRequest);
Here is a code snippet showing a simple usage example from within Compute Engine/App Engine Flexible. Note that you must supply credentials and a project ID if running this snippet elsewhere. Complete source code can be found at CreateSubscriptionAndPullMessages.java.
import com.google.cloud.pubsub.Message;
import com.google.cloud.pubsub.PubSub;
import com.google.cloud.pubsub.PubSub.MessageConsumer;
import com.google.cloud.pubsub.PubSub.MessageProcessor;
import com.google.cloud.pubsub.PubSubOptions;
import com.google.cloud.pubsub.Subscription;
import com.google.cloud.pubsub.SubscriptionInfo;
try (PubSub pubsub = PubSubOptions.getDefaultInstance().getService()) {
Subscription subscription =
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
MessageProcessor callback = new MessageProcessor() {
@Override
public void process(Message message) throws Exception {
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
}
};
// Create a message consumer and pull messages (for 60 seconds)
try (MessageConsumer consumer = subscription.pullAsync(callback)) {
Thread.sleep(60_000);
}
}
Here is a code snippet showing a simple usage example. Note that you must supply Google SDK credentials for this service, not other forms of authentication listed in the Authentication section. Complete source code can be found at UpdateAndListProjects.java.
import com.google.cloud.resourcemanager.Project;
import com.google.cloud.resourcemanager.ResourceManager;
import com.google.cloud.resourcemanager.ResourceManagerOptions;
import java.util.Iterator;
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
Project project = resourceManager.get("some-project-id"); // Use an existing project's ID
if (project != null) {
Project newProject = project.toBuilder()
.addLabel("launch-status", "in-development")
.build()
.replace();
System.out.println("Updated the labels of project " + newProject.getProjectId()
+ " to be " + newProject.getLabels());
}
Iterator<Project> projectIterator = resourceManager.list().iterateAll();
System.out.println("Projects I can view:");
while (projectIterator.hasNext()) {
System.out.println(projectIterator.next().getProjectId());
}
Here's a snippet showing a simple usage example. The example shows how to detect the language of
some text and how to translate some text. The example assumes that either default application
credentials or a valid api key are available. An api key stored in the GOOGLE_API_KEY
environment
variable will be automatically detected. Alternatively, you can use the apiKey(String)
setter in
TranslateOptions.Builder
. Complete source code can be found at
DetectLanguageAndTranslate.java.
import com.google.cloud.translate.Detection;
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
Translate translate = TranslateOptions.getDefaultInstance().getService();
Detection detection = translate.detect("Hola");
String detectedLanguage = detection.getLanguage();
Translation translation = translate.translate(
"World",
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage(detectedLanguage));
System.out.printf("Hola %s%n", translation.getTranslatedText());
To get help, follow the instructions in the shared Troubleshooting document.
Java 7 or above is required for using this client.
This library provides tools to help write tests for code that uses google-cloud services.
See TESTING to read more about using our testing helpers.
This library follows [Semantic Versioning] (http://semver.org/).
Please note it is currently under active development. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.
Beta: Libraries defined at a Beta quality level are expected to be mostly stable and we're working towards their release candidate. We will address issues and requests with a higher priority.
Alpha: Libraries defined at an Alpha quality level are still a work-in-progress and are more likely to get backwards-incompatible updates.
Contributions to this library are always welcome and highly encouraged.
See google-cloud
's CONTRIBUTING documentation and the shared documentation for more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.
Apache 2.0 - See LICENSE for more information.