簡體   English   中英

將 JSON 文件導入 MySQL 5.7

[英]Importing JSON file to MySQL 5.7

我無法將 .json 文件作為 Mysql 5.7 中的表導入; 所有行都留空。 我不確定錯誤在哪里。

我正在使用存儲在/home/name/json_data/sample.json下的以下 json 文件

{
    "price": null,
    "sale_list": [
        {
            "buyer": "SmackMe089",
            "date": "April 29th 2019 21:20:50",
            "id": "1234",
            "item_desc": ""
        }
}

當我嘗試使用以下sql文件將其導入 mysql 5.7 時,未導入任何內容。

文件.sql:

CREATE TABLE example_table (
         id INT NOT NULL AUTO_INCREMENT,
         json_data JSON NOT NULL,
         PRIMARY KEY (id)
);

LOAD DATA INFILE '/home/path/json_data/sample.json' INTO TABLE example_table (json_data);

嘗試導入 mysql: mysql --host=host_ip -u root -p db_name < /home/path/data/file.sql

幾個選項是:

  1. 13.2.6 加載數據語法
  2. 7.2 JSON 導入實用程序

測試:

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.26 MySQL Community Server (GPL)

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.26    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `test`.`example_table`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `test`.`example_table` (
    ->   `id` INT NOT NULL AUTO_INCREMENT,
    ->   `json_data` JSON NOT NULL,
    ->   PRIMARY KEY (`id`)
    -> );
Query OK, 0 rows affected (0.01 sec)

1.加載數據

文件/path/to/file/sample.json

{"price": null, "sale_list": [{"buyer": "SmackMe089", "date": "April 29th 2019 21:20:50", "id": "1234", "item_desc": ""}]}

MySQL 命令行客戶端

mysql> LOAD DATA INFILE '/path/to/file/sample.json'
    -> INTO TABLE `test`.`example_table` (`json_data`);
Query OK, 1 row affected (0.01 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql> SELECT `id`, `json_data`
    -> FROM `test`.`example_table`\G
*************************** 1. row ***************************
       id: 1
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
1 row in set (0.00 sec)

2. JSON 導入工具

文件/path/to/file/sample.json

{
    "price": null,
    "sale_list": [
        {
            "buyer": "SmackMe089",
            "date": "April 29th 2019 21:20:50",
            "id": "1234",
            "item_desc": ""
        }
    ]
}

MySQL Shell :正如@TimBiegeleisen 所說,您可以使用MySQL Shell (即使是 MySQL 5.7),但您必須激活X 插件

$ mysqlsh --sql
MySQL Shell 8.0.16

Your MySQL connection id is 2 (X protocol)
Server version: 5.7.26 MySQL Community Server (GPL)
No default schema selected; type \use <schema> to set one.

MySQL 127.0.0.1:15726+ SQL > SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.26    |
+-----------+
1 row in set (0.0004 sec)

MySQL 127.0.0.1:15726+ SQL > SELECT `id`, `json_data`
                          -> FROM `test`.`example_table`\G
*************************** 1. row ***************************
       id: 1
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
1 row in set (0.00 sec)

MySQL 127.0.0.1:15726+ SQL > \js
Switching to JavaScript mode...

MySQL 127.0.0.1:15726+ JS > util.importJson('/path/to/file/sample.json', {schema: 'test', table: 'example_table', tableColumn: 'json_data'});
Importing from file "/path/to/file/sample.json" to table `test`.`example_table` in MySQL Server at 127.0.0.1:15726

.. 1.. 1
Processed 204 bytes in 1 document in 0.0007 sec (1.36K documents/s)
Total successfully imported documents 1 (1.36K documents/s)

MySQL 127.0.0.1:15726+ JS > \sql
Switching to SQL mode... Commands end with ;

MySQL 127.0.0.1:15726+ SQL > SELECT `id`, `json_data`
                          -> FROM `test`.`example_table`\G
*************************** 1. row ***************************
       id: 1
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
1 row in set (0.0007 sec)
*************************** 2. row ***************************
       id: 2
json_data: {"price": null, "sale_list": [{"id": "1234", "date": "April 29th 2019 21:20:50", "buyer": "SmackMe089", "item_desc": ""}]}
2 rows in set (0.0006 sec)

沒有辦法使用 LOAD DATA INFILE 來加載 JSON,因為正如上面的評論所說,該語句只加載 CSV 數據。

如果要將 JSON 文檔作為標量加載到表的一行中,可以使用LOAD_FILE()函數。

INSERT INTO example_table SET json_data = LOAD_FILE('/home/path/json_data/sample.json');

這只創建一行來存儲整個 JSON 文檔。 它不會解析 JSON 以在 JSON 中為 sales_list 數組中的每個條目插入單獨的行。

閱讀我鏈接到的文檔以獲取有關LOAD_FILE()函數的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM