我正在尝试使用 LOAD INFILE 导入 txt 文件,如下所示。
LOAD DATA LOCAL INFILE '/home/xxxx/cloud_20181003.txt' INTO TABLE cloud_log_test FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (CACHECACHESTATUS,CACHERESPONSEBYTES);
它正在正确导入。问题是我的数据采用键值格式。因此,在插入单元格时,它的插入方式类似于"CacheCacheStatus":"unknown"
CacheCacheStatus 列。它需要像 一样单独存储值unknown
。我尝试添加如下所示的替换,但它将完整的单元格插入为空。
LOAD DATA LOCAL INFILE '/home/xxxx/cloud_20181003.txt' INTO TABLE cloud_log_test FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (CACHECACHESTATUS,CACHERESPONSEBYTES) SET CacheCacheStatus = REPLACE(@CacheCacheStatus,'"CacheCacheStatus":"','');
答案1
我在查询中漏掉了“@”。 :-) 现在工作。
LOAD DATA LOCAL INFILE '/home/xxxx/cloud_20181003.txt' INTO TABLE cloud_log_test FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@CACHECACHESTATUS,CACHERESPONSEBYTES) SET CacheCacheStatus = REPLACE(@CacheCacheStatus,'"CacheCacheStatus":"','');