Examples for gcloud-java (Java idiomatic client for Google Cloud Platform services).
- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
- [Examples] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/examples/package-summary.html)
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-examples</artifactId>
<version>0.1.1</version>
</dependency>If you are using Gradle, add this to your dependencies
compile 'com.google.gcloud:gcloud-java-examples:0.1.1'If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.gcloud" % "gcloud-java-examples" % "0.1.1"To run examples from your command line:
-
Login 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 compilein command line from your base project directory) -
Run an example using Maven from command line.
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_file to the my_bucket bucket
(replace my_csv_file and my_bucket with actual file and bucket names) using the GCS
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:
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample" -Dexec.args="create dataset new_dataset_id"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample" -Dexec.args="create table new_dataset_id new_table_id field_name:string"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample" -Dexec.args="list tables new_dataset_id"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample" -Dexec.args="load new_dataset_id new_table_id CSV gs://my_bucket/my_csv_file"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample" -Dexec.args="query 'select * from new_dataset_id.new_table_id'"
Here's an example run of DatastoreExample.
Note that you have to enable the Google Cloud Datastore API on the Google Developers Console before running the following commands.
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.DatastoreExample" -Dexec.args="my_name add my\ comment"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.DatastoreExample" -Dexec.args="my_name display"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.DatastoreExample" -Dexec.args="my_name delete"
Here's an example run of ResourceManagerExample.
Be sure to change the placeholder project ID "my-project-id" with your own globally unique project ID.
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="create my-project-id"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="list"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="get my-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 API is enabled and that you have a bucket. Also ensure that you have a test file (test.txt is chosen here) to upload to Cloud Storage stored locally on your machine.
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="upload /path/to/test.txt <bucket_name>"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="list <bucket_name>"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="download <bucket_name> test.txt"
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="delete <bucket_name> test.txt"
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] (http://semver.org/).
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.