| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Java idiomatic client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services at a GA quality level:
This client supports the following Google Cloud Platform services at a Beta quality level:
This client supports the following Google Cloud Platform services at an Alpha quality level:
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.19.0-alpha</version>
</dependency>If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:google-cloud:0.19.0-alpha'If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud" % "0.19.0-alpha"For running on Google App Engine, see more instructions here.
Most google-cloud libraries require a project ID. There are multiple ways to specify this project ID.
Datastore datastore = DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService();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:
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:
Storage storage = StorageOptions.getDefaultInstance().getService();export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.jsonStorage storage = StorageOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/my/key.json")))
.build()
.getService();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):
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. 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");
}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 is a code snippet showing a simple usage example of LanguageServiceClient. The example assumes that either default application credentials or a valid api key are available. (See Authentication section for more information)
try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) {
Document document = Document.newBuilder().build();
AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document);
}Here is a code snippet showing a simple usage example of ImageAnnotatorClient. The example assumes that either default application credentials or a valid api key are available. (See Authentication section for more information)
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
List<AnnotateImageRequest> requests = new ArrayList<>();
BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests);
}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 of ErrorGroupServiceClient. Note that you must supply credentials and a project ID if running this snippet elsewhere.
try (ErrorGroupServiceClient errorGroupServiceClient = ErrorGroupServiceClient.create()) {
GroupName groupName = GroupName.create("[PROJECT]", "[GROUP]");
ErrorGroup response = errorGroupServiceClient.getGroup(groupName);
}Here is a code snippet showing a simple usage example of MetricServiceClient. Note that you must supply credentials and a project ID if running this snippet elsewhere.
try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
MonitoredResourceDescriptorName name =
MonitoredResourceDescriptorName.create("[PROJECT]", "[MONITORED_RESOURCE_DESCRIPTOR]");
MonitoredResourceDescriptor response = metricServiceClient.getMonitoredResourceDescriptor(name);
}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 CreateTopicAndPublishMessages.java.
import com.google.api.gax.core.ApiFuture;
import com.google.cloud.pubsub.spi.v1.Publisher;
import com.google.cloud.pubsub.spi.v1.TopicAdminClient;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
TopicName topic = TopicName.create("test-project", "test-topic");
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
topicAdminClient.createTopic(topic);
}
Publisher publisher = null;
try {
publisher = Publisher.newBuilder(topic).build();
ByteString data = ByteString.copyFromUtf8("my message");
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
ApiFuture<String> messageId = publisher.publish(pubsubMessage);
System.out.println("published with message ID: " + messageId.get());
} finally {
if (publisher != null) {
publisher.shutdown();
}
}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());Here is a code snippet showing a simple usage example of SpeechClient. The example assumes that either default application credentials or a valid api key are available. (See Authentication section for more information) Note that you must provide a uri to a FLAC audio file to run this.
try (SpeechClient speechClient = SpeechClient.create()) {
RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC;
int sampleRateHertz = 44100;
String languageCode = "en-US";
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(encoding)
.setSampleRateHertz(sampleRateHertz)
.setLanguageCode(languageCode)
.build();
String uri = "gs://bucket_name/file_name.flac";
RecognitionAudio audio = RecognitionAudio.newBuilder()
.setUri(uri)
.build();
RecognizeResponse response = speechClient.recognize(config, audio);
}Here is a code snippet showing a simple usage example of TraceServiceClient. The example assumes that either default application credentials or a valid api key are available. Note that you must supply credentials and a project ID if running this snippet elsewhere.
try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
String projectId = "";
Traces traces = Traces.newBuilder().build();
traceServiceClient.patchTraces(projectId, traces);
}Here is a code snippet showing a simple usage example of TraceServiceClient. The example assumes that either default application credentials or a valid api key are available. Note that you must supply credentials and a project ID if running this snippet elsewhere.
try (VideoIntelligenceServiceClient videoIntelligenceServiceClient =
VideoIntelligenceServiceClient.create()) {
String inputUri = "";
List<Feature> features = new ArrayList<>();
VideoContext videoContext = VideoContext.newBuilder().build();
String outputUri = "";
String locationId = "";
AnnotateVideoResponse response =
videoIntelligenceServiceClient.annotateVideoAsync(
inputUri, features, videoContext, outputUri, locationId).get();
}To get help, follow the instructions in the shared Troubleshooting document.
Java 7 or above is required for using this client.
This client is supported on Mac OS X, Windows and Linux (excluding Android and Alpine). Google Cloud Platform environments currently supported include GCE, GKE and GAE Flex. GAE Standard is not currently supported.
Spring Boot users : Native Tomcat is not currently supported. Please use embedded Jetty to get your application working with 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, but with some additional qualifications:
Components marked with @BetaApi are considered to be "0.x" features inside a "1.x" library. This means they can change between minor and patch releases in incompatible ways. These features should not be used by any library "B" that itself has consumers, unless the components of library B that use @BetaApi features are also marked with @BetaApi. Features marked as @BetaApi are on a path to eventually become "1.x" features with the marker removed.
Special exception for google-cloud-java: google-cloud-java is allowed to depend on @BetaApi features in gax-java without declaring the consuming code @BetaApi, because gax-java and google-cloud-java move in step with each other. For this reason, gax-java should not be used independently of google-cloud-java.
Components marked with @InternalApi are technically public, but are only public for technical reasons, because of the limitations of Java's access modifiers. For the purposes of semver, they should be considered private.
Please note it is currently under active development. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.
GA: Libraries defined at a GA quality level are expected to be stable and all updates in the libraries are guaranteed to be backwards-compatible. Any backwards-incompatible changes will lead to the major version increment (1.x.y -> 2.0.0).
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.
| Back | FazBrowse Home | New Git URL |