This topic describes how to query ApsaraDB for MongoDB data by using external tables of AnalyticDB for MySQL Data Lakehouse Edition (V3.0) and how to import data from ApsaraDB for MongoDB to AnalyticDB for MySQL Data Lakehouse Edition (V3.0).
Prerequisites
- An AnalyticDB for MySQL Data Lakehouse Edition (V3.0) cluster is created in the same virtual private cloud (VPC) as the ApsaraDB for MongoDB instance. For more information, see Create a Data Lakehouse Edition (V3.0) cluster.
- ENI is turned on for the AnalyticDB for MySQL cluster in the Network Information section of the Cluster Information page in the console.
- The VPC CIDR block of the AnalyticDB for MySQL Data Lakehouse Edition (V3.0) cluster is added to a whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist or an ECS security group for an ApsaraDB for MongoDB instance.
Import data from non-nested documents of ApsaraDB for MongoDB
Sample data
In this example, a collection named
person
is created in the test_mongodb
database of the ApsaraDB for MongoDB instance.use test_mongodb;
db.createCollection("person");
Documents are inserted into the
person
collection.db.person.insert({"id":1,"name":"james","age":10});
db.person.insert({"id":2,"name":"bond","age":20});
db.person.insert({"id":3,"name":"jack","age":30});
db.person.insert({"id":4,"name":"lock","age":40});
Procedure
- Go to the SQL editor.
- Log on to the AnalyticDB for MySQL console.
- In the upper-left corner of the page, select the region where the cluster resides.
- In the left-side navigation pane, click Clusters.
- On the Data Lakehouse Edition (V3.0) tab, find the cluster and click the Cluster ID.
- In the left-side navigation pane, choose .
- Execute the following statement to create an external database:
CREATE EXTERNAL DATABASE adb_external_db;
- Create an external table. Important The AnalyticDB for MySQL external table must have the same names, quantity, order, and data types of fields as the ApsaraDB for MongoDB collection.
CREATE TABLE adb_external_db.person ( id int, name string, age int ) ENGINE = 'MONGODB' TABLE_PROPERTIES ='{ "mapped_name":"person", "location":"mongodb://testuser:****@dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717,dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717/test_mongodb", "username":"testuser", "password":"password", }';
Table 1. Parameters Parameter Description mapped_name The name of the ApsaraDB for MongoDB collection. In this example, person
is used.location The VPC endpoint that is used to connect to the ApsaraDB for MongoDB instance. For more information about how to obtain the VPC endpoint, see Overview of instance connections. Format:
mongodb://testuser:****@dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717,dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717/database
.Example:
mongodb://testuser:****@dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717,dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717/test_mongodb
.username The name of the database account that is used to connect to the ApsaraDB for MongoDB instance. For more information about how to create a database account, see Manage user permissions on MongoDB databases. Note ApsaraDB for MongoDB verifies the specified database account and the password. You must use the database account that is contained in the VPC endpoint of the ApsaraDB for MongoDB instance. If you have any questions, contact technical support.password The password of the database account. - Query data. After the external table is created, you can execute the SELECT statement to query data of the
person
collection.
The following information is returned:SELECT * FROM adb_external_db.person;
+------+-------+------+ | id | name | age | +------+-------+------+ | 1 | james | 10 | | 2 | bond | 20 | | 3 | jack | 30 | | 4 | lock | 40 | +------+-------+------+ 4 rows in set (0.35 sec)
- Create a database and a table in the AnalyticDB for MySQL Data Lakehouse Edition (V3.0) cluster to store data that is imported from the ApsaraDB for MongoDB instance.
- Import data from the ApsaraDB for MongoDB instance to the AnalyticDB for MySQL Data Lakehouse Edition (V3.0) cluster.
- Method 1: Execute the
INSERT INTO
statement to import data. If the primary key has duplicate values, data is not repeatedly inserted, and the INSERT INTO statement is equivalent toINSERT IGNORE INTO
. For more information, see INSERT INTO.INSERT INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.person;
- Method 2: Execute the
INSERT OVERWRITE INTO
statement to synchronously import data. If the primary key has duplicate values, the original value is overwritten by the new value.INSERT OVERWRITE INTO adb_demo.adb_import_test SELECT * FROM adb_external_db.person;
- Method 3: Execute the
INSERT OVERWRITE INTO
statement to asynchronously import data. For more information, see Asynchronous writing.SUBMIT JOB INSERT OVERWRITE adb_demo.adb_import_test SELECT * FROM adb_external_db.person;
- Method 1: Execute the
- After data is imported, execute the SELECT statement to query data of the
adb_demo.adb_import_test
table.
The following information is returned:SELECT * FROM adb_demo.adb_import_test;
+------+-------+------+ | id | name | age | +------+-------+------+ | 1 | james | 10 | | 2 | bond | 20 | | 3 | jack | 30 | | 4 | lock | 40 | +------+-------+------+
Query data in nested documents of ApsaraDB for MongoDB
Sample data
In this example, a collection named
test_json
is created in the test_mongodb
database of the ApsaraDB for MongoDB instance.db.createCollection("test_json");
A document is inserted into the
test_json
collection. The city
and name
fields are nested fields.db.test_json.insert( {
'id': 1,
'details':{'city': "hangzhou", "name":"jack"}
})
Procedure
- Go to the SQL editor.
- Log on to the AnalyticDB for MySQL console.
- In the upper-left corner of the page, select the region where the cluster resides.
- In the left-side navigation pane, click Clusters.
- On the Data Lakehouse Edition (V3.0) tab, find the cluster and click the Cluster ID.
- In the left-side navigation pane, choose .
- Execute the following statement to create an external database:
CREATE EXTERNAL DATABASE adb_external_db;
- Create an external table. Important
- The AnalyticDB for MySQL external table must have the same names, quantity, order, and data types of fields as the ApsaraDB for MongoDB collection.
- You cannot use external tables to import data to nested documents of ApsaraDB for MongoDB.
Parameters:CREATE TABLE adb_external_db.test_json ( id int, city string, name string ) ENGINE = 'MONGODB' TABLE_PROPERTIES ='{ "mapped_name":"test_json", "location":"mongodb://testuser:****@dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717,dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717/test_mongodb", "username":"testuser", "password":"password", "COLUMN_MAPPING":"city,details.city;name,details.name", }';
- COLUMN_MAPPING: defines the mappings between external table fields and ApsaraDB for MongoDB document fields. For example, a mapping between the
city
field of the external table and thedetails.city
field of the ApsaraDB for MongoDB document is created. - For information about other parameters that are used to create an external table, see the "Parameters" section of this topic.
- Query data. After the external table is created, you can execute the SELECT statement to query data of the
test_json
collection.
The following information is returned:SELECT * FROM adb_external_db.test_json;
+------+----------+-------+ | id | city | name | +------+----------+-------+ | 1 | hangzhou | jack | +------+----------+-------+
Note To import data from nested documents of ApsaraDB for MongoDB to AnalyticDB for MySQL, you must create a database and a table first. For more information, see Steps 5 to 7 in the "Import data from non-nested documents of ApsaraDB for MongoDB" section of this topic.
Query data of ObjectId fields
Sample data
In this example, a collection named
A document is inserted into the test_objectid
is created in the test_mongodb
database of the ApsaraDB for MongoDB instance.db.createCollection("test_objectid");
test_objectid
collection.db.test_objectid.insert( {
'id': 1,
})
Data of the test_objectid
collection is queried.db.test_objectid.find()
The following information is returned:{
"_id":"ObjectId("641002ad883a73eb0d7291a7")"
"id":1
}
Procedure
- Go to the SQL editor.
- Log on to the AnalyticDB for MySQL console.
- In the upper-left corner of the page, select the region where the cluster resides.
- In the left-side navigation pane, click Clusters.
- On the Data Lakehouse Edition (V3.0) tab, find the cluster and click the Cluster ID.
- In the left-side navigation pane, choose .
- Execute the following statement to create an external database:
CREATE EXTERNAL DATABASE adb_external_db;
- Create an external table. Important The AnalyticDB for MySQL external table must have the same names, quantity, order, and data types of fields as the ApsaraDB for MongoDB collection.
CREATE TABLE adb_external_db.test_objectid ( id int, _id objectid ) ENGINE = 'MONGODB' TABLE_PROPERTIES ='{ "mapped_name":"test_objectid", "location":"mongodb://testuser:****@dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717,dds-bp113d414bca8****.mongodb.rds.aliyuncs.com:3717/test_mongodb", "username":"testuser", "password":"password", }';
- Query data. After the external table is created, you can execute the SELECT statement to query data of the
test_objectid
collection.
The following information is returned:SELECT cast(_id as string) FROM adb_external_db.test_objectid;
+----------------------------+ | CAST(_id AS string) | +----------------------------+ | 641002ad883a73eb0d7291a7 | +----------------------------+
Note To import data from ObjectId fields to AnalyticDB for MySQL, you must create a database and a table first. For more information, see Steps 5 to 7 in the "Import data from non-nested documents of ApsaraDB for MongoDB" section of this topic.
Data types supported by external tables
Data type supported by AnalyticDB for MySQL external tables | Data type supported by ApsaraDB for MongoDB collections |
---|---|
Boolean | Boolean |
ObjectId | ObjectId |
String | String |
Int | 32-bit Integer and Int |
Bigint | 64-bit Integer Long |
Double | Double |
Date | Date |