Migrate JSON from MongoDB to MaxCompute

更新时间:
复制 MD 格式

This topic describes how to use the Data Integration feature of DataWorks to migrate JSON fields from MongoDB to MaxCompute.

Prerequisites

Prepare test data in MongoDB

  1. Prepare a user account.

    Create a user in the database. You can use this user to add a data source in DataWorks. In this example, run the following command.

    db.createUser({user:"bookuser",pwd:"123456",roles:["user1"]})

    This command creates a user named bookuser with the password 123456. The role can be any role that has permissions to access the data.

  2. Prepare data.

    Upload data to the MongoDB database. This example uses ApsaraDB for MongoDB. The network type is VPC. A public endpoint is required for the VPC to communicate with the DataWorks public resource group. The following code provides the test data.

    {
        "store": {
            "book": [
                {
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": 8.95
                    },
                {
                    "category": "fiction",
                    "author": "Evelyn Waugh",
                    "title": "Sword of Honour",
                    "price": 12.99
                    },
                {
                    "category": "fiction",
                    "author": "J. R. R. Tolkien",
                    "title": "The Lord of the Rings",
                    "isbn": "0-395-19395-8",
                    "price": 22.99
                    }
                        ],
            "bicycle": {
                "color": "red",
                "price": 19.95
                    }
                        },
            "expensive": 10
                }
  3. This example uses the admin database and the userlog collection in the DMS console for MongoDB. To view the uploaded data, run the following command.

    db.userlog.find().limit(10)

Use DataWorks to migrate data

  1. Login DataWorks console.

  2. Create a destination table in DataWorks for the migrated MongoDB data.

    1. Right-click a created Workflow, Select Create Table > Table

    2. In Create Table page, select the engine type, and enter Name.

    3. On the table editing page, click DDL Statement.

    4. In the DDL Statement dialog box, enter the DDL statement and click Generate Table Schema.

      Important

      The table name in the DDL statement must match the Table Name you entered on the Create Table page.

      create table mqdata (mqdata string);
    5. Click Commit to Production Environment.

  3. Add a MongoDB data source. For more information, see Configure a MongoDB data source.

  4. Create an offline synchronization node.

    1. Go to the data analytics page. Right-click the specified workflow and choose Create Node > Data Integration > Offline synchronization.

    2. In Create Node dialog box, enter Name, and click Confirm.

    3. In the top navigation bar, choose Conversion scripticon.

    4. In script mode, click **icon.

    5. In import Template dialog box SOURCE type, data source, target type and data source, and click confirm.

    6. Enter the following script.

      {
          "type": "job",
          "steps": [
          {
              "stepType": "mongodb",
              "parameter": {
                  "datasource": "mongodb_userlog",
                  "column": [
                      {
                      "name": "store.bicycle.color", 
                      "type": "document.String" 
                      }
                    ],
                  "collectionName": "userlog"   
                  },
              "name": "Reader",
              "category": "reader"
              },
              {
                  "stepType": "odps",
                  "parameter": {
                  "partition": "",
                  "isCompress": false,
                  "truncate": true,
                  "datasource": "odps_source",
                  "column": [
                  "mqdata"  
                  ],
                  "emptyAsNull": false,
                  "table": "mqdata"
                  },
                  "name": "Writer",
                  "category": "writer"
                  }
                  ],
                  "version": "2.0",
                  "order": {
                  "hops": [
                  {
                  "from": "Reader",
                  "to": "Writer"
                  }
                  ]
                  },
                  "setting": {
                  "errorLimit": {
                  "record": ""
                  },
                  "speed": {
                  "concurrent": 2,
                  "throttle": false,
                  }
                  }
              }
    7. Click **icon to run the code.

    8. You can operation Log view the results.

Verify the result

  1. Right-click the workflow and choose new > MaxCompute > ODPS SQL.

  2. In create a node dialog box, enter node name, and click submit.

  3. On the ODPS SQL node editing page, enter the following statement.

    SELECT * from mqdata;
  4. Click **icon to run the code.

  5. You can Runtime Log view the results.