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