LOAD DATA INFILE не работает с CSV, содержащим закодированное значение JSON в полевых данных - PullRequest
0 голосов
/ 04 февраля 2020

У меня есть миллионы записей для вставки в таблицу mysql. Чтобы быстро перенести данные в таблицу, я попытался использовать концепцию LOAD DATA INFILE.

Ниже приведен формат данных в файле CSV (некоторые поля содержат данные JSON).

"1299","1420070400000","35520","1420070400000","0","0","0","2","null","{""types"": [3]}","{""types"": [2]}","{""type"": 0, ""color"": 0, ""types"": [1], ""colors"": [2]}","null","null","null","null","{""type"": 0, ""types"": [], ""sexDrive"": 0, ""sexDrives"": [1]}","null","{""results"": [2]}","{""types"": [3]}","null","null","null","{""types"": [5]}","null","null","null","null","null","null","null","null","2020-01-30 11:48:07","2020-01-30 11:48:07"

"1300","1420070400000","48219","1420070400000","0","0","0","2","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","2020-01-30 11:48:07","2020-01-30 11:48:07"

"1301","1420070400000","34938","1420070400000","0","0","0","2","null","null","null","{""type"": 0, ""color"": 0, ""types"": [1], ""colors"": [3]}","null","null","{""types"": [2]}","{""pain"": 0, ""pains"": [2], ""otherPains"": [3, 4, 5, 6], ""breastSymptom"": false, ""breastSymptoms"": [1]}","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","null","2020-01-30 11:48:07","2020-01-30 11:48:07"

Когда я пытался выполнить код, он показывает ошибку, как показано ниже -

SQLSTATE [22032]: <>: 3140 Неверный JSON текст: «Отсутствует запятая или '}» после объекта член «. в позиции 42 в значении для столбца 'user_day_record.notesObj'

В принципе, когда я пытаюсь выполнить ту же команду без значений JSON в CSV, она работает отлично. Но с данными JSON (в некоторых полях) в файле это выдает ошибку.

Вот команда -

LOAD DATA LOCAL INFILE '$file' INTO TABLE user_day_record CHARACTER SET UTF8 FIELDS TERMINATED BY ', ' LINES TERMINATED BY '\n' 

Может ли кто-нибудь помочь мне успешно выполнить команду со значениями json в полях в файле CSV?

Обновить -

Вот структура таблицы user_day_record -

user_day_record_table

1 Ответ

1 голос
/ 23 марта 2020

Вот решение для загрузки файла CSV, который содержит объект JSON. Ниже приведены дополнительные параметры, которые мы должны использовать для импорта полей

, оканчивающихся на ',', необязательно заключенных в '\ "'
, экранированных символами '\', экранированных '\' ', оканчивающихся на' \ n '

*************************** 1. row ***************************
       Table: user_day_record
