Skip to content

Commit 0f79d85

Browse files
authored
Document how to read from a stream layer (#745)
Add documentation that describes how to subscribe to a stream layer, get data from it, and unsubscribe. Relates-To: OLPEDGE-1726 Signed-off-by: Halyna Dumych <[email protected]>
1 parent d13055f commit 0f79d85

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Read from a Stream Layer (Example)
2+
3+
On this page, you can find instructions on how to build and run the example project on different platforms and get catalog and partition metadata, as well as partition data using the HERE Open Location Platform (OLP) SDK for C++.
4+
5+
Before you run the example project, authorize to HERE OLP:
6+
7+
1. On the [Apps & keys](https://platform.here.com/admin/apps) page, copy your application access key ID and access key secret.
8+
For instructions on how to get the access key ID and access key secret, see the [Get Credentials](https://developer.here.com/olp/documentation/access-control/user-guide/topics/get-credentials.html) section in the Terms and Permissions User Guide.
9+
10+
2. In `examples/main.cpp`, replace the placeholders with your access key ID, access key secret, and Here Resource Name (HRN) of the catalog.
11+
**Note:** You can also specify these values using the command line options.
12+
13+
```cpp
14+
AccessKey access_key{"here.access.key.id", "here.access.key.secret"};
15+
```
16+
17+
## Build and Run on Linux
18+
19+
To build and run the example project on Linux:
20+
21+
1. Enable examples of the `CMake` targets.
22+
23+
```bash
24+
mkdir build && cd build
25+
cmake -DOLP_SDK_BUILD_EXAMPLES=ON ..
26+
```
27+
28+
2. In the **build** folder, build the example project.
29+
30+
```bash
31+
cmake --build . --target dataservice-example
32+
```
33+
34+
3. Execute the example project.
35+
36+
```bash
37+
./examples/dataservice-example --example read_stream --key_id "here.access.key.id" --key_secret "here.access.key.secret" --catalog "catalog"
38+
```
39+
40+
4. (Optional) To run the example with other parameters, run the help command, and then select the needed parameter.
41+
42+
```bash
43+
./examples/dataservice-example --help
44+
```
45+
46+
After building and running the example project, you see the following information:
47+
48+
- `./dataservice-example` &ndash; a catalog and layer description
49+
- `read-stream-layer-example - Poll data - Success, messages size - 6` - a success message that displays the size of data retrieved from the layer.
50+
51+
```bash
52+
./dataservice-example --example read --key_id "here.access.key.id" --key_secret "here.access.key.secret" --catalog "catalog" --layer_id "layer_id" --type-of-subscription "subscription_type"
53+
[INFO] read-stream-layer-example - Message data: size - 16
54+
[INFO] read-stream-layer-example - No new messages is received
55+
[INFO] read-stream-layer-example - Poll data - Success, messages size - 6
56+
```
57+
58+
## How it works
59+
60+
### <a name="authenticate-to-here-olp-using-client-credentials"></a>Authenticate to HERE OLP Using Client Credentials
61+
62+
To authenticate with the Open Location Platform (OLP), you must get platform credentials that contain the access key ID and access key secret.
63+
64+
To authenticate using client credentials:
65+
66+
1. Get your platform credentials. For instructions, see the [Get Credentials](https://developer.here.com/olp/documentation/access-control/user-guide/topics/get-credentials.html) section in the Terms and Permissions User Guide.
67+
68+
You get the `credentials.properties` file.
69+
70+
2. Initialize the authentication settings using the **here.access.key.іd** and **here.access.key.secret** from the `credentials.properties` file as `access_key.id` and `access_key.secret` respectively.
71+
72+
```cpp
73+
olp::authentication::Settings settings({access_key.id, access_key.secret});
74+
settings.task_scheduler = task_scheduler;
75+
settings.network_request_handler = http_client;
76+
```
77+
78+
3. To get the OAuth 2.0 token from OLP, set up the `AuthenticationSettings` object with a default token provider.
79+
80+
```cpp
81+
olp::client::AuthenticationSettings auth_settings;
82+
auth_settings.provider =
83+
olp::authentication::TokenProviderDefault(std::move(settings));
84+
```
85+
86+
You can use the `TokenProvider` object to create the `OlpClientSettings` object. For more information, see [Create OlpClientSettings](#create-olpclientsettings).
87+
88+
### <a name="create-olpclientsettings"></a>Create `OlpClientSettings`
89+
90+
You need to create the `OlpClientSettings` object to get catalog and partition metadata and retrieve versioned and volatile layer data from OLP.
91+
92+
To create the `OlpClientSettings` object:
93+
94+
1. To perform requests asynchronously, create the `TaskScheduler` object.
95+
96+
```cpp
97+
std::shared_ptr<olp::thread::TaskScheduler> task_scheduler =
98+
olp::client::OlpClientSettingsFactory::CreateDefaultTaskScheduler(1u);
99+
```
100+
101+
2. To internally operate with the OLP services, create the `Network` client.
102+
103+
```cpp
104+
std::shared_ptr<olp::http::Network> http_client = olp::client::
105+
OlpClientSettingsFactory::CreateDefaultNetworkRequestHandler();
106+
```
107+
108+
> **Note:** The `Network` client is designed and intended to be shared.
109+
110+
3. [Authenticate](#authenticate-to-here-olp-using-client-credentials) to OLP.
111+
112+
4. Set up the `OlpClientSettings` object.
113+
114+
```cpp
115+
olp::client::OlpClientSettings client_settings;
116+
client_settings.authentication_settings = auth_settings;
117+
client_settings.task_scheduler = std::move(task_scheduler);
118+
client_settings.network_request_handler = std::move(http_client);
119+
client_settings.cache =
120+
olp::client::OlpClientSettingsFactory::CreateDefaultCache({});
121+
```
122+
123+
The `OlpClientSettings` class pulls together all the settings for customization of the client library behavior.
124+
125+
### Get Data from a Stream Layer
126+
127+
You can read messages from a [stream layer](https://developer.here.com/olp/documentation/data-user-guide/portal/layers/layers.html#stream-layers) if you subscribe to it.
128+
129+
To get data from the stream layer:
130+
131+
1. Get an access key ID and access key secret. For instructions, see [Authenticate to HERE OLP Using Client Credentials](#authenticate-to-here-olp-using-client-credentials).
132+
133+
2. Create the `OlpClientSettings` object. For instructions, see [Create OLP Client Settings](#create-olpclientsettings).
134+
135+
3. Create the `StreamLayerClient` object with the HERE Resource Name (HRN) of the catalog that contains the layer, layer ID, and OLP client settings from step 2.
136+
137+
```cpp
138+
olp::dataservice::read::StreamLayerClient client(
139+
client::HRN catalog, std::string layer_id,
140+
client::OlpClientSettings settings);
141+
```
142+
143+
4. Create the `SubscribeRequest` object with the `serial` or `parallel` subscription type.
144+
145+
- If your application should read smaller volumes of data using a single subscription, use the `serial` subscription type.
146+
147+
- If your application should read large volumes of data in a parallel manner, use the `parallel` subscription type and subscription ID.
148+
149+
```cpp
150+
auto request = olp::dataservice::read::SubscribeRequest()
151+
.WithSubscriptionMode(olp::dataservice::read::SubscribeRequest::SubscriptionMode::kSerial));
152+
```
153+
154+
5. Call the `Subscribe` method with the `SubscribeRequest` parameter.
155+
156+
```cpp
157+
client::CancellableFuture<SubscribeResponse> Subscribe(
158+
SubscribeRequest request);
159+
```
160+
161+
You receive a subscription ID from the requested subscription to the selected layer.
162+
163+
6. Call the `Poll` method.
164+
165+
```cpp
166+
client::CancellableFuture<PollResponse> Poll();
167+
```
168+
169+
You get messages with the layer data and partition metadata. The `Poll` method also commits the offsets, so you can continue polling new messages.
170+
171+
If the data size is less than 1 MB, the `data` field is populated. If the data size is greater than 1 MB, you get a data handle that points to the object stored in the blob store.
172+
173+
7. If the data size is greater than 1 MB, call the `GetData` method with the `Messages` instance.
174+
175+
```cpp
176+
client::CancellableFuture<DataResponse> GetData(
177+
const model::Message& message);
178+
```
179+
180+
You get data from the requested partition.
181+
182+
### Delete Your Subscription to a Stream Layer
183+
184+
If you want to stop message consumption, delete your subscription to a stream layer:
185+
186+
```cpp
187+
auto unsubscribe_response = stream_client.Unsubscribe().GetFuture().get();
188+
if (unsubscribe_response.IsSuccessful()) {
189+
// Successfully unsubscribed.
190+
}
191+
```

0 commit comments

Comments
 (0)