Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2,092 Commits

Repository files navigation

Google Cloud Java Client

Java idiomatic client for Google Cloud Platform services.

Build StatusCoverage StatusMavenCodacy BadgeDependency Status

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.

Where did gcloud-java go?

gcloud-java lives on under a new name, google-cloud. Your code will behave the same, simply change your dependency (see Quickstart).

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"

Example Applications

Specifying a Project ID

Most google-cloud libraries require a project ID. There are multiple ways to specify this project ID.

  1. 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.
  2. 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:
Datastoredatastore = 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:

  1. The project ID supplied when building the service options
  2. Project ID specified by the environment variable GOOGLE_CLOUD_PROJECT
  3. The App Engine project ID
  4. The project ID specified in the JSON credentials file pointed by the GOOGLE_APPLICATION_CREDENTIALS environment variable
  5. The Google Cloud SDK project ID
  6. The Compute Engine project ID

Authentication

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:

  1. When using google-cloud libraries from within Compute/App Engine, no additional authentication steps are necessary. For example:
Storagestorage = StorageOptions.getDefaultInstance().getService();
  1. 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:
    Storagestorage = StorageOptions.newBuilder()
    .setCredentials(ServiceAccountCredentials.fromStream(newFileInputStream("/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 then google-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):
Storagestorage = StorageOptions.newBuilder()
.setCredentials(newGoogleCredentials(newAccessToken(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):

  1. The credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
  2. Credentials provided by the Google Cloud SDK gcloud auth application-default login command
  3. Google App Engine built-in credentials
  4. Google Cloud Shell built-in credentials
  5. Google Compute Engine built-in credentials

Google Cloud BigQuery (Beta)

Preview

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.

importcom.google.cloud.bigquery.BigQuery;
importcom.google.cloud.bigquery.BigQueryOptions;
importcom.google.cloud.bigquery.Field;
importcom.google.cloud.bigquery.FormatOptions;
importcom.google.cloud.bigquery.Job;
importcom.google.cloud.bigquery.Schema;
importcom.google.cloud.bigquery.StandardTableDefinition;
importcom.google.cloud.bigquery.Table;
importcom.google.cloud.bigquery.TableId;
importcom.google.cloud.bigquery.TableInfo;
BigQuerybigquery = BigQueryOptions.getDefaultInstance().getService();
TableIdtableId = TableId.of("dataset", "table");
Tabletable = bigquery.getTable(tableId);
if (table == null) {
System.out.println("Creating table " + tableId);
FieldintegerField = Field.of("fieldName", Field.Type.integer());
Schemaschema = Schema.of(integerField);
table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
}
System.out.println("Loading data into table " + tableId);
JobloadJob = 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");
}

Stackdriver Logging (Beta)

Follow the activation instructions to use the Stackdriver Logging API with your project.

Preview

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.

importcom.google.cloud.MonitoredResource;
importcom.google.cloud.Page;
importcom.google.cloud.logging.LogEntry;
importcom.google.cloud.logging.Logging;
importcom.google.cloud.logging.Logging.EntryListOption;
importcom.google.cloud.logging.LoggingOptions;
importcom.google.cloud.logging.Payload.StringPayload;
importjava.util.Collections;
importjava.util.Iterator;
LoggingOptionsoptions = LoggingOptions.getDefaultInstance();
try(Logginglogging = options.getService()) {
LogEntryfirstEntry = 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.

importcom.google.cloud.logging.LoggingHandler;
importjava.util.logging.Logger;
Loggerlogger = Logger.getLogger(AddLoggingHandler.class.getName());
LoggingHandler.addHandler(logger, newLoggingHandler());
logger.warning("test warning");

Google Cloud Datastore (Beta)

Follow the activation instructions to use the Google Cloud Datastore API with your project.

Preview

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.

importcom.google.cloud.datastore.Datastore;
importcom.google.cloud.datastore.DatastoreOptions;
importcom.google.cloud.datastore.DateTime;
importcom.google.cloud.datastore.Entity;
importcom.google.cloud.datastore.Key;
importcom.google.cloud.datastore.KeyFactory;
Datastoredatastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactorykeyFactory = datastore.newKeyFactory().setKind("keyKind");
Keykey = keyFactory.newKey("keyName");
Entityentity = 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.

importcom.google.cloud.datastore.Datastore;
importcom.google.cloud.datastore.DatastoreOptions;
importcom.google.cloud.datastore.DateTime;
importcom.google.cloud.datastore.Entity;
importcom.google.cloud.datastore.Key;
importcom.google.cloud.datastore.KeyFactory;
Datastoredatastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactorykeyFactory = datastore.newKeyFactory().setKind("keyKind");
Keykey = keyFactory.newKey("keyName");
Entityentity = 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);
}

Google Cloud Storage (Beta)

Follow the activation instructions to use the Google Cloud Storage API with your project.

Preview

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.

importstaticjava.nio.charset.StandardCharsets.UTF_8;
importcom.google.cloud.storage.Blob;
importcom.google.cloud.storage.BlobId;
importcom.google.cloud.storage.BlobInfo;
importcom.google.cloud.storage.Storage;
importcom.google.cloud.storage.StorageOptions;
Storagestorage = StorageOptions.getDefaultInstance().getService();
BlobIdblobId = BlobId.of("bucket", "blob_name");
BlobInfoblobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blobblob = 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.

importstaticjava.nio.charset.StandardCharsets.UTF_8;
importcom.google.cloud.storage.Blob;
importcom.google.cloud.storage.BlobId;
importcom.google.cloud.storage.Storage;
importcom.google.cloud.storage.StorageOptions;
importjava.nio.ByteBuffer;
importjava.nio.channels.WritableByteChannel;
Storagestorage = StorageOptions.getDefaultInstance().getService();
BlobIdblobId = BlobId.of("bucket", "blob_name");
Blobblob = storage.get(blobId);
if (blob != null) {
byte[] prevContent = blob.getContent();
System.out.println(newString(prevContent, UTF_8));
WritableByteChannelchannel = blob.writer();
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
channel.close();
}

Cloud Spanner (Beta)

Preview

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.

importcom.google.cloud.spanner.DatabaseClient;
importcom.google.cloud.spanner.DatabaseId;
importcom.google.cloud.spanner.ResultSet;
importcom.google.cloud.spanner.Spanner;
importcom.google.cloud.spanner.SpannerOptions;
importcom.google.cloud.spanner.Statement;
// Instantiates a clientSpannerOptionsoptions = SpannerOptions.newBuilder().build();
Spannerspanner = options.getService();
Stringinstance = "my-instance";
Stringdatabase = "my-database";
try {
// Creates a database clientDatabaseClientdbClient = spanner.getDatabaseClient(
DatabaseId.of(options.getProjectId(), instance, database));
// Queries the databaseResultSetresultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
// Prints the resultswhile (resultSet.next()) {
System.out.printf("%d\n", resultSet.getLong(0));
}
} finally {
// Closes the client which will free up the resources usedspanner.closeAsync().get();
}

Google Cloud Compute (Alpha)

Preview

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.

importcom.google.cloud.compute.Compute;
importcom.google.cloud.compute.ComputeOptions;
importcom.google.cloud.compute.Disk;
importcom.google.cloud.compute.DiskId;
importcom.google.cloud.compute.Snapshot;
Computecompute = ComputeOptions.getDefaultInstance().getService();
DiskIddiskId = DiskId.of("us-central1-a", "disk-name");
Diskdisk = compute.getDisk(diskId, Compute.DiskOption.fields());
if (disk != null) {
StringsnapshotName = "disk-name-snapshot";
Operationoperation = disk.createSnapshot(snapshotName);
operation = operation.waitFor();
if (operation.getErrors() == null) {
// use snapshotSnapshotsnapshot = compute.getSnapshot(snapshotName);
}
}

The second snippet shows how to create a virtual machine instance. Complete source code can be found at CreateInstance.java.

importcom.google.cloud.compute.AttachedDisk;
importcom.google.cloud.compute.Compute;
importcom.google.cloud.compute.ComputeOptions;
importcom.google.cloud.compute.ImageId;
importcom.google.cloud.compute.Instance;
importcom.google.cloud.compute.InstanceId;
importcom.google.cloud.compute.InstanceInfo;
importcom.google.cloud.compute.MachineTypeId;
importcom.google.cloud.compute.NetworkId;
Computecompute = ComputeOptions.getDefaultInstance().getService();
ImageIdimageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
NetworkIdnetworkId = NetworkId.of("default");
AttachedDiskattachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId));
NetworkInterfacenetworkInterface = NetworkInterface.of(networkId);
InstanceIdinstanceId = InstanceId.of("us-central1-a", "instance-name");
MachineTypeIdmachineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
Operationoperation =
compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
operation = operation.waitFor();
if (operation.getErrors() == null) {
// use instanceInstanceinstance = compute.getInstance(instanceId);
}

