Write example
On this page, find instructions on how to build and run the write example project on different platforms and publish data to a stream layer using HERE Data SDK for C++.
Before you run the example project, authorize to the HERE platform:
-
On the Apps & keys page, copy your application access key ID and access key secret.
For instructions on how to get the access key ID and access key secret, see Register your application section in the Identity & Access Management Developer Guide.
-
In examples/main.cpp, replace the placeholders with your access key ID, access key secret, Here Resource Name (HRN) of the catalog, and name of the layer to which you want to publish data.
Note
You can also specify these values using the command line options.
AccessKey access_key{};
std::string catalog;
std::string layer_id;
Build and run on Linux
To build and run the example project on Linux:
-
Enable examples of the CMake targets.
mkdir build && cd build
cmake -DOLP_SDK_BUILD_EXAMPLES=ON ..
-
In the build folder, build the example project.
cmake --build . --target dataservice-example
-
Execute the example project.
./examples/dataservice-example --example write --key_id "here.access.key.id" --key_secret "here.access.key.secret" --catalog "catalog" --layer_id "layer_id"
-
(Optional) To run the example with other parameters, run the help command, and then select the needed parameter.
./examples/dataservice-example --help
After building and running the example project, the following message displays automatically: "Publish Successful - TraceID: \".
Build and run on Android
To integrate the Data SDK libraries in the Android example project:
Prerequisites
Before you integrate the Data SDK libraries in the Android example project:
- Set up the Android environment.
-
In examples/android/app/src/main/cpp/MainActivityNative.cpp.in
, replace the placeholders with your application access key ID, access key secret, catalog HRN, and layer name and specify that the example should run RunExampleWrite
.
For instructions on how to get the access key ID and access key secret, see Register your application section in the Identity & Access Management Developer Guide.
Build the Data SDK
To build the Data SDK on Android:
- Set
OLP_SDK_BUILD_EXAMPLES
to ON
. - Specify the path to the Android NDK toolchain file using the
CMAKE_TOOLCHAIN_FILE
variable. -
If you want to build the SDK for a specific Android platform, use the -DANDROID_PLATFORM CMake
flag, and if you want to build the SDK for a specific Android architecture, use the -DANDROID_ABI
flag. For more details, see NDK-specific CMake variables.
mkdir build && cd build
cmake .. -DOLP_SDK_BUILD_EXAMPLES=ON -DCMAKE_TOOLCHAIN_FILE=$NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a
The CMake command generates a Gradle
project in the build/examples/android
folder.
-
Install the Data SDK libraries into the sysroot directory.
(sudo) make install
Build and run the APK
To build and run the APK:
- In the Android Studio IDE, open the
build/examples/android/build.gradle
script. - Provide your application access key ID, access key secret, catalog HRN, and layer name.
- Install and run the
dataservice_example
APK.
The main screen displays the following message: "Example has finished successfully".
Build and run on iOS
To integrate the Data SDK libraries in the iOS example application written in the Objective-C language:
Prerequisites
Before you integrate the Data SDK libraries in the iOS example project:
- To set up the iOS development environment, install the Xcode and command-line tools.
-
Install external dependencies.
For information on dependencies and installation instructions, see the related section in the README.md file.
-
In examples/ios/ViewController.mm
, replace the placeholders with your application access key ID, access key secret, catalog HRN, and layer name and specify that the example should run RunExampleWrite
.
For instructions on how to get the access key ID and access key secret, see Register your application section in the Identity & Access Management Developer Guide.
mkdir build && cd build
cmake .. -GXcode -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/iOS.cmake -DPLATFORM=iphoneos -DOLP_SDK_BUILD_EXAMPLES=ON -DOLP_SDK_ENABLE_TESTING=OFF
To configure the Data SDK for a simulator, set the SIMULATOR
variable to ON
.
Build the Data SDK
To build the Data SDK on iOS:
- Set
OLP_SDK_BUILD_EXAMPLES
to ON
. - (Optional) To disable tests, set
OLP_SDK_ENABLE_TESTING
to OFF
. -
Specify the path to the iOS toolchain file using the CMAKE_TOOLCHAIN_FILE
variable.
Note
The iOS toolchain file is shipped together with the SDK and located under the <olp-sdk-root>/cmake/toolchains/iOS.cmake
.
mkdir build && cd build
cmake .. -GXcode -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/iOS.cmake -DPLATFORM=iphoneos -DOLP_SDK_BUILD_EXAMPLES=ON -DOLP_SDK_ENABLE_TESTING=OFF
Build and run the application
To build an run the example application on iOS:
-
Open the generated Xcode project.
open olp-cpp-sdk.xcodeproj
-
In the Xcode project, from the list of schemes, select the dataservice-example
scheme.
-
In the dataservice-example
target, specify your application access key ID and access key secret.
-
Build and run the example application.
The main UI screen displays the following message: "Example has finished successfully". For more details, check the device logs.
If you encounter an error message, for a detailed error description, check the device logs. Example of an error message: "Example failed!".
How it works
Publish data to a stream layer
You can create a queue that streams data to data consumers in real time using a stream layer.
To publish data to the stream layer:
-
Create the OlpClientSettings
object.
For instructions, see Create platform client settings.
-
Create the StreamLayerClientSettings
object.
auto stream_client_settings = olp::dataservice::write::StreamLayerClientSettings{};
-
Create the StreamLayerClient
object with the HERE Resource Name (HRN) of the catalog that contains the layer, the stream layer client settings from step 2, and the platform client settings from step 1.
auto client = olp::dataservice::write::StreamLayerClient(
olp::client::HRN{kCatalogHRN}, stream_client_settings, client_settings);
-
Create the PublishDataRequest
object with the data that you want to publish and layer ID.
auto request = PublishDataRequest().WithData(buffer).WithLayerId(kLayer);
-
Call the PublishData
method with the DataRequest
parameter.
auto futureResponse = client.PublishData(request);
-
Wait for the PublishDataResponse
future.
auto response = futureResponse.GetFuture().get();
The PublishDataResponse
object holds details of the completed operation and is used to determine operation success and access resultant data:
-
IsSuccessful()
– if the operation is successful, returns true
. Otherwise, returns false
. -
GetResult()
– if the operation is successful, returns the following resultant data: olp::dataservice::write::PublishDataResult
-
GetError()
– contains error information as a result of an error in the olp::client::ApiError
object.
if (response.IsSuccessful()) {
auto response_result = response.GetResult();
} else {
auto api_error = response.GetError();
}