Examples for gcloud-java (Java idiomatic client for Google Cloud Platform services).
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java-examples</artifactId>
<version>0.2.6</version>
</dependency>If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:gcloud-java-examples:0.2.6'If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "gcloud-java-examples" % "0.2.6"To run examples from your command line:
-
Log in using gcloud SDK (
gcloud auth loginin command line) -
Set your current project using
gcloud config set project PROJECT_ID. This step is not necessary forResourceManagerExample. -
Compile using Maven:
mvn install -DskipTestsin command line from your base project directory thencd gcloud-java-examplesand finallymvn package appassembler:assemble -DskipTests. -
Run an example from the command line using the Maven-generated scripts.
-
Here's an example run of
BigQueryExample.Before running the example, go to the Google Developers Console to ensure that BigQuery API is enabled. You can upload a CSV file
my_csv_fileto themy_bucketbucket (replacemy_csv_fileandmy_bucketwith actual file and bucket names) using the Google Cloud Storage web browser. The CSV file will be used to load data into a BigQuery table and should look something like:value1 value2 value3Then you are ready to run the following example:
target/appassembler/bin/BigQueryExample create dataset new_dataset_id target/appassembler/bin/BigQueryExample create table new_dataset_id new_table_id field_name:string target/appassembler/bin/BigQueryExample list tables new_dataset_id target/appassembler/bin/BigQueryExample load new_dataset_id new_table_id CSV gs://my_bucket/my_csv_file target/appassembler/bin/BigQueryExample query 'select * from new_dataset_id.new_table_id' -
Here's an example run of
ComputeExample.Before running the example, go to the Google Developers Console to ensure that Compute API is enabled.
target/appassembler/bin/ComputeExample create image-disk us-central1-a test-disk debian-cloud debian-8-jessie-v20160329 target/appassembler/bin/ComputeExample create instance us-central1-a test-instance n1-standard-1 test-disk default target/appassembler/bin/ComputeExample add-access-config us-central1-a test-instance nic0 NAT target/appassembler/bin/ComputeExample delete instance us-central1-a test-instance target/appassembler/bin/ComputeExample delete disk us-central1-a test-disk -
Here's an example run of
DatastoreExample.Be sure to change the placeholder project ID "your-project-id" with your own project ID. Also note that you have to enable the Google Cloud Datastore API on the Google Developers Console before running the following commands.
target/appassembler/bin/DatastoreExample your-project-id my_name add my\ comment target/appassembler/bin/DatastoreExample your-project-id my_name display target/appassembler/bin/DatastoreExample your-project-id my_name delete target/appassembler/bin/DatastoreExample your-project-id my_name set myname@mydomain.com 1234 -
Here's an example run of
DnsExample.Note that you have to enable the Google Cloud DNS API on the Google Developers Console before running the following commands. You will need to replace the domain name
elaborateexample.comwith your own domain name with [verified ownership] (https://www.google.com/webmasters/verification/home). Also, note that the example creates and deletes record sets of type A only. Operations with other record types are not implemented in the example.target/appassembler/bin/DnsExample create some-sample-zone elaborateexample.com. description target/appassembler/bin/DnsExample list target/appassembler/bin/DnsExample list some-sample-zone records target/appassembler/bin/DnsExample add-record some-sample-zone www.elaborateexample.com. 12.13.14.15 69 target/appassembler/bin/DnsExample get some-sample-zone target/appassembler/bin/DnsExample delete-record some-sample-zone www.elaborateexample.com. 12.13.14.15 69 target/appassembler/bin/DnsExample list some-sample-zone changes ascending target/appassembler/bin/DnsExample delete some-sample-zone -
Here's an example run of
LoggingExample.Before running the example, go to the Google Developers Console to ensure that Logging API is enabled.
target/appassembler/bin/LoggingExample create metric test-metric severity>=ERROR target/appassembler/bin/LoggingExample list metrics target/appassembler/bin/LoggingExample create sink test-sink bucket test-bucket severity>=ERROR target/appassembler/bin/LoggingExample list sinks target/appassembler/bin/LoggingExample write test-log-name ERROR test-message test-key test-value target/appassembler/bin/LoggingExample list entries -
Here's an example run of
ParallelCountBytes.Before running the example, go to the Google Developers Console to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it.
target/appassembler/bin/ParallelCountBytes gs://mybucket/myfile.txt -
Here's an example run of
PubSubExample.Before running the example, go to the Google Developers Console to ensure that "Google Cloud Pub/Sub" is enabled.
target/appassembler/bin/PubSubExample create topic test-topic target/appassembler/bin/PubSubExample create subscription test-topic test-subscription target/appassembler/bin/PubSubExample publish test-topic message1 message2 target/appassembler/bin/PubSubExample pull sync test-subscription 2 -
Here's an example run of
ResourceManagerExample.Be sure to change the placeholder project ID "your-project-id" with your own globally unique project ID.
target/appassembler/bin/ResourceManagerExample create your-project-id target/appassembler/bin/ResourceManagerExample list target/appassembler/bin/ResourceManagerExample get your-project-id -
Here's an example run of
StorageExample.Before running the example, go to the Google Developers Console to ensure that "Google Cloud Storage" and "Google Cloud Storage JSON API" are enabled and that you have a bucket. Also ensure that you have a test file (
test.txtis chosen here) to upload to Cloud Storage stored locally on your machine.target/appassembler/bin/StorageExample upload /path/to/test.txt <bucket_name> target/appassembler/bin/StorageExample list <bucket_name> target/appassembler/bin/StorageExample download <bucket_name> test.txt target/appassembler/bin/StorageExample delete <bucket_name> test.txt -
Here's an example run of
Stat, illustrating the use of gcloud-java-nio.Before running the example, go to the Google Developers Console to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it.
Run the sample with (from the gcloud-java-examples folder):
target/appassembler/bin/Stat --checkOr, if you have a file in
gs://mybucket/myfile.txt, you can run:target/appassembler/bin/Stat gs://mybucket/myfile.txtThe sample doesn't have anything special about Google Cloud Storage in it, it just opens files via the NIO API. It lists gcloud-java-nio as a dependency, and that enables it to interpret
gs://paths.
To get help, follow the gcloud-java links in the gcloud-* shared Troubleshooting document.
Java 7 or above is required for using this client.
This library follows Semantic Versioning.
It is currently in major version zero (0.y.z), which means that anything
may change at any time and the public API should not be considered
stable.
Contributions to this library are always welcome and highly encouraged.
See gcloud-java's CONTRIBUTING documentation and the gcloud-* 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.