Google Cloud DNS (Alpha)

Follow the activation instructions to use the Google Cloud DNS API with your project.

Preview

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.

importcom.google.cloud.dns.Dns;
importcom.google.cloud.dns.DnsOptions;
importcom.google.cloud.dns.Zone;
importcom.google.cloud.dns.ZoneInfo;
Dnsdns = DnsOptions.getDefaultInstance().getService();
StringzoneName = "my-unique-zone";
StringdomainName = "someexampledomain.com.";
Stringdescription = "This is a google-cloud-dns sample zone.";
ZoneInfozoneInfo = ZoneInfo.of(zoneName, domainName, description);
Zonezone = dns.create(zoneInfo);

The second snippet shows how to create records inside a zone. The complete code can be found on CreateOrUpdateRecordSets.java.

importcom.google.cloud.dns.ChangeRequestInfo;
importcom.google.cloud.dns.Dns;
importcom.google.cloud.dns.DnsOptions;
importcom.google.cloud.dns.RecordSet;
importcom.google.cloud.dns.Zone;
importjava.util.Iterator;
importjava.util.concurrent.TimeUnit;
Dnsdns = DnsOptions.getDefaultInstance().getService();
StringzoneName = "my-unique-zone";
Zonezone = dns.getZone(zoneName);
Stringip = "12.13.14.15";
RecordSettoCreate = RecordSet.newBuilder("www.someexampledomain.com.", RecordSet.Type.A)
.setTtl(24, TimeUnit.HOURS)
.addRecord(ip)
.build();
ChangeRequestInfo.BuilderchangeBuilder = 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()) {
RecordSetcurrent = recordSetIterator.next();
if (toCreate.getName().equals(current.getName()) &&
toCreate.getType().equals(current.getType())) {
changeBuilder.delete(current);
}
}
ChangeRequestInfochangeRequest = changeBuilder.build();
zone.applyChangeRequest(changeRequest);