Create Table: CREATE TABLE `user_day_record` (
  `dayRecordld` bigint(20) NOT NULL AUTO_INCREMENT,
  `id` varchar(191) NOT NULL,
  `userld` int(11) NOT NULL,
  `daylnMillis` bigint(20) NOT NULL,
  `periodStatusActive` tinyint(4) NOT NULL DEFAULT '0',
  `ignoreB8T` tinyint(4) NOT NULL DEFAULT '0',
  `isOCOff` tinyint(4) NOT NULL DEFAULT '0',
  `version` int(11) DEFAULT NULL,
  `bbt` json DEFAULT NULL,
  `cervicalMucusObj` json DEFAULT NULL,
  `cervicalPositionObj` json DEFAULT NULL,
  `spotting` json DEFAULT NULL,
  `periodStrength` json DEFAULT NULL,
  `temperaturelnterferenceObj` json DEFAULT NULL,
  `hygieneProductsObj` json DEFAULT NULL,
  `pain` json DEFAULT NULL,
  `intercourse` json DEFAULT NULL,
  `ovulationTestObj` json DEFAULT NULL,
  `pregnancyTestObj` json DEFAULT NULL,
  `illnessObj` json DEFAULT NULL,
  `medicationObj` json DEFAULT NULL,
  `notesObj` json DEFAULT NULL,
  `sleep` json DEFAULT NULL,
  `emotionsObj` json DEFAULT NULL,
  `activitiesObj` json DEFAULT NULL,
  `oralContraceptives` json DEFAULT NULL,
  `digestion` json DEFAULT NULL,
  `pExams` json DEFAULT NULL,
  `pMyBaby` json DEFAULT NULL,
  `pUltrasound` json DEFAULT NULL,
  `pVitamin` json DEFAULT NULL,
  `pWeight` json DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`dayRecordld`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> load data local infile '/var/root/temp_Data.csv' into table user_day_record fields terminated by ',' optionally enclosed by '\"'  escaped by '\\' escaped by '\"' lines terminated by '\n';
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql>
mysql> select * from user_day_record\G
*************************** 1. row ***************************
               dayRecordld: 1299
                        id: 1420070400000
                    userld: 35520
               daylnMillis: 1420070400000
        periodStatusActive: 0
                 ignoreB8T: 0
                   isOCOff: 0
                   version: 2
                       bbt: null
          cervicalMucusObj: {"types": [3]}
       cervicalPositionObj: {"types": [2]}
                  spotting: {"type": 0, "color": 0, "types": [1], "colors": [2]}
            periodStrength: null
temperaturelnterferenceObj: null
        hygieneProductsObj: null
                      pain: null
               intercourse: {"type": 0, "types": [], "sexDrive": 0, "sexDrives": [1]}
          ovulationTestObj: null
          pregnancyTestObj: {"results": [2]}
                illnessObj: {"types": [3]}
             medicationObj: null
                  notesObj: null
                     sleep: null
               emotionsObj: {"types": [5]}
             activitiesObj: null
        oralContraceptives: null
                 digestion: null
                    pExams: null
                   pMyBaby: null
               pUltrasound: null
                  pVitamin: null
                   pWeight: null
                created_at: 2020-01-30 11:48:07
                updated_at: 2020-01-30 11:48:07
*************************** 2. row ***************************
               dayRecordld: 1300
                        id: 1420070400000
                    userld: 48219
               daylnMillis: 1420070400000
        periodStatusActive: 0
                 ignoreB8T: 0
                   isOCOff: 0
                   version: 2
                       bbt: null
          cervicalMucusObj: null
       cervicalPositionObj: null
                  spotting: null
            periodStrength: null
temperaturelnterferenceObj: null
        hygieneProductsObj: null
                      pain: null
               intercourse: null
          ovulationTestObj: null
          pregnancyTestObj: null
                illnessObj: null
             medicationObj: null
                  notesObj: null
                     sleep: null
               emotionsObj: null
             activitiesObj: null
        oralContraceptives: null
                 digestion: null
                    pExams: null
                   pMyBaby: null
               pUltrasound: null
                  pVitamin: null
                   pWeight: null
                created_at: 2020-01-30 11:48:07
                updated_at: 2020-01-30 11:48:07
*************************** 3. row ***************************
               dayRecordld: 1301
                        id: 1420070400000
                    userld: 34938
               daylnMillis: 1420070400000
        periodStatusActive: 0
                 ignoreB8T: 0
                   isOCOff: 0
                   version: 2
                       bbt: null
          cervicalMucusObj: null
       cervicalPositionObj: null
                  spotting: {"type": 0, "color": 0, "types": [1], "colors": [3]}
            periodStrength: null
temperaturelnterferenceObj: null
        hygieneProductsObj: {"types": [2]}
                      pain: {"pain": 0, "pains": [2], "otherPains": [3, 4, 5, 6], "breastSymptom": false, "breastSymptoms": [1]}
               intercourse: null
          ovulationTestObj: null
          pregnancyTestObj: null
                illnessObj: null
             medicationObj: null
                  notesObj: null
                     sleep: null
               emotionsObj: null
             activitiesObj: null
        oralContraceptives: null
                 digestion: null
                    pExams: null
                   pMyBaby: null
               pUltrasound: null
                  pVitamin: null
                   pWeight: null
                created_at: 2020-01-30 11:48:07
                updated_at: 2020-01-30 11:48:07
3 rows in set (0.00 sec)

mysql>```
...