首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在字段数据中不使用包含编码JSON值的CSV的加载数据INFILE

问在字段数据中不使用包含编码JSON值的CSV的加载数据INFILE
EN

Stack Overflow用户
提问于 2020-02-04 14:03:11
回答 1查看 977关注 0票数 1

我要在mysql表中插入数百万条记录。为了快速地将数据迁移到表中,我尝试使用概念。

下面是CSV文件中数据的格式(一些具有JSON数据的字段)。

代码语言:javascript
复制
"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"

当我试图执行代码时,它会显示错误如下-

SQLSTATE22032:<>:3140无效JSON文本:“对象成员后面缺少逗号或'}‘。列“user_day_record.notesObj”的值位于42位置

基本上,当我试图在CSV中没有JSON值的情况下执行相同的命令时,它工作得很好。但是对于文件中的JSON数据(在某些字段中),它会产生错误。

这是命令-

代码语言:javascript
复制
LOAD DATA LOCAL INFILE '$file' INTO TABLE user_day_record CHARACTER SET UTF8 FIELDS TERMINATED BY ', ' LINES TERMINATED BY '\n' 

有人能帮助我在CSV文件中的字段中使用json值成功地执行命令吗?

更新-

以下是user_day_record表的结构-

​

​

EN

回答 1

Stack Overflow用户

发布于 2020-03-23 13:57:49

下面是加载包含JSON对象的CSV文件的解决方案。下面是我们导入时必须使用的额外选项

字段以“,”结尾,可选地以“\”括起来

代码语言:javascript
复制
escaped by '\'     escaped by '\"'     lines terminated by '\n'
代码语言:javascript
复制
*************************** 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>```
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60059075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档