以编程方式使用node.js读取JSON和INSERT INTO mysql的最佳方法

时间:2013-09-26 12:04:02

标签: mysql json migration

建议读取json并将其插入mysql db的最佳方法。

示例JSON:

{
    "PaymentOrder": [
        {
            "$": {
                "Id": "83C44C4A-7EFD-491E-913E-007A3598618F",
                "Name": "",
                "OrderNumber": "AH9NZPUI",
                "OrderType": "Recurring",
                "Amount": "1.000000",
                "CouponDiscountAmount": ".000000",
                "ChargeAmount": ".000000",
                "CurrencyCode": "USD",
                "OwnerId": "D9F48450-AB86-4ABE-B9AD-66FE3B7AEFB1",
                "OrderStatus": "Pending",
                "PaymentProcessor": "Global Collect",
                "PaymentMethodType": "Visa",
                "SoftDescriptorText": "CNext_AH9NZPUI",
                "UserPaymentInfoId": "FDD9C487-6F66-4919-B945-766699C9EF1D",
                "IsAuthorization": "1",
                "CreateTime": "2012-07-18T10:21:47.7610377",
                "UpdateTime": "2012-07-18T10:21:47.7610377",
                "IsEnabled": "1",
                "IsDeleted": "0"
            }
        },
        {
            "$": {
                "Id": "03B1600F-F92A-47BA-8B53-00B70A942E43",
                "Name": "",
                "OrderNumber": "3427BBC3",
                "OrderType": "Recurring",
                "Amount": ".000000",
                "CouponDiscountAmount": ".000000",
                "ChargeAmount": ".000000",
                "CurrencyCode": "USD",
                "OwnerId": "9A1186E4-BAC6-43D0-8C6F-B02ECD6544B5",
                "OrderStatus": "Success",
                "PaymentProcessor": "Global Collect",
                "PaymentMethodType": "Visa",
                "SoftDescriptorText": "ComputeNext Inc Order Number - 3427BBC3",
                "UserPaymentInfoId": "142C6B38-A470-40A9-B7A7-1C8CDD6FBD2C",
                "BillingHistoryId": "6BE8DF8E-4C97-492B-9407-38EBA2B942F2",
                "InvoiceDate": "2013-03-26T14:02:07.8570464",
                "IsAuthorization": "0",
                "CreateTime": "2013-03-26T18:08:19.5383118",
                "UpdateTime": "2013-03-26T18:08:19.5695118",
                "IsEnabled": "1",
                "IsDeleted": "0"
            }
        },
        {
            "$": {
                "Id": "8FC1D8EA-C388-4F31-91C7-00CB54C337A4",
                "Name": "",
                "OrderNumber": "YTM8TOKQ",
                "OrderType": "Recurring",
                "Amount": "1.000000",
                "CouponDiscountAmount": ".000000",
                "ChargeAmount": ".000000",
                "CurrencyCode": "USD",
                "OwnerId": "3702AB47-1095-4211-B844-82C47D0D9F6C",
                "OrderStatus": "Failure",
                "PaymentProcessor": "Global Collect",
                "PaymentMethodType": "Visa",
                "SoftDescriptorText": "CNext_YTM8TOKQ",
                "UserPaymentInfoId": "19193F72-14D1-4A72-9676-2493002711ED",
                "TransactionDuration": "0:0:40:809",
                "LastTransactionNumber": "HC0F4CBYMT14QP3",
                "IsAuthorization": "1",
                "CreateTime": "2012-07-26T21:15:25.8004068",
                "UpdateTime": "2012-07-26T21:16:06.6100785",
                "IsEnabled": "1",
                "IsDeleted": "0"
            }
        }
    ]
}

JSON中存在的属性等同于mysql中的列。

1 个答案:

答案 0 :(得分:1)

您可以使用您选择的语言对此进行编码(您只需要一个JSON读取工具和一个MySQL客户端,但现在这些都可以在所有语言中使用),或者您可以使用ETL工具。

使用ETL的优势

通常可以通过重新配置来解决源格式或目标格式的更改(例如,您需要从CSV文件或XML文件中读取这些数据)或内容(添加,删除或重命名字段)工具。

使用ETL的缺点

这是你必须管理的另一个(相当沉重的)基础设施,有些人会争辩说使用ETL工具完成这样的任务就像用大锤杀死一只苍蝇。

然而,根据您对灵活性的要求,ETL可能是最好/最简单的解决方案。