日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
HarmonyOS基礎(chǔ)技術(shù)賦能之分布式數(shù)據(jù)服務(wù)功能

HarmonyOS基礎(chǔ)技術(shù)賦能之分布式數(shù)據(jù)服務(wù)功能

作者:軟通張二龍 2021-08-27 09:57:18

開發(fā)

前端

分布式

OpenHarmony 分布式數(shù)據(jù)服務(wù)(Distributed Data Service,DDS) 為應(yīng)用程序提供不同設(shè)備間數(shù)據(jù)庫數(shù)據(jù)分布式的能力。通過調(diào)用分布式數(shù)據(jù)接口,應(yīng)用程序?qū)?shù)據(jù)保存到分布式數(shù)據(jù)庫中。

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

引言

分布式數(shù)據(jù)服務(wù)(Distributed Data Service,DDS) 為應(yīng)用程序提供不同設(shè)備間數(shù)據(jù)庫數(shù)據(jù)分布式的能力。通過調(diào)用分布式數(shù)據(jù)接口,應(yīng)用程序?qū)?shù)據(jù)保存到分布式數(shù)據(jù)庫中。通過結(jié)合帳號、應(yīng)用和數(shù)據(jù)庫三元組,分布式數(shù)據(jù)服務(wù)對屬于不同應(yīng)用的數(shù)據(jù)進(jìn)行隔離,保證不同應(yīng)用之間的數(shù)據(jù)不能通過分布式數(shù)據(jù)服務(wù)互相訪問。在通過可信認(rèn)證的設(shè)備間,分布式數(shù)據(jù)服務(wù)支持應(yīng)用數(shù)據(jù)相互同步,為用戶提供在多種終端設(shè)備上最終一致的數(shù)據(jù)訪問體驗。

功能介紹

此次通過HarmonyOS的分布式數(shù)據(jù)服務(wù)能力,一方面可以實現(xiàn)自身應(yīng)用界面的數(shù)據(jù)實時更新;另一方面也可以實現(xiàn)不同設(shè)備之間的數(shù)據(jù)實時更新。前提是在不同設(shè)備之間,要實現(xiàn)分布式數(shù)據(jù)服務(wù)的同步能力,需要同一個華為賬號登錄、并一個應(yīng)用包名、同一個網(wǎng)絡(luò)之間進(jìn)行,也可以兩個設(shè)備同時開啟藍(lán)牙。

開發(fā)指南

1. 在config.json中添加permisssion權(quán)限。

  
 
 
 
  1. // 添加在abilities同一目錄層級 
  2. "reqPermissions": [ 
  3.     { 
  4.         "name": "ohos.permission.DISTRIBUTED_DATASYNC" 
  5.     } 

2. 在MainAbility中添加權(quán)限

  
 
 
 
  1. @Override 
  2. public void onStart(Intent intent) { 
  3.   super.onStart(intent); 
  4.   super.setMainRoute(MainAbilitySlice.class.getName()); 
  5.   //實現(xiàn)Ability的代碼中顯式聲明需要使用多設(shè)備協(xié)同訪問的權(quán)限 
  6.   requestPermissionsFromUser(new String[]{ 
  7.       "ohos.permission.DISTRIBUTED_DATASYNC"}, 0); 
  8.  

3. 根據(jù)配置構(gòu)造分布式數(shù)據(jù)庫管理類實例KvManager以及創(chuàng)建分布式數(shù)據(jù)庫對象SingleKvStore。

  
 
 
 
  1. //實現(xiàn)數(shù)據(jù)庫的初始化 
  2. // 初入的參數(shù)context: Context context = getApplicationContext()獲得;storeId為分布式數(shù)據(jù)庫id,String類型,可自行定義,例如“testApp”。 
  3. public static SingleKvStore initOrGetDB(Context context, String storeId) { 
  4.   KvManagerConfig kvManagerConfig = new KvManagerConfig(context); 
  5.   kvManager = KvManagerFactory.getInstance().createKvManager(kvManagerConfig); 
  6.   Options options = new Options(); 
  7.   options.setCreateIfMissing(true) 
  8.     .setEncrypt(false) 
  9.     .setKvStoreType(KvStoreType.SINGLE_VERSION) //數(shù)據(jù)庫類型:單版本分布式數(shù)據(jù)庫 
  10.     .setAutoSync(true);//設(shè)置數(shù)據(jù)為自動同步 
  11.   singleKvStore = kvManager.getKvStore(options, storeId); 
  12.   return singleKvStore; 

4. 將數(shù)據(jù)寫入單版本分布式數(shù)據(jù)庫。

  
 
 
 
  1. //以key-value形式存儲到分布式數(shù)據(jù)庫 
  2. try { 
  3.   long id = System.currentTimeMillis(); 
  4.   singleKvStore.putString("key", 
  5.       "{\"id\":" + id + 
  6.           ",\"temp\":" + temperature + 
  7.           ",\"humidity\":" + humidity + 
  8.           ",\"NH4\":" + 0.0 + 
  9.           ",\"H2S\":" + 0.0 + 
  10.           ",\"other\":" + gas + "}"); 
  11. } catch (KvStoreException e) { 
  12.   e.printStackTrace(); 

5.訂閱分布式數(shù)據(jù)變化??蛻舳诵枰獙崿F(xiàn)KvStoreObserver接口,監(jiān)聽數(shù)據(jù)變化。

  
 
 
 
  1. try { 
  2. //訂閱類型SubscribeType.SUBSCRIBE_TYPE_ALL意思可以同步到本機(jī)和其他外圍設(shè)備 
  3.   innerKvStoreObserver = new InnerKvStoreObserver(); 
  4.   singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL, innerKvStoreObserver); 
  5. } catch (KvStoreException e) { 
  6.   e.printStackTrace(); 
  7.  
  8. public class InnerKvStoreObserver implements KvStoreObserver { 
  9.  
  10.   @Override 
  11.   public void onChange(ChangeNotification changeNotification) { 
  12.     //刷新頁面上的數(shù)據(jù),同樣有一個坑,onChange方法實質(zhì)上,在一個子線程里執(zhí)行 
  13.     MainAbilitySlice.taskDispatcher.asyncDispatch(() -> { 
  14.       //在這里執(zhí)行頁面ui組件的顯示刷新 
  15.       flushUIData(); 
  16.     }); 
  17.   } 

6.獲取分布式數(shù)據(jù)庫數(shù)據(jù)

  
 
 
 
  1. private void flushUIData() { 
  2.   //查詢分布式數(shù)據(jù)的數(shù)據(jù),獲取數(shù)據(jù)可以通過get(String key)/ getEntries(String key)方法獲取數(shù)據(jù) 
  3.   List entries = singleKvStore.getEntries(“key”); 
  4.   if (entries.size() > 0) { 
  5.     ZSONObject zsonObject = ZSONObject.stringToZSON(entries.get(0).getValue().getString()); 
  6.     int temp = zsonObject.getIntValue("temp"); 
  7.     int humidity = zsonObject.getIntValue("humidity"); 
  8.     int other = zsonObject.getIntValue("other"); 
  9.     tvTemp.setText(temp+"℃"); 
  10.     tvHumi.setText(humidity+"% RH"); 
  11.     tvGas.setText(other+"% LEL"); 
  12.   } 

7. 解除訂閱。一般在頁面銷毀時調(diào)用,也就是MainAbilitySlice的onStop()中調(diào)用

  
 
 
 
  1. if (singleKvStore != null) { 
  2.   singleKvStore.unSubscribe(innerKvStoreObserver); 

8. 同步數(shù)據(jù)到其他設(shè)備。獲取已連接的設(shè)備列表,選擇同步方式進(jìn)行數(shù)據(jù)同步

  
 
 
 
  1. List deviceInfoList = kvManager.getConnectedDevicesInfo(DeviceFilterStrategy.NO_FILTER); 
  2. List deviceIdList = new ArrayList<>(); 
  3. for (DeviceInfo deviceInfo : deviceInfoList) { 
  4.     deviceIdList.add(deviceInfo.getId()); 
  5. singleKvStore.sync(deviceIdList, SyncMode.PUSH_ONLY); 

項目中采用在后臺service中開啟定時任務(wù),實時保存數(shù)據(jù)到分布式數(shù)據(jù)庫,然后在主界面,監(jiān)聽數(shù)據(jù)變化,實時更新數(shù)據(jù)。

結(jié)果演示

1.剛開始安裝完成后效果:

2.每隔3秒,界面數(shù)據(jù)都會發(fā)生變化:

附上源碼:

1.MainAbilitySlice

  
 
 
 
  1. public class MainAbilitySlice extends AbilitySlice { 
  2.   private SingleKvStore singleKvStore; 
  3.   private Text tvTemp; 
  4.   private Text tvHumi; 
  5.   private Text tvGas; 
  6.   private Intent serviceIntent; 
  7.   private InnerKvStoreObserver innerKvStoreObserver; 
  8.  
  9.   @Override 
  10.   public void onStart(Intent intent) { 
  11.     super.onStart(intent); 
  12.     super.setUIContent(ResourceTable.Layout_ability_main); 
  13.     tvTemp=(Text)findComponentById(ResourceTable.Id_tvTemp); 
  14.     tvHumi=(Text)findComponentById(ResourceTable.Id_tvHumi); 
  15.     tvGas=(Text)findComponentById(ResourceTable.Id_tvGas); 
  16.     initService(); 
  17.  
  18.     try { 
  19.       //獲取數(shù)據(jù)庫 
  20.       singleKvStore = DBUtils.initOrGetDB(this, DBUtils.STORE_ID); 
  21.       innerKvStoreObserver = new InnerKvStoreObserver(); 
  22.       singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL, innerKvStoreObserver); 
  23.     } catch (KvStoreException e) { 
  24.       e.printStackTrace(); 
  25.     } 
  26.   } 
  27.  
  28.   public class InnerKvStoreObserver implements KvStoreObserver { 
  29.  
  30.     @Override 
  31.     public void onChange(ChangeNotification changeNotification) { 
  32.       //刷新頁面上的數(shù)據(jù),同樣有一個坑,onChange方法實質(zhì)上,在一個子線程里執(zhí)行 
  33.       getUITaskDispatcher().asyncDispatch(() -> { 
  34.         //在這里執(zhí)行頁面ui組件的顯示刷新 
  35.         flushUIData(); 
  36.       }); 
  37.     } 
  38.   } 
  39.  
  40.   private void flushUIData() { 
  41.     //查詢分布式數(shù)據(jù)的數(shù)據(jù) 
  42.     List entries = singleKvStore.getEntries("key"); 
  43.     if (entries.size() > 0) { 
  44.       ZSONObject zsonObject = ZSONObject.stringToZSON(entries.get(0).getValue().getString()); 
  45.       int temp = zsonObject.getIntValue("temp"); 
  46.       int humidity = zsonObject.getIntValue("humidity"); 
  47.       int other = zsonObject.getIntValue("other"); 
  48.       tvTemp.setText(temp+"℃"); 
  49.       tvHumi.setText(humidity+"% RH"); 
  50.       tvGas.setText(other+"% LEL"); 
  51.     } 
  52.  
  53.  
  54.  
  55.   } 
  56.  
  57.   private void initService() { 
  58.     //啟動ServiceAbility 
  59.      serviceIntent = new Intent(); 
  60.     Operation operation = new Intent.OperationBuilder() 
  61.         .withDeviceId("") 
  62.         .withBundleName("com.isoftstone.kvstoreapp") 
  63.         .withAbilityName("com.isoftstone.kvstoreapp.ServiceAbility") 
  64.         .build(); 
  65.     serviceIntent.setOperation(operation); 
  66.     startAbility(serviceIntent); 
  67.   } 
  68.  
  69.   @Override 
  70.   public void onActive() { 
  71.     super.onActive(); 
  72.   } 
  73.  
  74.   @Override 
  75.   public void onForeground(Intent intent) { 
  76.     super.onForeground(intent); 
  77.   } 
  78.  
  79.   @Override 
  80.   protected void onStop() { 
  81.     super.onStop(); 
  82.     //銷毀service 
  83.    stopAbility(serviceIntent); 
  84.    //刪除數(shù)據(jù)庫 
  85.    DBUtils.clearDB(); 
  86.     //解除訂閱 
  87.     if (singleKvStore != null) { 
  88.       singleKvStore.unSubscribe(innerKvStoreObserver); 
  89.     } 
  90.   } 

2.ServiceAbility

  
 
 
 
  1. public class ServiceAbility extends Ability { 
  2.  
  3.   private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo"); 
  4.   private SingleKvStore singleKvStore; 
  5.   private Timer timer; 
  6.   private MyTimerTask myTimerTask; 
  7.   private int temperature; 
  8.   private int humidity; 
  9.   private int gas; 
  10.  
  11.   @Override 
  12.   public void onStart(Intent intent) { 
  13.     super.onStart(intent); 
  14.     singleKvStore = DBUtils.initOrGetDB(this, DBUtils.STORE_ID); 
  15.     timer=new Timer(); 
  16.     myTimerTask=new MyTimerTask(); 
  17.     timer.schedule(myTimerTask,0,3000); 
  18.  
  19.   } 
  20.  
  21.   @Override 
  22.   public void onBackground() { 
  23.     super.onBackground(); 
  24.     HiLog.info(LABEL_LOG, "ServiceAbility::onBackground"); 
  25.   } 
  26.  
  27.   @Override 
  28.   public void onStop() { 
  29.     super.onStop(); 
  30.     if(myTimerTask!=null){ 
  31.       myTimerTask.cancel(); 
  32.     } 
  33.     if(timer!=null){ 
  34.       timer.cancel(); 
  35.     } 
  36.   } 
  37.  
  38.   @Override 
  39.   public void onCommand(Intent intent, boolean restart, int startId) { 
  40.   } 
  41.  
  42.   @Override 
  43.   public IRemoteObject onConnect(Intent intent) { 
  44.     return null; 
  45.   } 
  46.  
  47.   @Override 
  48.   public void onDisconnect(Intent intent) { 
  49.   } 
  50.  
  51.   private class MyTimerTask extends TimerTask{ 
  52.  
  53.     @Override 
  54.     public void run() { 
  55.       temperature++; 
  56.       humidity++; 
  57.       gas++; 
  58.       try { 
  59.         long id = System.currentTimeMillis(); 
  60.         singleKvStore.putString("key", 
  61.             "{\"id\":" + id + 
  62.                 ",\"temp\":" + temperature + 
  63.                 ",\"humidity\":" + humidity + 
  64.                 ",\"NH4\":" + 0.0 + 
  65.                 ",\"H2S\":" + 0.0 + 
  66.                 ",\"other\":" + gas + "}"); 
  67.       } catch (KvStoreException e) { 
  68.         e.printStackTrace(); 
  69.       } 
  70.  
  71.     } 
  72.   } 

3.DBUtils

  
 
 
 
  1. public class DBUtils { 
  2.   //分布式數(shù)據(jù)庫storeId 
  3.   public static final String STORE_ID="kvStoreDB"; 
  4.   private static KvManager kvManager; 
  5.   private static SingleKvStore singleKvStore; 
  6.  
  7.  
  8.   //具體的實現(xiàn)數(shù)據(jù)庫的初始化 
  9.   public static SingleKvStore initOrGetDB(Context context, String storeId) { 
  10.  
  11.     KvManagerConfig kvManagerConfig = new KvManagerConfig(context); 
  12.     kvManager = KvManagerFactory.getInstance().createKvManager(kvManagerConfig); 
  13.     Options options = new Options(); 
  14.     options.setCreateIfMissing(true) 
  15.         .setEncrypt(false) 
  16.         .setKvStoreType(KvStoreType.SINGLE_VERSION) 
  17.         .setAutoSync(true);//設(shè)置數(shù)據(jù)為自動同步 
  18.     singleKvStore = kvManager.getKvStore(options, storeId); 
  19.     return singleKvStore; 
  20.   } 
  21.  
  22.   // 如果數(shù)據(jù)庫中的字段有修改,只能先關(guān)閉,后刪除,然后重新創(chuàng)建才生效 
  23.   public static void clearDB() { 
  24.     kvManager.closeKvStore(singleKvStore); 
  25.     kvManager.deleteKvStore(STORE_ID); 
  26.   } 
  27.  
  28.  

4. MainAbility

  
 
 
 
  1. public class MainAbility extends Ability { 
  2.  
  3.   @Override 
  4.   public void onStart(Intent intent) { 
  5.     super.onStart(intent); 
  6.     super.setMainRoute(MainAbilitySlice.class.getName()); 
  7.     //實現(xiàn)Ability的代碼中顯式聲明需要使用多設(shè)備協(xié)同訪問的權(quán)限 
  8.     requestPermissionsFromUser(new String[]{ 
  9.         "ohos.permission.DISTRIBUTED_DATASYNC"}, 0); 
  10.   } 

5. MyApplication

  
 
 
 
  1. public class MyApplication extends AbilityPackage { 
  2.  
  3.   @Override 
  4.   public void onInitialize() { 
  5.     super.onInitialize(); 
  6.   } 

6. config.json 文件

  
 
 
 
  1.   "app": { 
  2.     "bundleName": "com.isoftstone.healthdata", 
  3.     "vendor": "isoftstone", 
  4.     "version": { 
  5.       "code": 1000000, 
  6.       "name": "1.0" 
  7.     }, 
  8.     "apiVersion": { 
  9.       "compatible": 4, 
  10.       "target": 5, 
  11.       "releaseType": "Release" 
  12.     } 
  13.   }, 
  14.   "deviceConfig": {}, 
  15.   "module": { 
  16.     "package": "com.isoftstone.kvstoreapp", 
  17.     "name": ".MyApplication", 
  18.     "deviceType": [ 
  19.       "phone" 
  20.     ], 
  21.     "distro": { 
  22.       "deliveryWithInstall": true, 
  23.       "moduleName": "entry", 
  24.       "moduleType": "entry" 
  25.     }, 
  26.     "reqPermissions": [ 
  27.       { 
  28.         "name": "ohos.permission.DISTRIBUTED_DATASYNC" 
  29.       } 
  30.     ], 
  31.     "abilities": [ 
  32.       { 
  33.         "skills": [ 
  34.           { 
  35.             "entities": [ 
  36.               "entity.system.home" 
  37.             ], 
  38.             "actions": [ 
  39.               "action.system.home" 
  40.             ] 
  41.           } 
  42.         ], 
  43.         "orientation": "unspecified", 
  44.         "name": "com.isoftstone.kvstoreapp.MainAbility", 
  45.         "icon": "$media:icon", 
  46.         "description": "$string:mainability_description", 
  47.         "label": "$string:app_name", 
  48.         "type": "page", 
  49.         "launchType": "standard" 
  50.       }, 
  51.       { 
  52.         "name": "com.isoftstone.kvstoreapp.ServiceAbility", 
  53.         "icon": "$media:icon", 
  54.         "description": "$string:serviceability_description", 
  55.         "type": "service" 
  56.       } 
  57.     ] 
  58.   } 

7.xml布局文件

  
 
 
 
  1.  
  2.   xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.   ohos:height="match_parent" 
  4.   ohos:orientation="vertical" 
  5.   ohos:width="match_parent"> 
  6.  
  7.   ohos:padding="20vp" 
  8.   ohos:height="match_content" 
  9.   ohos:width="match_parent" 
  10.   ohos:orientation="horizontal"> 
  11.   
  12.     ohos:width="match_content" 
  13.     ohos:height="match_content" 
  14.     ohos:text_size="20vp" 
  15.     ohos:text="溫度:"/> 
  16.   
  17.     ohos:id="$+id:tvTemp" 
  18.     ohos:width="0" 
  19.     ohos:height="match_content" 
  20.     ohos:text_size="22vp" 
  21.     ohos:text_color="#00ff00" 
  22.     ohos:text="待采集..." 
  23.     ohos:weight="1"/> 
  24.  
  25.   
  26.     ohos:height="1vp" 
  27.     ohos:width="match_parent" 
  28.     ohos:background_element="#cccccc"/> 
  29.  
  30.   
  31.     ohos:padding="20vp" 
  32.     ohos:height="match_content" 
  33.     ohos:width="match_parent" 
  34.     ohos:orientation="horizontal"> 
  35.     
  36.       ohos:width="match_content" 
  37.       ohos:height="match_content" 
  38.       ohos:text_size="20vp" 
  39.       ohos:text="濕度:"/> 
  40.     
  41.       ohos:id="$+id:tvHumi" 
  42.       ohos:width="0" 
  43.       ohos:height="match_content" 
  44.       ohos:text_size="22vp" 
  45.       ohos:text_color="#00ff00" 
  46.       ohos:text="待采集..." 
  47.       ohos:weight="1"/> 
  48.    
  49.   
  50.     ohos:height="1vp" 
  51.     ohos:width="match_parent" 
  52.     ohos:background_element="#cccccc"/> 
  53.  
  54.  
  55.   
  56.     ohos:padding="20vp" 
  57.     ohos:height="match_content" 
  58.     ohos:width="match_parent" 
  59.     ohos:orientation="horizontal"> 
  60.     
  61.       ohos:width="match_content" 
  62.       ohos:height="match_content" 
  63.       ohos:text_size="20vp" 
  64.       ohos:text="可燃?xì)怏w:"/> 
  65.     
  66.       ohos:id="$+id:tvGas" 
  67.       ohos:width="0" 
  68.       ohos:height="match_content" 
  69.       ohos:text_size="22vp" 
  70.       ohos:text_color="#00ff00" 
  71.       ohos:text="待采集..." 
  72.       ohos:weight="1"/> 
  73.    
  74.   
  75.     ohos:height="1vp" 
  76.     ohos:width="match_parent" 
  77.     ohos:background_element="#cccccc"/> 
  78.  
  79.  

文章題目:HarmonyOS基礎(chǔ)技術(shù)賦能之分布式數(shù)據(jù)服務(wù)功能
URL鏈接:http://www.dlmjj.cn/article/ccdiseh.html