Google Cloud Pub/Sub (Alpha)

Preview

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.

importcom.google.cloud.pubsub.Message;
importcom.google.cloud.pubsub.PubSub;
importcom.google.cloud.pubsub.PubSub.MessageConsumer;
importcom.google.cloud.pubsub.PubSub.MessageProcessor;
importcom.google.cloud.pubsub.PubSubOptions;
importcom.google.cloud.pubsub.Subscription;
importcom.google.cloud.pubsub.SubscriptionInfo;
try (PubSubpubsub = PubSubOptions.getDefaultInstance().getService()) {
Subscriptionsubscription =
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
MessageProcessorcallback = newMessageProcessor() {
@Overridepublicvoidprocess(Messagemessage) throwsException {
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
}
};
// Create a message consumer and pull messages (for 60 seconds)try (MessageConsumerconsumer = subscription.pullAsync(callback)) {
Thread.sleep(60_000);
}
}

Google Cloud Resource Manager (Alpha)

Preview

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.

importcom.google.cloud.resourcemanager.Project;
importcom.google.cloud.resourcemanager.ResourceManager;
importcom.google.cloud.resourcemanager.ResourceManagerOptions;
importjava.util.Iterator;
ResourceManagerresourceManager = ResourceManagerOptions.getDefaultInstance().getService();
Projectproject = resourceManager.get("some-project-id"); // Use an existing project's IDif (project != null) {
ProjectnewProject = 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());
}

Google Translate (Alpha)

Preview

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.

importcom.google.cloud.translate.Detection;
importcom.google.cloud.translate.Translate;
importcom.google.cloud.translate.Translate.TranslateOption;
importcom.google.cloud.translate.TranslateOptions;
importcom.google.cloud.translate.Translation;
Translatetranslate = TranslateOptions.getDefaultInstance().getService();
Detectiondetection = translate.detect("Hola");
StringdetectedLanguage = detection.getLanguage();
Translationtranslation = translate.translate(
"World",
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage(detectedLanguage));
System.out.printf("Hola %s%n", translation.getTranslatedText());

Troubleshooting

To get help, follow the instructions in the shared Troubleshooting document.

Java Versions

Java 7 or above is required for using this client.

Testing

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.

Versioning

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.

Contributing

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.

License

Apache 2.0 - See LICENSE for more information.

About

Google Cloud Client Library for Java

Resources

Code of conduct

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages