- How do I use distributed databases
Distributed Data Service (DDS) provides applications with the ability to distribute database Data among different devices. By invoking the distributed data interface, the application saves the data to a distributed database. By combining the triad of account, application and database, the distributed data service isolates the data belonging to different applications and ensures that the data between different applications cannot be accessed from each other through the distributed data service. Distributed data services support synchronization of application data across trusted devices, providing users with a consistent data access experience across multiple end devices. A more detailed description of distributed data services can be found in the distributed data services game. This tutorial will walk you through the following and show you complete examples:
Distributed Database Creation Distributed Database Insertion and Deletion Distributed Database Data Synchronization
- Implementing a distributed database needs to apply for permission
In order to realize the distributed database, need to entry \ SRC \ main \ config application ohos in json. Permission. DISTRIBUTED_DATASYNC permissions, in order to allow the data exchange between different devices. The sample code is as follows:
"reqPermissions": [
{
"name": "ohos.permission.DISTRIBUTED_DATASYNC"
}
]
}
- How do I create a distributed database
To create a distributed database, the first thing to do is to create a distributed database manager instance, kvManager. We define the following methods:
KvManager manager = null;
try {
KvManagerConfig config = new KvManagerConfig(this);
manager = KvManagerFactory.getInstance().createKvManager(config);
}
catch (KvStoreException exception) {
HiLog.info(LABEL_LOG, LOG_FORMAT,TAG, "some exception happen");
}
return manager;
}
After successful creation, create the SINGLE_VERSION distributed database with KvManager, as follows:
SingleKvStore kvStore = null;
try {
Options options = new Options();
options.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION);
kvStore = kvManager.getKvStore(options, STORE_ID);
} catch (KvStoreException exception) {
HiLog.info(LABEL_LOG, LOG_FORMAT,TAG, "some exception happen");
}
return kvStore;
}
- How do I query, insert, and delete data
Insert data into
Before the data is written to the distributed database, the Key and Value of the distributed database need to be constructed first, and the data is written to the database through the putString method. The specific examples are as follows:
if (key == null || key.isEmpty() || value == null || value.isEmpty()) {
return;
}
singleKvStore.putString(key, value);
}
Data query
The data query in the distributed database is carried out according to the Key. If the Key is specified, the data corresponding to the Key will be queried. If the Key is not specified, it is null, then all the data is queried. The query sample code is as follows
List entryList = singleKvStore.getEntries("");
contactArray.clear();
try {
for (Entry entry : entryList) {
contactArray.add(new Contacter(entry.getValue().getString(), entry.getKey()));
}
} catch (KvStoreException exception) {
HiLog.info(LABEL_LOG, LOG_FORMAT,TAG,"the value must be String");
}
contactAdapter.notifyDataChanged();
}
- How to synchronize distributed databases
Before data synchronization, it is necessary to first get the list of devices in the current networking environment, and then specify the synchronization mode (PULL_ONLY,PUSH_ONLY,PUSH_PULL) for synchronization. Take PUSH_PULL mode as an example, the example code is as follows:
List<DeviceInfo> deviceInfoList = kvManager.getConnectedDevicesInfo(DeviceFilterStrategy.NO_FILTER); List<String> deviceIdList = new ArrayList<>(); for (DeviceInfo deviceInfo : deviceInfoList) { deviceIdList.add(deviceInfo.getId()); } HiLog.info(LABEL_LOG, LOG_FORMAT,TAG, "device size= " + deviceIdList.size()); If (deviceidlist.size () == 0) {showTip(" Failed "); return; } singleKvStore.registerSyncCallback(new SyncCallback() { @Override public void syncCompleted(Map<String, Integer> map) { getUITaskDispatcher().asyncDispatch(new Runnable() { @Override public void run() { queryContact(); ShowTip (" Synchronized success "); }}); singleKvStore.unRegisterSyncCallback(); }}); singleKvStore.sync(deviceIdList, SyncMode.PUSH_PULL); }
- The final effect
- Example code code structure interpretation
In order to facilitate your learning, we provide the complete code of the distributed database example project. The project takes a simple information management system as the background, and the engineering structure of the code is as follows: