Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
7c80800d
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7c80800d
编写于
6月 21, 2023
作者:
D
dboy190
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update udmf data channel guide
Signed-off-by:
N
dboy190
<
dulei1@huawei.com
>
上级
c42bbfd6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
114 addition
and
79 deletion
+114
-79
zh-cn/application-dev/database/unified-data-channels.md
zh-cn/application-dev/database/unified-data-channels.md
+95
-69
zh-cn/application-dev/database/unified-data-definition.md
zh-cn/application-dev/database/unified-data-definition.md
+19
-10
未找到文件。
zh-cn/application-dev/database/unified-data-channels.md
浏览文件 @
7c80800d
...
...
@@ -3,102 +3,128 @@
## 场景介绍
为了构建OpenHarmony数据跨应用/设备交互的标准定义,降低应用/业务数据交互成本,促进数据生态建设,UDMF提供了标准化的数据定义,统一定义了多种常用的数据类型。应用可以使用统一数据管理框架提供的接口创建和使用这些标准化数据类型
。
UDMF定义了安全、标准化的数据接入与读取通路,为各种业务场景(如跨应用跨设备数据拖拽)提供了跨应用、跨设备的数据接入与读取通路,降低业务跨应用、跨设备数据交互的成本
。
## 接口说明
以下是
键值型数据库持久化功能的相关接口,大部分为异步接口。异步接口均有callback和Promise两种返回形式,下表均以callback形式为例,更多接口及使用方式请见
[
分布式键值数据库
](
../reference/apis/js-apis-distributedKVStore
.md
)
。
以下是
UDMF标准化数据通路的相关接口,均为异步接口。异步接口均有callback和Promise两种返回形式,下表均以callback形式为例,更多接口及使用方式请见
[
统一数据管理框架
](
../reference/apis/js-apis-data-udmf
.md
)
。
| 接口名称 | 描述 |
| -------- | -------- |
| createKVManager(config: KVManagerConfig): KVManager | 创建一个KVManager对象实例,用于管理数据库对象。 |
| getKVStore
<
T
>
(storeId: string, options: Options, callback: AsyncCallback
<
T
>
): void | 指定Options和storeId,创建并得到指定类型的KVStore数据库。 |
| put(key: string, value: Uint8Array
\|
string
\|
number
\|
boolean, callback: AsyncCallback
<
void
>
): void | 添加指定类型的键值对到数据库。 |
| get(key: string, callback: AsyncCallback
<
Uint8Array\|string\|boolean\|number
>
): void | 获取指定键的值。 |
| delete(key: string, callback: AsyncCallback
<
void
>
): void | 从数据库中删除指定键值的数据。 |
| 接口名称 | 描述 |
|----------------------------------------------------------------------------------------|---------------------------------------------|
| insertData(options: Options, data: UnifiedData, callback: AsyncCallback
<string>
): void | 将数据写入UDMF的公共存储中,并生成数据的唯一标识符,使用callback异步回调。 |
| updateData(options: Options, data: UnifiedData, callback: AsyncCallback
<void>
): void | 更新已写入UDMF的公共存储的数据,使用callback异步回调。 |
| queryData(options: Options, callback: AsyncCallback
<Array
<
UnifiedData
>
>): void | 查询UDMF公共存储的数据,使用callback异步回调。 |
| deleteData(options: Options, callback: AsyncCallback
<Array
<
UnifiedData
>
>): void | 删除UDMF公共存储的数据,返回删除的数据集,使用callback异步回调。 |
## 开发步骤
以一次
创建数据(包含图片、纯文本、二进制图片三条数据记录)为例,说明开发步骤
。
以一次
统一数据对象的插入、更新、查询和删除为例,说明开发步骤,示例代码均采用Callback形式,Promise形式请见
。
1.
导入
`@ohos.data.UDMF`
模块
```
js
import
UDMF
from
'
@ohos.data.UDMF
'
;
```
2.
创建图片数据记录,并初始化得到带有该数据记录的UnifiedData对象
(1)创建图片数据记录
```
js
let
image
=
new
UDMF
.
Image
();
```
(2)修改对象属性
```
js
// Image对象包含一个属性imageUri
image
.
imageUri
=
'
...
'
;
```
(3)访问对象属性
2.
创建一个统一数据对象并插入到UDMF的公共存储中。
```
js
console
.
info
(
`imageUri =
${
image
.
imageUri
}
`
);
```
(4)创建一个统一数据对象实例
```
js
let
unifiedData
=
new
UDMF
.
UnifiedData
(
image
);
let
plainText
=
new
UDMF
.
PlainText
();
plainText
.
textContent
=
'
hello world!
'
;
let
unifiedData
=
new
UDMF
.
UnifiedData
(
plainText
);
let
options
=
{
intention
:
UDMF
.
Intention
.
SUPER_HUB
}
try
{
UDMF
.
insertData
(
options
,
unifiedData
,
(
err
,
data
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
`Succeeded in inserting data. key =
${
data
}
`
);
}
else
{
console
.
error
(
`Failed to insert data. code is
${
err
.
code
}
,message is
${
err
.
message
}
`
);
}
});
}
catch
(
e
)
{
console
.
error
(
`Insert data throws an exception. code is
${
e
.
code
}
,message is
${
e
.
message
}
`
);
}
```
3.
创建纯文本数据类型记录,将其添加到刚才创建的UnifiedData对象
3.
更新上一步骤插入的统一数据对象。
```
js
let
plainText
=
new
UDMF
.
PlainText
();
plainText
.
textContent
=
'
this is textContent of plainText
'
;
plainText
.
abstract
=
'
abstract of plainText
'
;
plainText
.
details
=
{
plainKey1
:
'
plainValue1
'
,
plainKey2
:
'
plainValue2
'
,
plainText
.
textContent
=
'
hello world!
'
;
let
unifiedData
=
new
UDMF
.
UnifiedData
(
plainText
)
;
let
options
=
{
key
:
'
udmf://SuperHub/com.ohos.test/0123456789
'
};
unifiedData
.
addRecord
(
plainText
);
```
4.
创建二进制图片数据类型记录,将其添加到UnifiedData对象
```
js
let
sdPixelMap
=
new
UDMF
.
SystemDefinedPixelMap
();
sdPixelMap
.
rawData
=
new
Uint8Array
([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]);
unifiedData
.
addRecord
(
sdPixelMap
);
try
{
UDMF
.
updateData
(
options
,
unifiedData
,
(
err
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
'
Succeeded in updating data.
'
);
}
else
{
console
.
error
(
'
Failed to update data. code is ${err.code},message is ${err.message} `);
}
});
} catch(e) {
console.error(`Update data throws an exception. code is ${e.code},message is ${e.message} `);
}
```
5.
记录添加完成后,可获取当前UnifiedData对象内的所有数据记录
4.
查询存储在UDMF公共存储中的统一数据对象。
```
js
let
records
=
unifiedData
.
getRecords
();
let
options
=
{
intention
:
UDMF
.
Intention
.
SUPER_HUB
};
try
{
UDMF
.
queryData
(
options
,
(
err
,
data
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
`Succeeded in querying data. size =
${
data
.
length
}
`
);
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
let
records
=
data
[
i
].
getRecords
();
for
(
let
j
=
0
;
j
<
records
.
length
;
j
++
)
{
if
(
records
[
j
].
getType
()
===
UDMF
.
UnifiedDataType
.
PLAIN_TEXT
)
{
let
text
=
<
UDMF
.
PlainText
>
(
records
[
j
]);
console
.
info
(
`
${
i
+
1
}
.
${
text
.
textContent
}
`
);
}
}
}
}
else
{
console
.
error
(
`Failed to query data. code is
${
err
.
code
}
,message is
${
err
.
message
}
`
);
}
});
}
catch
(
e
)
{
console
.
error
(
`Query data throws an exception. code is
${
e
.
code
}
,message is
${
e
.
message
}
`
);
}
```
6.
遍历每条记录,判断该记录的数据类型,转换为子类对象,得到原数据记录
5.
删除存储在UDMF公共存储中的统一数据对象。
```
js
for
(
let
i
=
0
;
i
<
records
.
length
;
i
++
)
{
// 读取该数据记录的类型
let
type
=
records
[
i
].
getType
();
switch
(
type
)
{
case
UDMF
.
UnifiedDataType
.
IMAGE
:
// 转换得到原图片数据记录
let
image
=
<
UDMF
.
Image
>
(
records
[
i
]);
break
;
case
UDMF
.
UnifiedDataType
.
PLAIN_TEXT
:
// 转换得到原文本数据记录
let
plainText
=
<
UDMF
.
PlainText
>
(
records
[
i
]);
break
;
case
UDMF
.
UnifiedDataType
.
SYSTEM_DEFINED_PIXEL_MAP
:
// 转换得到原二进制图片数据记录
let
sdPixelMap
=
<
UDMF
.
SystemDefinedPixelMap
>
(
records
[
i
]);
break
;
default
:
break
;
}
let
options
=
{
intention
:
UDMF
.
Intention
.
SUPER_HUB
};
try
{
UDMF
.
deleteData
(
options
,
(
err
,
data
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
`Succeeded in deleting data. size =
${
data
.
length
}
`
);
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
let
records
=
data
[
i
].
getRecords
();
for
(
let
j
=
0
;
j
<
records
.
length
;
j
++
)
{
if
(
records
[
j
].
getType
()
===
UDMF
.
UnifiedDataType
.
PLAIN_TEXT
)
{
let
text
=
<
UDMF
.
PlainText
>
(
records
[
j
]);
console
.
info
(
`
${
i
+
1
}
.
${
text
.
textContent
}
`
);
}
}
}
}
else
{
console
.
error
(
`Failed to delete data. code is
${
err
.
code
}
,message is
${
err
.
message
}
`
);
}
});
}
catch
(
e
)
{
console
.
error
(
`Delete data throws an exception. code is
${
e
.
code
}
,message is
${
e
.
message
}
`
);
}
```
zh-cn/application-dev/database/unified-data-definition.md
浏览文件 @
7c80800d
...
...
@@ -3,25 +3,34 @@
## 场景介绍
为了构建OpenHarmony数据跨应用
/
设备交互的标准定义,降低应用/业务数据交互成本,促进数据生态建设,UDMF提供了标准化的数据定义,统一定义了多种常用的数据类型。应用可以使用统一数据管理框架提供的接口创建和使用这些标准化数据类型。
为了构建OpenHarmony数据跨应用
、跨
设备交互的标准定义,降低应用/业务数据交互成本,促进数据生态建设,UDMF提供了标准化的数据定义,统一定义了多种常用的数据类型。应用可以使用统一数据管理框架提供的接口创建和使用这些标准化数据类型。
## 约束限制
-
UDMF中每条数据记录大小不超过2MB。
-
UDMF支持批量数据记录的分组管理,每个分组最多支持512条数据,整体大小不超过4MB。
-
UDMF支持批量数据记录的分组管理,每个分组整体大小不超过4MB。
## 接口说明
以下是键值型数据库持久化功能的相关接口,大部分为异步接口。异步接口均有callback和Promise两种返回形式,下表均以callback形式为例,更多接口及使用方式请见
[
分布式键值数据库
](
../reference/apis/js-apis-distributedKVStore.md
)
。
UDMF支持的标准化数据类型列表,请见接口参考中的
[
UnifiedDataType
](
../reference/apis/js-apis-data-udmf.md#UnifiedDataType
)
。
UDMF标准化数据类型均为数据记录
[
UnifiedRecord
](
../reference/apis/js-apis-data-udmf.md#UnifiedRecord
)
的子类,如下表所示,
通过基类的getType方法可以获取具体的数据类型。
| 接口名称 | 描述 |
|-------------------|-----------------------------------------------------------------------------------------------|
| getType(): string | 获取当前数据记录对应的具体数据类型,见
[
UnifiedDataType
](
../reference/apis/js-apis-data-udmf.md#UnifiedDataType
)
。 |
UDMF支持批量数据记录的分组管理,通过统一数据对象
[
UnifiedData
](
../reference/apis/js-apis-data-udmf.md#UnifiedData
)
封装一组数据记录,
UnifiedData的接口如下表所示。
| 接口名称 | 描述 |
| -------- | -------- |
| createKVManager(config: KVManagerConfig): KVManager | 创建一个KVManager对象实例,用于管理数据库对象。 |
| getKVStore
<
T
>
(storeId: string, options: Options, callback: AsyncCallback
<
T
>
): void | 指定Options和storeId,创建并得到指定类型的KVStore数据库。 |
| put(key: string, value: Uint8Array
\|
string
\|
number
\|
boolean, callback: AsyncCallback
<
void
>
): void | 添加指定类型的键值对到数据库。 |
| get(key: string, callback: AsyncCallback
<
Uint8Array\|string\|boolean\|number
>
): void | 获取指定键的值。 |
| delete(key: string, callback: AsyncCallback
<
void
>
): void | 从数据库中删除指定键值的数据。 |
| 接口名称 | 描述 |
|----------------------------------------|--------------------------------------------------------------------------|
| constructor(record: UnifiedRecord) | 用于创建带有一条数据记录的统一数据对象。 |
| addRecord(record: UnifiedRecord): void | 在当前统一数据对象中添加一条数据记录。 |
| getRecords(): Array
<UnifiedRecord>
| 将当前统一数据对象中的所有数据记录取出,通过本接口取出的数据为UnifiedRecord类型,需通过getType获取数据类型后转为子类再使用。 |
## 开发步骤
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录