简体   繁体   English

如何使用简单的查询将 JSON 文件导入 MySQL 数据库,而不实际将其转换为任何其他文件格式,如 CSV 等?

[英]How can I import a JSON file into MySQL database, using a simple query, without actually converting it to any other file formats like CSV etc.?

I tried to import a JSON file which looks like:我试图导入一个 JSON 文件,它看起来像:

[ 
{ 
"executionDateTime":"2017-07-07 15:21:15",
"A":1,
"B":1
},
{ 
"executionDateTime":"2017-07-07 15:21:15",
"A":2,
"B":2
},
{ 
"executionDateTime":"2017-07-07 15:21:15",
"A":3,
"B":3
},
{ 
"executionDateTime":"2017-07-07 15:21:15",
"A":4,
"B":4
}]

I want to import the above file into a mySQL DB, and i want my table to look somewhat like this:我想将上述文件导入到 mySQL DB 中,并且我希望我的表看起来像这样:

 executionDateTime           A               B

2017-07-07 15:21:15          1               1
2017-07-07 15:21:15          2               2
2017-07-07 15:21:15          3               3
2017-07-07 15:21:15          4               4

I have tried the below query to do so (which would work out for me if the file format is CSV), but it didn't work.我已经尝试了下面的查询来这样做(如果文件格式是 CSV,这对我有用),但它没有用。

LOAD DATA local INFILE '<path>/my_file.json' 
INTO TABLE database_name.my_table FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'LINES TERMINATED BY '\n' IGNORE 1 ROWS;

The above query gave me a table which looked like this:上面的查询给了我一个看起来像这样的表:

输出表

(I KNOW, mySQL HAS AN INBUILT FUNCTION TO IMPORT A FILE, BUT I DO NOT WANT TO USE THAT FUNCTION, A QUERY IS WHAT I AM LOOKING FOR... :) ) (我知道,mySQL 具有导入文件的内置功能,但我不想使用该功能,我正在寻找一个查询...... :))

Please suggest, if anyone had to face such a problem and got a solution.请建议,如果有人不得不面对这样的问题并得到解决方案。 Please don't mark this question as a duplicate without giving a SOLUTION THAT ACTUALLY WORKS.如果没有给出实际有效的解决方案,请不要将此问题标记为重复。

Many thanks.非常感谢。

Reference: https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html参考: https : //dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

I know this is an older thread, but MySQL 5.7 now has a JSON type where you can import JSON into a field, then you can use calculated fields to split the json into separate fields.我知道这是一个较旧的线程,但 MySQL 5.7 现在有一个 JSON 类型,您可以在其中将 JSON 导入一个字段,然后您可以使用计算字段将 json 拆分为单独的字段。 Here's rough code (not tested):这是粗略的代码(未经测试):

Create a JSON Test Table:创建一个 JSON 测试表:

CREATE TABLE IF NOT EXISTS jsontest(
     rowid INT AUTO_INCREMENT NOT NULL UNIQUE,
     jsondata json,
     `executionDateTime` TIMESTAMP,
     `A` BIGINT UNSIGNED,
     `B` BIGINT UNSIGNED,
     );

Import your JSON into JSON field:将您的 JSON 导入 JSON 字段:

LOAD DATA LOCAL INFILE '/path/to/testfile.json' into table jsontest(jsondata);

Split your Data Up (this could be combined into a single command)拆分您的数据(这可以合并为一个命令)

UPDATE jsontest set executionDateTime=(jsondata->>'$.executionDateTime');
UPDATE jsontest set A=(jsondata->>'$.A');
UPDATE jsontest set B=(jsondata->>'$.B');

If you don't want to have extra fields, you can query the jsondata field like this:如果你不想有额外的字段,你可以像这样查询 jsondata 字段:

SELECT jsondata->>"$.executionDateTime" AS executionDateTime,
       jsondata->>"$.A" AS A,
       jsondata->>"$.B" AS B;

I hope that in the near future there is a native functionality from MySQL.我希望在不久的将来有来自 MySQL 的本机功能。

An option (not simple query) is something like the following script (adapt as needed).一个选项(不是简单的查询)类似于以下脚本(根据需要进行调整)。 Depending on the number of items may have performance problems.根据项目的数量可能有性能问题。

File: /path/to/file/loadsetProfile.json :文件: /path/to/file/loadsetProfile.json

