1 -> Development process
ApsaraDB is a device-cloud collaborative database product that provides collaborative management of device-cloud data, a unified data model, and a variety of data management APIs. A cloud database uses an object-based data storage structure. Data is stored in different storage areas in the form of objects, and each object is a complete data record. ObjectType is used to define the collection of stored objects, and different object types correspond to different data structures. A zone is a separate data storage area, each with the exact same object type definition.
You can use DevEco Studio to develop a cloud database under the device-cloud integration cloud-side project, and the overall process is as follows:
Create Object Type: Create an object type that stores data entries.
Add data entries: Add data items to the object type you just created, and configure the storage area where the data is located.
Deploy the cloud database: After the data is successfully added, you can directly deploy the data to the AGC cloud. You can also wait for all object types and data items to be developed before deploying them to the AGC cloud in batches.
2 -> Create an object type
ObjectType is used to define the collection of stored objects, and different object types correspond to different data structures. Each time an object type is created, ApsaraDB instantiates an object type corresponding to its structure in each bucket to store the corresponding data.
To create an object type, perform the following steps:
- Right-click the clouddb/objecttype directory and select New > Cloud DB Object Type.
- Enter the object type name (objecttype1 is used as an example) and click OK.
illustrate
The object type name must meet the following specifications:
It can contain only letters (A-Z or a-z), numbers (0-9), and underscores (), and at least the letter type.
Must start with a letter, end with a letter or number, not with "sqlite", not with an underscore ().
The following system reserved names are not allowed: naturalbase_metadata, ObjectTypeInfoHelper, t_data_upgrade_info, t_index_schema, t_nstore_config, t_schema_negotiate_info, t_metadata_schema, t_nstore permission、t_system_config。
In the clouddb/objecttype directory, generate and open the JSON file objecttype1.json.
For example, we can configure the following fields for the "objecttype1" object type.
- Configure the index, the fields contained in the index, and the sorting method of the fields contained in the index for the object type in Indexes.
For example, we can configure the following two indexes for the "objecttype1" object type.
- In permissions, set whether each role has the Read, Upsert (including adding and modifying), and Delete permissions of the object type.
illustrate
Each role can only perform operations with the corresponding permissions, and the cloud side of operations that exceed the scope of the permissions will return a "permission denied" error. Since the initialization code of the device-cloud integration project does not have an AccessToken configured, the Upsert and Delete permissions are added to the World role in CloudProgram/clouddb/objecttype/Post.json.
For example, we can configure the permissions for the object type "objecttype1" for each role in the following table.
3 -> Add data entries
After you create an object type, you can add a DataEntry to the object type and configure the storage area where the data is located.
Supports manual creation and automatic generation of data entry files.
3.1 -> Manually create a data entry file
- Right-click the clouddb/dataentry directory and select New > Cloud DB Data Entry.
- Select the object type to which you want to add data entries in the Associated Cloud DB Object Type column, define the file name of the data entry in the Enter Cloud DB Data Entry Name column, and click OK.
For example, if you select objecttype1, the default name of the data entry file is d_objecttype1.
As shown in the following figure, the new data entry JSON file "d_objecttype1" is generated and opened in the clouddb/dataentry directory, which presets the name of the object type (objecttype1) and the field name of the object type (id, bookName, author, price, publishTime, and shadowFlag) in the clouddb/dataentry directory.
- Configure the values (i.e., data) of the store and fields.
cloudDBZoneName: indicates the name of the storage area. In the preceding example, default indicates that data entries are added to the default bucket. You can modify it, as shown in the following figure: cloudDBZoneName1. In addition, this field needs to be referenced when accessing cloud database coding using APIs.
objects: Sets the values of all fields in the current object type, that is, writes data. An object is a piece of data, and you can create an object to assign a new value to the field, or modify the value of a field under an object (the value of the primary key or encrypted field cannot be modified). As shown in the figure below, two pieces of data are written.
3.2 -> Automatically generate data entry files
- Right-click the object type JSON file and choose Generate Data Entry.
For example, objecttype1 contains six fields: id, bookName, author, price, publishTime, and shadowFlag.
- In the New Cloud DB Data Entry dialog box, define the name of the data entry file to be generated. The default value of d_objecttype1 is used as an example.
As shown in the following figure, the clouddb/dataentry directory automatically generates a data entry file "d_objecttype1" for the object type "objecttype1", which has preset the object type name ("objecttype1") and the field name of the object type ("id", "bookName", "author", "price", "publishTime", shadowFlag”)。
- Configure the values (i.e., data) of the store and fields.
cloudDBZoneName: indicates the name of the storage area. In the preceding example, default indicates that data entries are added to the default bucket. You can modify it, as shown in the following figure: cloudDBZoneName1. In addition, this field needs to be referenced when accessing cloud database coding using APIs.
objects: Sets the values of all fields in the current object type, that is, writes data. An object is a piece of data, and you can create an object to assign a new value to the field, or modify the value of a field under an object (the value of the primary key or encrypted field cannot be modified). As shown in the figure below, two pieces of data are written.
4 -> Deploy a cloud database
After a data entry is created, you can deploy it directly. You can also wait for all object types and data items to be developed before deploying them to the AGC cloud in batches.
illustrate
The number of storage areas deployed to AGC Cloud must not exceed 4, otherwise the deployment will fail and the message "clouddb deploy failed. Reason is the number of CloudDBZone exceeds the limit.” Mistake. If there are currently 4 storage areas in the AGC Cloud, please deploy the data to the existing storage area, or delete the existing storage area and deploy a new storage area. It should be noted that if you delete a storage area, the data in the storage area will also be deleted and cannot be recovered.
Do not modify fields such as fieldType in the object type locally after they are deployed to the AGC cloud. For example, if fieldType is set to String, and the objectType is successfully deployed, and the fieldType is changed to Integer locally, the deployment will fail again, and the message "clouddb deploy failed. Reason is existing fields cannot be modified.” Mistake. If you want to change fields such as fieldType, delete the object type deployed in the cloud. It should be noted that if you delete an object type in the cloud, the data added to the object type will also be deleted and cannot be recovered.
To deploy a cloud database, perform the following steps:
- Right-click the clouddb directory and select Deploy Cloud DB.
- You can view the progress of cloud database packaging and deployment on the right side of the status bar at the bottom.
Wait patiently until the "Deploy successfully" message appears, indicating that the cloud database has been successfully deployed.
illustrate
After the ApsaraDB is deployed, DevEco Studio automatically downloads the schema file of the ApsaraDB for the AppScope/resources/rawfile/schema.json path, which is a configuration file that must be introduced into the ApsaraDB for the device.
- Select Tools > CloudDev in the menu bar.
- In the CloudDev panel that opens, click Go to console under Serverless > Cloud DB to enter the cloud database service page of the current project.
- Click the Object Type, Storage Area, and Data tabs respectively to view that the locally developed cloud database resources have been successfully deployed to the AGC cloud.
After the deployment is successful, you can access the cloud database from the device side.
You can also edit the deployed cloud database resources in the AGC console.
Demo and cloudDBZoneName1 are stored in the storage area to which object type Post and objecttype1 belong.
Data entries in "d_Post.json", data entries in "d_objecttype1.json":
illustrate
A JSON file of an object type or data entry is actually an object type or data entry contained in a JSON file. As a result, the AGC console will see object types or pieces of data, not JSON files.
Top comments (0)