简体   繁体   English

将数据导入到mysql的csv文件

[英]import csv file with data to mysql

I'm trying to import csv file to MYSQL, and I have the following schema. 我正在尝试将csv文件导入MYSQL,我有以下架构。

CREATE TABLE `monitor` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`time` time DEFAULT NULL,
`domain_name` text,
`cpu_ns` int(11) DEFAULT NULL,
`cpu_percentage` int(11) DEFAULT NULL,
`mem_bytes` int(11) DEFAULT NULL,
`mem_percentage` int(11) DEFAULT NULL,
`block_rdby` int(11) DEFAULT NULL,
`block_wrby` int(11) DEFAULT NULL,
`net_rxby` int(11) DEFAULT NULL,
`net_wrby` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

I'm having issues importing a file with data presented as follows. 我在导入包含如下数据的文件时遇到问题。

16:48:27,2,s-1-VM,220000000.,0.448204684384,262144,0,0,0,60,0
16:48:30,2,s-1-VM,260000000.,0.528932926209,262144,0,0,16384,300,0
16:48:33,2,s-1-VM,300000000.,0.609786677944,262144,0,0,0,180,0
16:48:37,2,s-1-VM,290000000.,0.59000206364,262144,0,0,16384,120,0
16:48:40,2,s-1-VM,270000000.,0.54985661784,262144,0,0,0,649,425
16:48:43,2,s-1-VM,310000000.,0.631207212346,262144,0,0,0,180,0
16:48:46,2,s-1-VM,220000000.,0.44728232907,262144,0,0,20480,60,0
16:48:49,2,s-1-VM,200000000.,0.407008216196,262144,0,0,0,300,0
16:48:52,2,s-1-VM,250000000.,0.508946559213,262144,0,0,0,240,0
16:48:55,2,s-1-VM,240000000.,0.488674160215,262144,0,0,0,120,0

How can import this to my database? 如何将其导入我的数据库?

I have tried the following and I get lots of warnings. 我尝试了以下内容,并收到了很多警告。

LOAD DATA LOCAL INFILE '/tmp/domain2.csv' INTO TABLE vtop  FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';

your help is highly appreciated. 非常感谢您的帮助。

Thank you 谢谢

If I understand http://dev.mysql.com/doc/refman/5.1/de/load-data.html correctly, you should set ID to null (that makes it auto_increment) via 如果我正确理解http://dev.mysql.com/doc/refman/5.1/de/load-data.html ,您应该将ID设置为null(使其成为auto_increment)

"SET id=NULL " 

at the end of the statement. 在声明的最后。 Otherwise column counts and column orders have to match perfectly. 否则列数和列顺序必须完美匹配。

But your columns don't match at all (what is the "2" at position 2?). 但你的列根本不匹配(位置2的“2”是什么?)。 So create a temp-Table with the structure of your CSV and then assign via insert into ... select the matching columns. 因此,使用CSV的结构创建一个临时表,然后通过插入分配到...中选择匹配的列。

MySQL isn't an AI and can't figure out that 16:48:27 should go into the the time field - it'll be trying to stuff that into id instead. MySQL不是AI,也无法弄清楚16:48:27应该进入time字段 - 它会尝试将其填入id而不是。

You need to explictly map the columns in your CSV file to the fields they should go into in the table: 您需要明确地将CSV文件中的列映射到它们应该在表中输入的字段:

LOAD DATA .... (time, domain_name, x, y, z, foo, bar) 

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

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