[
  {
    "executionDateTime":"2017-07-07 15:21:15",
    "A":1,
    "B":1
  },
  {
    "executionDateTime":"2017-07-07 15:21:15",
    "A":2,
    "B":2
  },
  {
    "executionDateTime":"2017-07-07 15:21:15",
    "A":3,
    "B":3
  },
  {
    "executionDateTime":"2017-07-07 15:21:15",
    "A":4,
    "B":4
  }
]

MySQL Command-Line : MySQL命令行

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

mysql> DROP PROCEDURE IF EXISTS `import_from_json`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP FUNCTION IF EXISTS `uuid_to_bin`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `temp_my_table`, `my_table`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `temp_my_table` (
    ->   `id` BINARY(16) NOT NULL PRIMARY KEY,
    ->   `content` JSON NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `my_table` (
    ->   `executionDateTime` TIMESTAMP,
    ->   `A` BIGINT UNSIGNED,
    ->   `B` BIGINT UNSIGNED
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE FUNCTION `uuid_to_bin` (`id` VARCHAR(36))
    -> RETURNS BINARY(16)
    -> DETERMINISTIC
    ->   RETURN UNHEX(REPLACE(`id`, '-', ''));
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `import_from_json`(`_id` VARCHAR(36))
    -> BEGIN
    ->   DECLARE `_id_current_json` BINARY(16) DEFAULT `uuid_to_bin`(`_id`);
    ->   DECLARE `_items_length`,
    ->           `_current_item` BIGINT UNSIGNED DEFAULT 0;
    ->   DECLARE `_content` JSON DEFAULT (SELECT `content`
    ->                                    FROM `temp_my_table`
    ->                                    WHERE `id` = `_id_current_json`);
    -> 
    ->   IF JSON_VALID(`_content`) THEN
    ->     SET `_items_length` := JSON_LENGTH(`_content`),
    ->         @`insert_import_from_json` := NULL;
    ->     WHILE `_current_item` < `_items_length` DO
    ->       SET @`insert_import_from_json` := CONCAT('
    '>         INSERT INTO `my_table` (
    '>            `executionDateTime`,
    '>            `A`,
    '>            `B`
    '>         )
    '>         SELECT
    '>           `content` ->> \'$[', `_current_item`, '].executionDateTime\',
    '>           `content` ->> \'$[', `_current_item`, '].A\',
    '>           `content` ->> \'$[', `_current_item`, '].B\'
    '>         FROM `temp_my_table`
    '>         WHERE `id` = \'', `_id_current_json`, '\'
    '>       ');
    ->       PREPARE `stmt` FROM @`insert_import_from_json`;
    ->       EXECUTE `stmt`;
    ->       SET `_current_item` := `_current_item` + 1;
    ->     END WHILE;
    ->
    ->     IF `_current_item` > 0 THEN
    ->       SET @`insert_import_from_json` := NULL;
    ->       DEALLOCATE PREPARE `stmt`;
    ->     END IF;
    ->   END IF;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> SET @`UUID` := UUID();
Query OK, 0 rows affected (0.00 sec)

mysql> LOAD DATA LOCAL INFILE '/path/to/file/loadsetProfile.json' 
    -> INTO TABLE `temp_my_table`
    -> LINES TERMINATED BY '\r'
    -> (`content`)
    -> SET `id` = `uuid_to_bin`(@`UUID`);
Query OK, 1 row affected (0.00 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql> CALL `import_from_json`(@`UUID`);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    ->   `executionDateTime`,
    ->   `A`,
    ->   `B`
    -> FROM
    ->   `my_table`;
+---------------------+------+------+
| executionDateTime   | A    | B    |
+---------------------+------+------+
| 2017-07-07 15:21:15 |    1 |    1 |
| 2017-07-07 15:21:15 |    2 |    2 |
| 2017-07-07 15:21:15 |    3 |    3 |
| 2017-07-07 15:21:15 |    4 |    4 |
+---------------------+------+------+
4 rows in set (0.01 sec)

I am using this web page which transform json file in sql file, after that, I execute script in toad mysql and works.我正在使用这个网页,它在 sql 文件中转换 json 文件,之后,我在 toad mysql 中执行脚本并工作。

URI of web I am using is converter web我正在使用的 Web URI 是转换器 Web

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM