|
| 1 | +# Subscribe to avro records |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The quickstart shows how to subscribe to receive avro messages that could be for |
| 6 | +different schema revisions. This example uses the [Avro C++] library and |
| 7 | +[C++ Cloud Pub/Sub] library to use the [Cloud Pub/Sub] service. The setup |
| 8 | +involves: |
| 9 | + |
| 10 | +1. Creating an initial schema (Schema 1) |
| 11 | +1. Creating a topic with Schema 1 |
| 12 | +1. Creating a subscription to the topic |
| 13 | +1. Publishing a message to the topic with Schema 1 |
| 14 | +1. Commiting a revision schema (Schema 2) |
| 15 | +1. Publishing a message to the topic with Schema 2 |
| 16 | +1. Recieve both messages using a subscriber |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +### 1. Create a project in the Google Cloud Platform Console |
| 21 | + |
| 22 | +If you haven't already created a project, create one now. |
| 23 | + |
| 24 | +Projects enable you to manage all Google Cloud Platform resources for your app, |
| 25 | +including deployment, access control, billing, and services. |
| 26 | + |
| 27 | +1. Open the [Cloud Platform Console](https://console.cloud.google.com/). |
| 28 | +1. In the drop-down menu at the top, select Create a project. |
| 29 | +1. Give your project a name. |
| 30 | +1. Make a note of the project ID, which might be different from the project |
| 31 | + name. The project ID is used in commands and in configurations. |
| 32 | + |
| 33 | +### 2. Enable billing for your project |
| 34 | + |
| 35 | +If you haven't already enabled billing for your project, |
| 36 | +[enable billing now](https://console.cloud.google.com/project/_/settings). |
| 37 | +Enabling billing allows the application to consume billable resources such as |
| 38 | +Pub/Sub API calls. |
| 39 | + |
| 40 | +See |
| 41 | +[Cloud Platform Console Help](https://support.google.com/cloud/answer/6288653) |
| 42 | +for more information about billing settings. |
| 43 | + |
| 44 | +### 3. Enable APIs for your project |
| 45 | + |
| 46 | +[Click here](https://console.cloud.google.com/flows/enableapi?apiid=speech&showconfirmation=true) |
| 47 | +to visit Cloud Platform Console and enable the Pub/Sub and Trace API via the UI. |
| 48 | + |
| 49 | +Or use the CLI: |
| 50 | + |
| 51 | +``` |
| 52 | +gcloud services enable pubsub.googleapis.com |
| 53 | +``` |
| 54 | + |
| 55 | +## Build using CMake and Vcpkg |
| 56 | + |
| 57 | +To build and run the sample, [setup a C++ development environment]. |
| 58 | + |
| 59 | +### 1. Install vcpkg |
| 60 | + |
| 61 | +This project uses [`vcpkg`](https://github.com/microsoft/vcpkg) for dependency |
| 62 | +management. Clone the vcpkg repository to your preferred location. In these |
| 63 | +instructions we use`$HOME`: |
| 64 | + |
| 65 | +```shell |
| 66 | +git clone -C $HOME https://github.com/microsoft/vcpkg.git |
| 67 | +cd $HOME/vcpkg |
| 68 | +./vcpkg install google-cloud-cpp |
| 69 | +``` |
| 70 | + |
| 71 | +### 2. Download or clone this repo |
| 72 | + |
| 73 | +```shell |
| 74 | +git clone https://github.com/GoogleCloudPlatform/cpp-samples |
| 75 | +``` |
| 76 | + |
| 77 | +### 3. Compile these examples |
| 78 | + |
| 79 | +Use the `vcpkg` toolchain file to download and compile dependencies. This file |
| 80 | +would be in the directory you cloned `vcpkg` into, `$HOME/vcpkg` if you are |
| 81 | +following the instructions to the letter. Note that building all the |
| 82 | +dependencies can take up to an hour, depending on the performance of your |
| 83 | +workstation. These dependencies are cached, so a second build should be |
| 84 | +substantially faster. |
| 85 | + |
| 86 | +```sh |
| 87 | +cd cpp-samples/pubsub-open-telemetry |
| 88 | +cmake -S . -B .build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake -G Ninja |
| 89 | +cmake --build .build |
| 90 | +``` |
| 91 | + |
| 92 | +## Setup the Pub/Sub messages |
| 93 | + |
| 94 | +Export the following environment variables to run the setup scripts: |
| 95 | + |
| 96 | +```shell |
| 97 | +export GOOGLE_CLOUD_PROJECT=[PROJECT ID] # Use your project ID here |
| 98 | +export GOOGLE_CLOUD_TOPIC=avro-topic |
| 99 | +export GOOGLE_CLOUD_SUBSCRIPTION=avro-sub |
| 100 | +export GOOGLE_CLOUD_SCHEMA_NAME=state |
| 101 | +export GOOGLE_CLOUD_SCHEMA_FILE1=schema1.avro |
| 102 | +export GOOGLE_CLOUD_SCHEMA_FILE2=schema2.avro |
| 103 | +``` |
| 104 | + |
| 105 | +```shell |
| 106 | +./setup.sh |
| 107 | +``` |
| 108 | + |
| 109 | +## Run the example |
| 110 | + |
| 111 | +This will resolve the schemas when recieving the message and return data in the |
| 112 | +format of schema2, even if it was sent in the format of schema1. |
| 113 | + |
| 114 | +```sh |
| 115 | +.build/quickstart ${GOOGLE_CLOUD_PROJECT} avro-sub schema2.avro |
| 116 | +``` |
| 117 | + |
| 118 | +If you want to send more message to test, you can use the following commands to |
| 119 | +send a message in schema1 |
| 120 | + |
| 121 | +```sh |
| 122 | +gcloud pubsub topics publish ${GOOGLE_CLOUD_TOPIC} \ |
| 123 | + --project ${GOOGLE_CLOUD_PROJECT} \ |
| 124 | + --message '{"name": "New York", "post_abbr": "NY"}' |
| 125 | +``` |
| 126 | + |
| 127 | +Or in schema2 |
| 128 | + |
| 129 | +```sh |
| 130 | +gcloud pubsub topics publish ${GOOGLE_CLOUD_TOPIC} \ |
| 131 | + --project ${GOOGLE_CLOUD_PROJECT} \ |
| 132 | + --message '{"name": "New York", "post_abbr": "NY", "population": 10000}' |
| 133 | +``` |
| 134 | + |
| 135 | +## Cleanup |
| 136 | + |
| 137 | +To delete the created resources (topic, subscription, schema), run: |
| 138 | + |
| 139 | +```shell |
| 140 | +./cleanup.sh |
| 141 | +``` |
| 142 | + |
| 143 | +## Platform Specific Notes |
| 144 | + |
| 145 | +### macOS |
| 146 | + |
| 147 | +gRPC [requires][grpc-roots-pem-bug] an environment variable to configure the |
| 148 | +trust store for SSL certificates, you can download and configure this using: |
| 149 | + |
| 150 | +```bash |
| 151 | +curl -Lo roots.pem https://pki.google.com/roots.pem |
| 152 | +export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH="$PWD/roots.pem" |
| 153 | +``` |
| 154 | + |
| 155 | +### Windows |
| 156 | + |
| 157 | +gRPC [requires][grpc-roots-pem-bug] an environment variable to configure the |
| 158 | +trust store for SSL certificates, you can download and configure this using: |
| 159 | + |
| 160 | +```console |
| 161 | +@powershell -NoProfile -ExecutionPolicy unrestricted -Command ^ |
| 162 | + (new-object System.Net.WebClient).Downloadfile( ^ |
| 163 | + 'https://pki.google.com/roots.pem', 'roots.pem') |
| 164 | +set GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=%cd%\roots.pem |
| 165 | +``` |
| 166 | + |
| 167 | +[avro c++]: https://avro.apache.org/docs/1.11.1/api/cpp/html/ |
| 168 | +[c++ cloud pub/sub]: https://cloud.google.com/cpp/docs/reference/pubsub/latest |
| 169 | +[cloud pub/sub]: https://cloud.google.com/pubsub/docs |
| 170 | +[grpc-roots-pem-bug]: https://github.com/grpc/grpc/issues/16571 |
| 171 | +[setup a c++ development environment]: cloud.google.com/cpp/docs/setup |
0 commit comments