设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 663|回复: 0
打印 上一主题 下一主题

mysql之load data [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2011-5-17 12:46:41 |只看该作者 |倒序浏览
    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'    [REPLACE | IGNORE] INTO TABLE tbl_name    [CHARACTER SET charset_name]  [{FIELDS | COLUMNS} [TERMINATED BY 'string']  [[OPTIONALLY] ENCLOSED BY 'char']    [ESCAPED BY 'char'] ]  [LINES [STARTING BY 'string']    [TERMINATED BY 'string'] ]    [IGNORE number LINES]    [(col_name_or_user_var,...)]    [SET col_name = expr,...]
    LOW_PRIORITY 设置对于表级锁的存储引擎会导致load data需要在没有任何其他客户端在访问这张表时才会被执行   CONCURRENT   设置对于满足高并发(不包含空块)的myisam表执行load data时,其他线程可以检索这个表的数据
    ==默认不做设置==

    TERMINATED BY 将字段、行之间的间隔符进行过滤   ENCLOSED BY   将字符两端的引号字符过滤掉   ESCAPED BY    转义

    LINES STARTING BY  可以将每行包含特定字符的记录过滤后将特定字符后面的字符插入数据库   IGNORE umber LINES 可以设置将文件前多少行忽略   SET col_name=expr   可以将文本中未指定默认值的字段进行赋值
    注:
    1、mysql里面用“\”(反斜干)对字符进行转义,表达一个真实的“\”需要写成“\\”
    2、ENCLOSED BY、ESCAPED BY后面的值只能是单个的字符,TERMINATED BY、LINES STARTING BY和LINES TERMINATED BY后面可以跟多个字符


    关于mysql的远程load data参考陈旭的这篇blog总结的很好     http://orzdba.wordpress.com/2011/03/17/mysql_load-data/


    另外关于load data还有几个小细节以及实验过程如下:
    --local-infile选项
      0 启动mysqld从服务器端禁用所有LOAD DATA LOCAL命令;1  启用LOAD DATA LOCA

    如果LOAD DATA LOCAL INFILE在服务器或客户端被禁用,试图执行该语句的客户端将收到下面的错误消息:
    ERROR 1148: The used command is not allowed with this MySQL version

    (出现以上报错的时候就要检查下是否设置了--local-infile=1了,默认是0的)

    1、在需要远程导入数据的主机上授权用户:
    [MM-Writable@v031014.sqa.cm4 ~]
    grant select,insert,update,delete on test.* to 'qianwei'@'%' identified by 'qianwei';
    (远程登录的时候必须要有密码的帐号)

    2、测试表结构如下:
    [MM-Writable@v031014.sqa.cm4 ~]
    root@test 12:01:06>show create table tmp_qw_test\G
    *************************** 1. row ***************************
           Table: tmp_qw_test
    Create Table: CREATE TABLE `tmp_qw_test` (
      `id` bigint(20) unsigned NOT NULL auto_increment,
      `user_id` bigint(20) NOT NULL,
      `target_id` bigint(20) NOT NULL,
      `group_id` bigint(20) NOT NULL,
      `common_friend_count` int(11) NOT NULL,
      `common_friends` varchar(512) NOT NULL,
      `gmt_create` datetime NOT NULL,
      `gmt_modified` datetime NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gbk
    1 row in set (0.00 sec)

    root@test 12:09:00>select * from tmp_qw_test;
    Empty set (0.00 sec)

    3、数据文件格式如下:
    [MM-Writable@dev031033.sqa.cm4 /tmp]
    $more tmp_qw_test.txt
    "2011-04-03 00:00:00",100001536,34806598,11217864,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,204886714,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,48259232,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,201869594,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,76686624,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,279932430,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,54773280,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,448342406,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,362098910,1,"103924803"
    "2011-04-03 00:00:00",100001536,34806598,196823514,1,"103924803"

    对应的字段为:gmt_create,user_id,group_id,f_friend_id,commfriends,commfriends_list

    4、远程导数据命令:
    [MM-Writable@dev031033.sqa.cm4 /tmp]
    $mysql -uqianwei -pqianwei test -h10.232.31.14  --local-infile=1 --show-warnings -v -v -v -e "load data LOCAL infile '/tmp/tmp_qw_test.txt'  REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '\"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)";
    --------------
    load data LOCAL infile '/tmp/tmp_qw_test.txt'  REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)
    --------------

    Query OK, 10 rows affected, 1 warning (0.00 sec)
    Records: 10  Deleted: 0  Skipped: 0  Warnings: 0

    Warning (Code 1364): Field 'gmt_modified' doesn't have a default value
    Bye

    因为字段不是完全匹配,gmt_modified 字段表结构要求非空。此处会报warning,分别测试了使用replace、ignore或者忽略参数三种情况数据均能正常插入

    [MM-Writable@v031014.sqa.cm4 ~]
    root@test 12:09:09>select * from tmp_qw_test;
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    | id | user_id   | target_id | group_id | common_friend_count | common_friends | gmt_create          | gmt_modified        |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    |  1 | 100001536 |  11217864 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  2 | 100001536 | 204886714 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  3 | 100001536 |  48259232 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  4 | 100001536 | 201869594 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  5 | 100001536 |  76686624 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  6 | 100001536 | 279932430 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  7 | 100001536 |  54773280 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  8 | 100001536 | 448342406 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    |  9 | 100001536 | 362098910 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    | 10 | 100001536 | 196823514 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 0000-00-00 00:00:00 |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    10 rows in set (0.00 sec)

    5、文件中的双引号不进行过滤时,时间字段会被截断变为0:
    [MM-Writable@dev031033.sqa.cm4 /tmp]
    $mysql -uqianwei -pqianwei test -h10.232.31.14  --local-infile=1 --show-warnings -v -v -v -e "load data LOCAL infile '/tmp/tmp_qw_test.txt'  REPLACE into table tmp_qw_test fields terminated by ','  (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)";
    --------------
    load data LOCAL infile '/tmp/tmp_qw_test.txt'  REPLACE into table tmp_qw_test fields terminated by ','  (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends)
    --------------

    Query OK, 10 rows affected, 11 warnings (0.12 sec)
    Records: 10  Deleted: 0  Skipped: 0  Warnings: 10

    Warning (Code 1364): Field 'gmt_modified' doesn't have a default value
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 1
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 2
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 3
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 4
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 5
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 6
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 7
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 8
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 9
    Warning (Code 1264): Out of range value adjusted for column 'gmt_create' at row 10
    Bye

    表数据:
    [MM-Writable@v031014.sqa.cm4 ~]
    root@test 12:13:59>select * from tmp_qw_test;
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    | id | user_id   | target_id | group_id | common_friend_count | common_friends | gmt_create          | gmt_modified        |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    |  1 | 100001536 |  11217864 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  2 | 100001536 | 204886714 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  3 | 100001536 |  48259232 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  4 | 100001536 | 201869594 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  5 | 100001536 |  76686624 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  6 | 100001536 | 279932430 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  7 | 100001536 |  54773280 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  8 | 100001536 | 448342406 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  9 | 100001536 | 362098910 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    | 10 | 100001536 | 196823514 | 34806598 |                   1 | "103924803"    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    10 rows in set (0.01 sec)

    6、设置gmt_modified字段取值和gmt_create一致:
    [MM-Writable@dev031033.sqa.cm4 /tmp]
    $mysql -uqianwei -pqianwei test -h10.232.31.14  --local-infile=1 --show-warnings -v -v -v -e "load data LOCAL infile '/tmp/tmp_qw_test.txt'  REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '\"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends) set gmt_modified=gmt_create";
    --------------
    load data LOCAL infile '/tmp/tmp_qw_test.txt'  REPLACE into table tmp_qw_test fields terminated by ',' ENCLOSED BY '"' (gmt_create,user_id,group_id,target_id,common_friend_count,common_friends) set gmt_modified=gmt_create
    --------------

    Query OK, 10 rows affected (0.00 sec)
    Records: 10  Deleted: 0  Skipped: 0  Warnings: 0

    Bye

    [MM-Writable@v031014.sqa.cm4 ~]
    root@test 12:19:01>select * from tmp_qw_test;
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    | id | user_id   | target_id | group_id | common_friend_count | common_friends | gmt_create          | gmt_modified        |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    |  1 | 100001536 |  11217864 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  2 | 100001536 | 204886714 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  3 | 100001536 |  48259232 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  4 | 100001536 | 201869594 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  5 | 100001536 |  76686624 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  6 | 100001536 | 279932430 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  7 | 100001536 |  54773280 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  8 | 100001536 | 448342406 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  9 | 100001536 | 362098910 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    | 10 | 100001536 | 196823514 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    10 rows in set (0.00 sec)

    7、使用select...into outfile导出数据文件,此时使用的是tab键作为分隔符。导入时不需要进行符号相关处理
    [MM-Writable@v031014.sqa.cm4 ~]
    root@test 12:35:52>select * into outfile '/tmp/tmp_qw_test1.sql' from tmp_qw_test;
    Query OK, 10 rows affected (0.04 sec)

    [MM-Writable@v031014.sqa.cm4 /tmp]
    $more tmp_qw_test1.sql
    1 100001536 11217864 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    2 100001536 204886714 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    3 100001536 48259232 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    4 100001536 201869594 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    5 100001536 76686624 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    6 100001536 279932430 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    7 100001536 54773280 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    8 100001536 448342406 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    9 100001536 362098910 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00
    10 100001536 196823514 34806598 1 103924803 2011-04-03 00:00:00 2011-04-03 00:00:00


    [MM-Writable@v031014.sqa.cm4 /tmp]
    root@test 01:00:48>truncate tmp_qw_test;
    Query OK, 10 rows affected (0.00 sec)

    root@test 01:00:52>load data infile '/tmp/tmp_qw_test1.sql' into table tmp_qw_test(id,user_id,target_id,group_id,common_friend_count,common_friends,gmt_create,gmt_modified);
    Query OK, 10 rows affected (0.00 sec)
    Records: 10  Deleted: 0  Skipped: 0  Warnings: 0

    root@test 01:07:20>select * from tmp_qw_test;+----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    | id | user_id   | target_id | group_id | common_friend_count | common_friends | gmt_create          | gmt_modified        |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    |  1 | 100001536 |  11217864 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  2 | 100001536 | 204886714 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  3 | 100001536 |  48259232 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  4 | 100001536 | 201869594 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  5 | 100001536 |  76686624 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  6 | 100001536 | 279932430 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  7 | 100001536 |  54773280 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  8 | 100001536 | 448342406 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    |  9 | 100001536 | 362098910 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    | 10 | 100001536 | 196823514 | 34806598 |                   1 | 103924803      | 2011-04-03 00:00:00 | 2011-04-03 00:00:00 |
    +----+-----------+-----------+----------+---------------------+----------------+---------------------+---------------------+
    10 rows in set (0.00 sec)

    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-1 18:40 , Processed in 0.103918 second(s), 28 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部