Intermediate: Data management (Distributed Data Service) in Harmony OS
Introduction
Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Distributed Data Service (DDS) with Java in Harmony OS.
DDS provides the capability to store data in the databases of different devices. Using DDS API service we can save data in Distributed database. DDS synchronize application data between different devices and handles database version compatibility issues and data synchronization conflicts easily.
Working Principles
DDS supports distributed management of application data. Data can be synchronize between login with same account.
When to Use
DDS synchronizes application data on different devices in a distributed way. When an application on a device inserts, deletes, or updates data in the distributed database, the same application on another device can obtain the data changes.
Development Overview
You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.
Hardware Requirements
- A computer (desktop or laptop) running Windows 10.
- A Huawei phone (with the USB cable), which is used for debugging.
Software Requirements
- Java JDK installation package.
- DevEcho studio installed.
Follows the steps.
1. Create Unity Project.
- Open DevEcho studio.
- Click NEW Project, select a Project Templet.
- Select ability template and click Next as per below image.
- Enter Project and Package Name and click on Finish.
2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.
3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values. Add below permission to access distributed database.
4. Create New Ability as follows.
5. Development Procedure.
Create MainAbilitySlice.java ability and add the below code.
Create ability_main.xml layout and add the below code.
Create StoreKVdetail.java ability and add the below code.
Create store_kv_detail_layout.xml layout and add the below code.
Create KvStoreObserverClient.java ability and add the below code.
6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.
Result
- Run Application on connected device and Click on UI Write data Single KV Store button. It will navigate to StoreKVdetail screen as per below images.
2. Enter data and click on “Save KV Detail” button. It will store data in Distributed database as per below images.
3. Click on “Read Data Single KV Store” button, we will read data from distributed database as per below images.
Tips and Tricks
- Always use the latest version of DevEcho Studio.
- Use Harmony Device Simulator from HVD section.
- Do not forgot to add permission in config.json file.
Conclusion
In this article, we have learnt Distributed database service in Harmony OS. Distributed Data Service (DDS) provides the capability to store data in the databases of different devices. When an application on a device inserts, deletes, or updates data in the distributed database, the same application on another device can obtain the data changes.
Thanks for reading the article, please do like and comment your queries or suggestions.
References
Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1
Distributed database service: https://developer.harmonyos.com/en/docs/documentation/doc-guides/database-mdds-overview-0000001160636563?ha_source=hms1