1*55e87721SMatt GilbrideGoogle Cloud Java Client Examples 2*55e87721SMatt Gilbride================================= 3*55e87721SMatt Gilbride 4*55e87721SMatt GilbrideExamples for google-cloud (Java idiomatic client for [Google Cloud Platform][cloud-platform] services). 5*55e87721SMatt Gilbride 6*55e87721SMatt Gilbride**Note about migration**: In September 2022, this folder was moved from [main_202208 branch]( 7*55e87721SMatt Gilbridehttps://github.com/googleapis/google-cloud-java/tree/main_202208/google-cloud-examples) 8*55e87721SMatt Gilbrideto this branch. The `pom.xml` file has been removed as we no longer build the directory. 9*55e87721SMatt GilbrideThe old branch has pom.xml to build the project. 10*55e87721SMatt Gilbride 11*55e87721SMatt Gilbride[](https://circleci.com/gh/googleapis/google-cloud-java/tree/master) 12*55e87721SMatt Gilbride[](https://coveralls.io/r/googleapis/google-cloud-java?branch=master) 13*55e87721SMatt Gilbride[]( https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-examples.svg) 14*55e87721SMatt Gilbride[](https://www.codacy.com/app/mziccard/google-cloud-java) 15*55e87721SMatt Gilbride[](https://www.versioneye.com/user/projects/58fe4c8d6ac171426c414772) 16*55e87721SMatt Gilbride 17*55e87721SMatt Gilbride- [Examples](https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/examples/package-summary.html) 18*55e87721SMatt Gilbride 19*55e87721SMatt GilbrideQuickstart 20*55e87721SMatt Gilbride---------- 21*55e87721SMatt Gilbride 22*55e87721SMatt Gilbride[//]: # ({x-version-update-start:google-cloud-examples:released}) 23*55e87721SMatt GilbrideIf you are using Maven, add this to your pom.xml file 24*55e87721SMatt Gilbride```xml 25*55e87721SMatt Gilbride<dependency> 26*55e87721SMatt Gilbride <groupId>com.google.cloud</groupId> 27*55e87721SMatt Gilbride <artifactId>google-cloud-examples</artifactId> 28*55e87721SMatt Gilbride <version>0.120.0-alpha</version> 29*55e87721SMatt Gilbride</dependency> 30*55e87721SMatt Gilbride``` 31*55e87721SMatt GilbrideIf you are using Gradle, add this to your dependencies 32*55e87721SMatt Gilbride```Groovy 33*55e87721SMatt Gilbridecompile 'com.google.cloud:google-cloud-examples:0.120.0-alpha' 34*55e87721SMatt Gilbride``` 35*55e87721SMatt GilbrideIf you are using SBT, add this to your dependencies 36*55e87721SMatt Gilbride```Scala 37*55e87721SMatt GilbridelibraryDependencies += "com.google.cloud" % "google-cloud-examples" % "0.120.0-alpha" 38*55e87721SMatt Gilbride``` 39*55e87721SMatt Gilbride[//]: # ({x-version-update-end}) 40*55e87721SMatt Gilbride 41*55e87721SMatt GilbrideTo run examples from your command line: 42*55e87721SMatt Gilbride 43*55e87721SMatt Gilbride1. Log in using gcloud SDK (`gcloud auth login` in command line) 44*55e87721SMatt Gilbride 45*55e87721SMatt Gilbride2. Set your current project using `gcloud config set project PROJECT_ID`. This step is not necessary for `ResourceManagerExample`. 46*55e87721SMatt Gilbride 47*55e87721SMatt Gilbride3. Compile using Maven: `mvn install -DskipTests` in command line from your base project directory 48*55e87721SMatt Gilbride then `cd google-cloud-examples` and finally `mvn package appassembler:assemble -DskipTests`. 49*55e87721SMatt Gilbride 50*55e87721SMatt Gilbride4. Run an example from the command line using the Maven-generated scripts. 51*55e87721SMatt Gilbride 52*55e87721SMatt Gilbride * Here's an example run of `ComputeExample`. 53*55e87721SMatt Gilbride 54*55e87721SMatt Gilbride Before running the example, go to the [Google Developers Console][developers-console] to ensure 55*55e87721SMatt Gilbride that Compute API is enabled. 56*55e87721SMatt Gilbride ``` 57*55e87721SMatt Gilbride target/appassembler/bin/ComputeExample create image-disk us-central1-a test-disk debian-cloud debian-8-jessie-v20160329 58*55e87721SMatt Gilbride target/appassembler/bin/ComputeExample create instance us-central1-a test-instance n1-standard-1 test-disk default 59*55e87721SMatt Gilbride target/appassembler/bin/ComputeExample add-access-config us-central1-a test-instance nic0 NAT 60*55e87721SMatt Gilbride target/appassembler/bin/ComputeExample delete instance us-central1-a test-instance 61*55e87721SMatt Gilbride target/appassembler/bin/ComputeExample delete disk us-central1-a test-disk 62*55e87721SMatt Gilbride ``` 63*55e87721SMatt Gilbride 64*55e87721SMatt Gilbride * Here's an example run of `DatastoreExample`. 65*55e87721SMatt Gilbride 66*55e87721SMatt Gilbride 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][developers-console] before running the following commands. 67*55e87721SMatt Gilbride ``` 68*55e87721SMatt Gilbride target/appassembler/bin/DatastoreExample your-project-id my_name add my\ comment 69*55e87721SMatt Gilbride target/appassembler/bin/DatastoreExample your-project-id my_name display 70*55e87721SMatt Gilbride target/appassembler/bin/DatastoreExample your-project-id my_name delete 71*55e87721SMatt Gilbride target/appassembler/bin/DatastoreExample your-project-id my_name set [email protected] 1234 72*55e87721SMatt Gilbride ``` 73*55e87721SMatt Gilbride 74*55e87721SMatt Gilbride * Here's an example run of `DnsExample`. 75*55e87721SMatt Gilbride 76*55e87721SMatt Gilbride Note that you have to enable the Google Cloud DNS API on the [Google Developers Console][developers-console] before running the following commands. 77*55e87721SMatt Gilbride You will need to replace the domain name `elaborateexample.com` with your own domain name with [verified ownership](https://www.google.com/webmasters/verification/home). 78*55e87721SMatt Gilbride 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. 79*55e87721SMatt Gilbride ``` 80*55e87721SMatt Gilbride target/appassembler/bin/DnsExample create some-sample-zone elaborateexample.com. description 81*55e87721SMatt Gilbride target/appassembler/bin/DnsExample list 82*55e87721SMatt Gilbride target/appassembler/bin/DnsExample list some-sample-zone records 83*55e87721SMatt Gilbride target/appassembler/bin/DnsExample add-record some-sample-zone www.elaborateexample.com. 12.13.14.15 69 84*55e87721SMatt Gilbride target/appassembler/bin/DnsExample get some-sample-zone 85*55e87721SMatt Gilbride target/appassembler/bin/DnsExample delete-record some-sample-zone www.elaborateexample.com. 12.13.14.15 69 86*55e87721SMatt Gilbride target/appassembler/bin/DnsExample list some-sample-zone changes ascending 87*55e87721SMatt Gilbride target/appassembler/bin/DnsExample delete some-sample-zone 88*55e87721SMatt Gilbride ``` 89*55e87721SMatt Gilbride 90*55e87721SMatt Gilbride * Here's an example run of `LoggingExample`. 91*55e87721SMatt Gilbride 92*55e87721SMatt Gilbride Before running the example, go to the [Google Developers Console][developers-console] to ensure 93*55e87721SMatt Gilbride that Logging API is enabled. 94*55e87721SMatt Gilbride ``` 95*55e87721SMatt Gilbride target/appassembler/bin/LoggingExample create metric test-metric severity>=ERROR 96*55e87721SMatt Gilbride target/appassembler/bin/LoggingExample list metrics 97*55e87721SMatt Gilbride target/appassembler/bin/LoggingExample create sink test-sink bucket test-bucket severity>=ERROR 98*55e87721SMatt Gilbride target/appassembler/bin/LoggingExample list sinks 99*55e87721SMatt Gilbride target/appassembler/bin/LoggingExample write test-log-name ERROR test-message test-key test-value 100*55e87721SMatt Gilbride target/appassembler/bin/LoggingExample list entries 101*55e87721SMatt Gilbride ``` 102*55e87721SMatt Gilbride 103*55e87721SMatt Gilbride * Here's an example run of `ParallelCountBytes`. 104*55e87721SMatt Gilbride 105*55e87721SMatt Gilbride Before running the example, go to the [Google Developers Console][developers-console] to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it. 106*55e87721SMatt Gilbride ``` 107*55e87721SMatt Gilbride target/appassembler/bin/ParallelCountBytes gs://mybucket/myfile.txt 108*55e87721SMatt Gilbride ``` 109*55e87721SMatt Gilbride 110*55e87721SMatt Gilbride * Here's an example run of `ResourceManagerExample`. 111*55e87721SMatt Gilbride 112*55e87721SMatt Gilbride Be sure to change the placeholder project ID "your-project-id" with your own globally unique project ID. 113*55e87721SMatt Gilbride ``` 114*55e87721SMatt Gilbride target/appassembler/bin/ResourceManagerExample create your-project-id 115*55e87721SMatt Gilbride target/appassembler/bin/ResourceManagerExample list 116*55e87721SMatt Gilbride target/appassembler/bin/ResourceManagerExample get your-project-id 117*55e87721SMatt Gilbride ``` 118*55e87721SMatt Gilbride 119*55e87721SMatt Gilbride * Here's an example run of `Stat`, illustrating the use of google-cloud-nio. 120*55e87721SMatt Gilbride 121*55e87721SMatt Gilbride Before running the example, go to the [Google Developers Console][developers-console] to ensure that Google Cloud Storage API is enabled and that you have a bucket with a file in it. 122*55e87721SMatt Gilbride 123*55e87721SMatt Gilbride Run the sample with (from the google-cloud-examples folder): 124*55e87721SMatt Gilbride ``` 125*55e87721SMatt Gilbride target/appassembler/bin/Stat --check 126*55e87721SMatt Gilbride 127*55e87721SMatt Gilbride ``` 128*55e87721SMatt Gilbride Or, if you have a file in `gs://mybucket/myfile.txt`, you can run: 129*55e87721SMatt Gilbride ``` 130*55e87721SMatt Gilbride target/appassembler/bin/Stat gs://mybucket/myfile.txt 131*55e87721SMatt Gilbride ``` 132*55e87721SMatt Gilbride 133*55e87721SMatt Gilbride The sample doesn't have anything special about Google Cloud Storage in it, it just opens files 134*55e87721SMatt Gilbride via the NIO API. It lists google-cloud-nio as a dependency, and that enables it to interpret 135*55e87721SMatt Gilbride `gs://` paths. 136*55e87721SMatt Gilbride 137*55e87721SMatt Gilbride * Here's an example run of `TranslateExample`. 138*55e87721SMatt Gilbride 139*55e87721SMatt Gilbride Before running the example, go to the [Google Developers Console][developers-console] to ensure that "Google Translation API" is enabled. 140*55e87721SMatt Gilbride ``` 141*55e87721SMatt Gilbride target/appassembler/bin/TranslateExample languages 142*55e87721SMatt Gilbride target/appassembler/bin/TranslateExample detect Hello,\ World! 143*55e87721SMatt Gilbride target/appassembler/bin/TranslateExample translate ¡Hola\ Mundo! 144*55e87721SMatt Gilbride target/appassembler/bin/TranslateExample es translate Hello,\ World! 145*55e87721SMatt Gilbride ``` 146*55e87721SMatt Gilbride 147*55e87721SMatt GilbrideTroubleshooting 148*55e87721SMatt Gilbride--------------- 149*55e87721SMatt Gilbride 150*55e87721SMatt GilbrideTo get help, follow the instructions in the [shared Troubleshooting document](https://github.com/googleapis/google-cloud-common/blob/master/troubleshooting/readme.md#troubleshooting). 151*55e87721SMatt Gilbride 152*55e87721SMatt GilbrideJava Versions 153*55e87721SMatt Gilbride------------- 154*55e87721SMatt Gilbride 155*55e87721SMatt GilbrideJava 7 or above is required for using this client. 156*55e87721SMatt Gilbride 157*55e87721SMatt GilbrideVersioning 158*55e87721SMatt Gilbride---------- 159*55e87721SMatt Gilbride 160*55e87721SMatt GilbrideThis library follows [Semantic Versioning](http://semver.org/). 161*55e87721SMatt Gilbride 162*55e87721SMatt GilbrideIt is currently in major version zero (``0.y.z``), which means that anything 163*55e87721SMatt Gilbridemay change at any time and the public API should not be considered 164*55e87721SMatt Gilbridestable. 165*55e87721SMatt Gilbride 166*55e87721SMatt GilbrideContributing 167*55e87721SMatt Gilbride------------ 168*55e87721SMatt Gilbride 169*55e87721SMatt GilbrideContributions to this library are always welcome and highly encouraged. 170*55e87721SMatt Gilbride 171*55e87721SMatt GilbrideSee `google-cloud`'s [CONTRIBUTING] documentation and the [shared documentation](https://github.com/googleapis/google-cloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started. 172*55e87721SMatt Gilbride 173*55e87721SMatt GilbridePlease 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][code-of-conduct] for more information. 174*55e87721SMatt Gilbride 175*55e87721SMatt GilbrideLicense 176*55e87721SMatt Gilbride------- 177*55e87721SMatt Gilbride 178*55e87721SMatt GilbrideApache 2.0 - See [LICENSE] for more information. 179*55e87721SMatt Gilbride 180*55e87721SMatt Gilbride 181*55e87721SMatt Gilbride[CONTRIBUTING]:https://github.com/googleapis/google-cloud-java/blob/master/CONTRIBUTING.md 182*55e87721SMatt Gilbride[code-of-conduct]:https://github.com/googleapis/google-cloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct 183*55e87721SMatt Gilbride[LICENSE]: https://github.com/googleapis/google-cloud-java/blob/master/LICENSE 184*55e87721SMatt Gilbride[cloud-platform]: https://cloud.google.com/ 185*55e87721SMatt Gilbride[developers-console]:https://console.developers.google.com/ 186