|
| 1 | +# Cloud Bigtable Hello World with Tracing |
| 2 | + |
| 3 | +This application builds on top of the [basic Cloud Bigtable hello world example](../hello-world). |
| 4 | +This project adds automatic exports of [tracing to stackdriver](https://cloud.google.com/trace/) and |
| 5 | +[Z-Pages](https://github.com/census-instrumentation/opencensus-java/tree/master/contrib/zpages) that |
| 6 | +show information about traces on the local machine. |
| 7 | + |
| 8 | +See the [documentation for this |
| 9 | +sample](https://cloud.google.com/bigtable/docs/samples-java-hello) for a brief |
| 10 | +explanation of the hello world code. |
| 11 | + |
| 12 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 13 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 14 | + |
| 15 | +**Table of Contents** |
| 16 | + |
| 17 | +- [Downloading the sample](#downloading-the-sample) |
| 18 | +- [Costs](#costs) |
| 19 | +- [Before you begin](#before-you-begin) |
| 20 | + - [Installing Maven](#installing-maven) |
| 21 | + - [Creating a Project in the Google Cloud Platform Console](#creating-a-project-in-the-google-cloud-platform-console) |
| 22 | + - [Enabling billing for your project.](#enabling-billing-for-your-project) |
| 23 | + - [Install the Google Cloud SDK.](#install-the-google-cloud-sdk) |
| 24 | + - [Setting Google Application Default Credentials](#setting-google-application-default-credentials) |
| 25 | +- [Provisioning an instance](#provisioning-an-instance) |
| 26 | +- [Running the application](#running-the-application) |
| 27 | +- [Cleaning up](#cleaning-up) |
| 28 | + |
| 29 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 30 | + |
| 31 | + |
| 32 | +## Downloading the sample |
| 33 | + |
| 34 | +Download the sample app and navigate into the app directory: |
| 35 | + |
| 36 | +1. Clone the [Cloud Bigtable examples repository][github-repo], to your local |
| 37 | + machine: |
| 38 | + |
| 39 | + git clone https://github.com/GoogleCloudPlatform/cloud-bigtable-examples.git |
| 40 | + |
| 41 | + Alternatively, you can [download the sample][github-zip] as a zip file and |
| 42 | + extract it. |
| 43 | + |
| 44 | +2. Change to the Hello World code sample directory. |
| 45 | + |
| 46 | + cd cloud-bigtable-examples/java/tracing-hello-world |
| 47 | + |
| 48 | +[github-repo]: https://github.com/GoogleCloudPlatform/cloud-bigtable-examples |
| 49 | +[github-zip]: https://github.com/GoogleCloudPlatform/cloud-bigtable-examples/archive/master.zip |
| 50 | + |
| 51 | + |
| 52 | +## Costs |
| 53 | + |
| 54 | +This sample uses billable components of Cloud Platform, including: |
| 55 | + |
| 56 | ++ Google Cloud Bigtable |
| 57 | + |
| 58 | +Use the [Pricing Calculator][bigtable-pricing] to generate a cost estimate |
| 59 | +based on your projected usage. New Cloud Platform users might be eligible for |
| 60 | +a [free trial][free-trial]. |
| 61 | + |
| 62 | +[bigtable-pricing]: https://cloud.google.com/products/calculator/#id=1eb47664-13a2-4be1-9d16-6722902a7572 |
| 63 | +[free-trial]: https://cloud.google.com/free-trial |
| 64 | + |
| 65 | + |
| 66 | +## Before you begin |
| 67 | + |
| 68 | +This sample assumes you have [Java 8][java8] JDK installed. |
| 69 | + |
| 70 | +[java8]: http://www.oracle.com/technetwork/java/javase/downloads/ |
| 71 | + |
| 72 | +### Installing Maven |
| 73 | + |
| 74 | +These samples use the [Apache Maven][maven] build system. Before getting |
| 75 | +started, be sure to [download][maven-download] and [install][maven-install] it. |
| 76 | +When you use Maven as described here, it will automatically download the needed |
| 77 | +client libraries. |
| 78 | + |
| 79 | +[maven]: https://maven.apache.org |
| 80 | +[maven-download]: https://maven.apache.org/download.cgi |
| 81 | +[maven-install]: https://maven.apache.org/install.html |
| 82 | + |
| 83 | +### Creating a Project in the Google Cloud Platform Console |
| 84 | + |
| 85 | +If you haven't already created a project, create one now. Projects enable you to |
| 86 | +manage all Google Cloud Platform resources for your app, including deployment, |
| 87 | +access control, billing, and services. |
| 88 | + |
| 89 | +1. Open the [Cloud Platform Console][cloud-console]. |
| 90 | +1. In the drop-down menu at the top, select **Create a project**. |
| 91 | +1. Give your project a name. |
| 92 | +1. Make a note of the project ID, which might be different from the project |
| 93 | + name. The project ID is used in commands and in configurations. |
| 94 | + |
| 95 | +[cloud-console]: https://console.cloud.google.com/ |
| 96 | + |
| 97 | +### Enabling billing for your project. |
| 98 | + |
| 99 | +If you haven't already enabled billing for your project, [enable |
| 100 | +billing][enable-billing] now. Enabling billing allows is required to use Cloud Bigtable |
| 101 | +and to create VM instances. |
| 102 | + |
| 103 | +[enable-billing]: https://console.cloud.google.com/project/_/settings |
| 104 | + |
| 105 | +### Install the Google Cloud SDK. |
| 106 | + |
| 107 | +If you haven't already installed the Google Cloud SDK, [install the Google |
| 108 | +Cloud SDK][cloud-sdk] now. The SDK contains tools and libraries that enable you |
| 109 | +to create and manage resources on Google Cloud Platform. |
| 110 | + |
| 111 | +[cloud-sdk]: https://cloud.google.com/sdk/ |
| 112 | + |
| 113 | +### Setting Google Application Default Credentials |
| 114 | + |
| 115 | +Set your [Google Application Default |
| 116 | +Credentials][application-default-credentials] by [initializing the Google Cloud |
| 117 | +SDK][cloud-sdk-init] with the command: |
| 118 | + |
| 119 | + gcloud init |
| 120 | + |
| 121 | +Generate a credentials file by running the [application-default login](https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login) command: |
| 122 | + |
| 123 | + gcloud auth application-default login |
| 124 | + |
| 125 | +[cloud-sdk-init]: https://cloud.google.com/sdk/docs/initializing |
| 126 | +[application-default-credentials]: https://developers.google.com/identity/protocols/application-default-credentials |
| 127 | + |
| 128 | + |
| 129 | +## Provisioning an instance |
| 130 | + |
| 131 | +Follow the instructions in the [user |
| 132 | +documentation](https://cloud.google.com/bigtable/docs/creating-instance) to |
| 133 | +create a Google Cloud Platform project and Cloud Bigtable instance if necessary. |
| 134 | +You'll need to reference your project id and instance id to run the |
| 135 | +application. |
| 136 | + |
| 137 | + |
| 138 | +## Running the application |
| 139 | + |
| 140 | +Build and run the sample using Maven. |
| 141 | + |
| 142 | + mvn package |
| 143 | + mvn exec:java -Dbigtable.projectID=GCLOUDPROJECT -Dbigtable.instanceID=BIGTABLEINSTANCE |
| 144 | + |
| 145 | +You will see output resembling the following, interspersed with informational logging |
| 146 | +from the underlying libraries: |
| 147 | + |
| 148 | + HelloWorld: Create table Hello-Bigtable |
| 149 | + HelloWorld: Write some greetings to the table |
| 150 | + HelloWorld: Scan for all greetings: |
| 151 | + Hello World! |
| 152 | + Hello Cloud Bigtable! |
| 153 | + Hello HBase! |
| 154 | + HelloWorld: Delete the table |
| 155 | + |
| 156 | +## View Z-Pages |
| 157 | + |
| 158 | +Open [http://localhost:8080/tracez](http://localhost:8080/trace) to see a local rendering |
| 159 | +of trace information |
| 160 | + |
| 161 | +## View Stackdriver tracing |
| 162 | + |
| 163 | +Open [Stackriver traces](https://pantheon.corp.google.com/traces/traces) to see traces |
| 164 | +in the Google Cloud Console. |
| 165 | + |
| 166 | +## Pom.xml |
| 167 | + |
| 168 | +Setting up the [pom.xml](pom.xml) is as follows. |
| 169 | + |
| 170 | +NOTE: cloud-bigtable-hbase-1.x-hadoop, cloud-bigtable-dataflow and cloud-bigtable-beam |
| 171 | +do not require this configuration, since they already include |
| 172 | +[shaded](https://maven.apache.org/plugins/maven-shade-plugin/) versions of tracing dependencies. |
| 173 | + |
| 174 | +```xml |
| 175 | + |
| 176 | + <!-- Opencensus dependencies. Pay close attention to the exclusions. --> |
| 177 | + |
| 178 | + <!-- OpenCensus Java implementation --> |
| 179 | + <dependency> |
| 180 | + <groupId>io.opencensus</groupId> |
| 181 | + <artifactId>opencensus-impl</artifactId> |
| 182 | + <version>${opencensus.version}</version> |
| 183 | + <exclusions> |
| 184 | + <exclusion> |
| 185 | + <groupId>io.grpc</groupId> |
| 186 | + <artifactId>*</artifactId> |
| 187 | + </exclusion> |
| 188 | + </exclusions> |
| 189 | + </dependency> |
| 190 | + |
| 191 | + <!-- Dependency for Z-Pages --> |
| 192 | + <dependency> |
| 193 | + <groupId>io.opencensus</groupId> |
| 194 | + <artifactId>opencensus-contrib-zpages</artifactId> |
| 195 | + <version>${opencensus.version}</version> |
| 196 | + <exclusions> |
| 197 | + <exclusion> |
| 198 | + <groupId>io.grpc</groupId> |
| 199 | + <artifactId>*</artifactId> |
| 200 | + </exclusion> |
| 201 | + </exclusions> |
| 202 | + </dependency> |
| 203 | + |
| 204 | + <!-- Dependency to export traces to stackdriver --> |
| 205 | + <dependency> |
| 206 | + <groupId>io.opencensus</groupId> |
| 207 | + <artifactId>opencensus-exporter-trace-stackdriver</artifactId> |
| 208 | + <version>${opencensus.version}</version> |
| 209 | + <exclusions> |
| 210 | + <exclusion> |
| 211 | + <groupId>io.grpc</groupId> |
| 212 | + <artifactId>*</artifactId> |
| 213 | + </exclusion> |
| 214 | + <exclusion> |
| 215 | + <groupId>io.netty</groupId> |
| 216 | + <artifactId>*</artifactId> |
| 217 | + </exclusion> |
| 218 | + </exclusions> |
| 219 | + </dependency> |
| 220 | +``` |
| 221 | + |
| 222 | +## Java code |
| 223 | + |
| 224 | +Here is the code from [HelloWorld](src/main/java/com/example/cloud/bigtable/helloworld/HelloWorld.java) |
| 225 | +that sets up tracing |
| 226 | + |
| 227 | +```java |
| 228 | + |
| 229 | + // Force tracing for every request for demo purposes. |
| 230 | + Tracing.getTraceConfig().updateActiveTraceParams( |
| 231 | + TraceParams.DEFAULT.toBuilder().setSampler(Samplers.probabilitySampler(1)).build()); |
| 232 | + |
| 233 | + // HBase Bigtable specific setup for zpages |
| 234 | + HBaseTracingUtilities.setupTracingConfig(); |
| 235 | + |
| 236 | + StackdriverExporter.createAndRegisterWithProjectId(projectId); |
| 237 | + |
| 238 | + // Start a web server on port 8080 for tracing data |
| 239 | + ZPageHandlers.startHttpServerAndRegisterAll(8080); |
| 240 | + |
| 241 | + doHelloWorld(projectId, instanceId); |
| 242 | + |
| 243 | + System.out.println("Sleeping for 1 minute so that you can view http://localhost:8080/tracez"); |
| 244 | + // Sleep for 1 minute. |
| 245 | + Thread.sleep(TimeUnit.MINUTES.toSeconds(1)); |
| 246 | + |
| 247 | +``` |
| 248 | + |
| 249 | +## Cleaning up |
| 250 | + |
| 251 | +To avoid incurring extra charges to your Google Cloud Platform account, remove |
| 252 | +the resources created for this sample. |
| 253 | + |
| 254 | +1. Go to the Clusters page in the [Cloud |
| 255 | + Console](https://console.cloud.google.com). |
| 256 | + |
| 257 | + [Go to the Clusters page](https://console.cloud.google.com/project/_/bigtable/clusters) |
| 258 | + |
| 259 | +1. Click the cluster name. |
| 260 | + |
| 261 | +1. Click **Delete**. |
| 262 | + |
| 263 | +  |
| 264 | + |
| 265 | +1. Type the cluster ID, then click **Delete** to delete the cluster. |
| 266 | + |
0 commit comments