天天看點

MySQL LOAD DATA的常見用法

目錄

  • ​​LOAD 基本背景​​
  • ​​LOAD 基礎參數​​
  • ​​LOAD 示例資料及示例表結構​​
  • ​​LOAD 場景示例​​
  • ​​場景1. LOAD 檔案中的字段比資料表中的字段多​​
  • ​​場景2. LOAD 檔案中的字段比資料表中的字段少​​
  • ​​場景3. LOAD 生成自定義字段資料​​
  • ​​場景4. LOAD 定長資料​​
  • ​​LOAD 總結​​

LOAD 基本背景

我們在資料庫運維過程中難免會涉及到需要對文本資料進行處理,并導入到資料庫中,本文整理了一些導入導出時常見的場景進行示例示範

LOAD 基礎參數

文章後續示例均使用以下指令導出的csv格式樣例資料(以​

​,​

​逗号做分隔符,以​

​"​

​雙引号作為界定符)
-- 導出基礎參數
select * into outfile '/data/mysql/3306/tmp/employees.txt'
character set utf8mb4
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
from employees.employees limit 10;

-- 導入基礎參數
load data infile '/data/mysql/3306/tmp/employees.txt'
replace into table demo.emp
character set utf8mb4
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
...      

LOAD 示例資料及示例表結構

以下為示例資料,表結構及對應關系資訊
-- 導出的檔案資料内容
[root@10-186-61-162 tmp]# cat employees.txt
"10001","1953-09-02","Georgi","Facello","M","1986-06-26"
"10002","1964-06-02","Bezalel","Simmel","F","1985-11-21"
"10003","1959-12-03","Parto","Bamford","M","1986-08-28"
"10004","1954-05-01","Chirstian","Koblick","M","1986-12-01"
"10005","1955-01-21","Kyoichi","Maliniak","M","1989-09-12"
"10006","1953-04-20","Anneke","Preusig","F","1989-06-02"
"10007","1957-05-23","Tzvetan","Zielinski","F","1989-02-10"
"10008","1958-02-19","Saniya","Kalloufi","M","1994-09-15"
"10009","1952-04-19","Sumant","Peac","F","1985-02-18"
"10010","1963-06-01","Duangkaew","Piveteau","F","1989-08-24"

-- 示例表結構
SQL > desc demo.emp;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| emp_no      | int           | NO   | PRI | NULL    |       |
| birth_date  | date          | NO   |     | NULL    |       |
| first_name  | varchar(16)   | NO   |     | NULL    |       |
| last_name   | varchar(16)   | NO   |     | NULL    |       |
| fullname    | varchar(32)   | YES  |     | NULL    |       | -- 表新增字段,導出資料檔案中不存在
| gender      | enum('M','F') | NO   |     | NULL    |       |
| hire_date   | date          | NO   |     | NULL    |       |
| modify_date | datetime      | YES  |     | NULL    |       | -- 表新增字段,導出資料檔案中不存在
| delete_flag | char(1)       | YES  |     | NULL    |       | -- 表新增字段,導出資料檔案中不存在
+-------------+---------------+------+-----+---------+-------+

-- 導出的資料與字段對應關系
emp_no   birth_date    first_name   last_name    gender  hire_date
"10001"  "1953-09-02"  "Georgi"     "Facello"    "M"    "1986-06-26"
"10002"  "1964-06-02"  "Bezalel"    "Simmel"     "F"    "1985-11-21"
"10003"  "1959-12-03"  "Parto"      "Bamford"    "M"    "1986-08-28"
"10004"  "1954-05-01"  "Chirstian"  "Koblick"    "M"    "1986-12-01"
"10005"  "1955-01-21"  "Kyoichi"    "Maliniak"   "M"    "1989-09-12"
"10006"  "1953-04-20"  "Anneke"     "Preusig"    "F"    "1989-06-02"
"10007"  "1957-05-23"  "Tzvetan"    "Zielinski"  "F"    "1989-02-10"
"10008"  "1958-02-19"  "Saniya"     "Kalloufi"   "M"    "1994-09-15"
"10009"  "1952-04-19"  "Sumant"     "Peac"       "F"    "1985-02-18"
"10010"  "1963-06-01"  "Duangkaew"  "Piveteau"   "F"    "1989-08-24"      

LOAD 場景示例

場景1. LOAD 檔案中的字段比資料表中的字段多

隻需要文本檔案中部分資料導入到資料表中
-- 臨時建立2個字段的表結構
SQL > create table emp_tmp select emp_no,hire_date from emp;
SQL > desc emp_tmp;
+-----------+------+------+-----+---------+-------+
| Field     | Type | Null | Key | Default | Extra |
+-----------+------+------+-----+---------+-------+
| emp_no    | int  | NO   |     | NULL    |       |
| hire_date | date | NO   |     | NULL    |       |
+-----------+------+------+-----+---------+-------+

-- 導入資料語句
load data infile '/data/mysql/3306/tmp/employees.txt'
replace into table demo.emp_tmp
character set utf8mb4
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(@C1,@C2,@C3,@C4,@C5,@C6) -- 該部分對應employees.txt檔案中6列資料
-- 隻對導出資料中指定的2個列與表中字段做比對,mapping關系指定的順序不影響導入結果
set hire_date=@C6,
    emp_no=@C1; 

-- 導入資料結果示例
SQL > select * from emp_tmp;
+--------+------------+
| emp_no | hire_date  |
+--------+------------+
|  10001 | 1986-06-26 |
|  10002 | 1985-11-21 |
|  10003 | 1986-08-28 |
|  10004 | 1986-12-01 |
|  10005 | 1989-09-12 |
|  10006 | 1989-06-02 |
|  10007 | 1989-02-10 |
|  10008 | 1994-09-15 |
|  10009 | 1985-02-18 |
|  10010 | 1989-08-24 |
+--------+------------+
10 rows in set (0.0016 sec)      

場景2. LOAD 檔案中的字段比資料表中的字段少

表字段不僅包含文本檔案中所有資料,還包含了額外的字段
-- 導入資料語句
load data infile '/data/mysql/3306/tmp/employees.txt'
replace into table demo.emp
character set utf8mb4
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(@C1,@C2,@C3,@C4,@C5,@C6) -- 該部分對應employees.txt檔案中6列資料
-- 将檔案中的字段與表中字段做mapping對應,表中多出的字段不做處理
set emp_no=@C1,
    birth_date=@C2,
    first_name=@C3,
    last_name=@C4,
    gender=@C5,
    hire_date=@C6;      
MySQL LOAD DATA的常見用法

場景3. LOAD 生成自定義字段資料

從場景2的驗證可以看到,emp表中新增的字段​

​fullname,modify_date,delete_flag​

​​字段在導入時并未做處理,被置為了NULL值,如果需要對其進行處理,可在LOAD時通過​

​MySQL支援的函數​

​​或給定​

​固定值​

​自行定義資料,對于檔案中存在的字段也可做函數處理,結合導入導出,實作簡單的ETL功能,如下所示
-- 導入資料語句
load data infile '/data/mysql/3306/tmp/employees.txt'
replace into table demo.emp
character set utf8mb4
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(@C1,@C2,@C3,@C4,@C5,@C6)                          -- 該部分對應employees.txt檔案中6列資料

-- 以下部分明确對表中字段與資料檔案中的字段做Mapping關系,不存在的資料通過函數處理生成(也可設定為固定值)
set emp_no=@C1,
    birth_date=@C2,
    first_name=upper(@C3),                          -- 将導入的資料轉為大寫
    last_name=lower(@C4),                            -- 将導入的資料轉為小寫
    fullname=concat(first_name,' ',last_name),      -- 對first_name和last_name做拼接
    gender=@C5,
    hire_date=@C6 ,
    modify_date=now(),                              -- 生成目前時間資料
    delete_flag=if(hire_date<'1988-01-01','Y','N'); -- 對需要生成的值基于某一列做條件運算      
MySQL LOAD DATA的常見用法

場景4. LOAD 定長資料

SQL > select 
    c1 as sample_data,
    substr(c1,1,3)  as c1,
    substr(c1,4,3)  as c2,
    substr(c1,7,2)  as c3,
    substr(c1,9,5)  as c4,
    substr(c1,14,3) as c5,
    substr(c1,17,3) as c6 from t1
    
*************************** 1. row ***************************
sample_data: ABC餘振興CDMySQLEFG資料庫
         c1: ABC
         c2: 餘振興
         c3: CD
         c4: MySQL
         c5: EFG
         c6: 資料庫      
-- 生成定長資料
SQL > select 
        concat(rpad(emp_no,10,' '),
          rpad(birth_date,19,' '),
          rpad(first_name,14,' '),
          rpad(last_name,16,' '),
          rpad(gender,2,' '),
          rpad(hire_date,19,' ')) as fixed_length_data 
      from employees.employees limit 10;

+----------------------------------------------------------------------------------+
| fixed_length_data                                                                |
+----------------------------------------------------------------------------------+
| 10001     1953-09-02         Georgi        Facello         M 1986-06-26          |
| 10002     1964-06-02         Bezalel       Simmel          F 1985-11-21          |
| 10003     1959-12-03         Parto         Bamford         M 1986-08-28          |
| 10004     1954-05-01         Chirstian     Koblick         M 1986-12-01          |
| 10005     1955-01-21         Kyoichi       Maliniak        M 1989-09-12          |
| 10006     1953-04-20         Anneke        Preusig         F 1989-06-02          |
| 10007     1957-05-23         Tzvetan       Zielinski       F 1989-02-10          |
| 10008     1958-02-19         Saniya        Kalloufi        M 1994-09-15          |
| 10009     1952-04-19         Sumant        Peac            F 1985-02-18          |
| 10010     1963-06-01         Duangkaew     Piveteau        F 1989-08-24          |
+----------------------------------------------------------------------------------+

-- 導出定長資料
select 
        concat(rpad(emp_no,10,' '),
          rpad(birth_date,19,' '),
          rpad(first_name,14,' '),
          rpad(last_name,16,' '),
          rpad(gender,2,' '),
          rpad(hire_date,19,' ')) as fixed_length_data 
into outfile '/data/mysql/3306/tmp/employees_fixed.txt'
character set utf8mb4
lines terminated by '\n'
from employees.employees limit 10;

-- 導出資料示例
[root@10-186-61-162 tmp]# cat employees_fixed.txt
10001     1953-09-02         Georgi        Facello         M 1986-06-26
10002     1964-06-02         Bezalel       Simmel          F 1985-11-21
10003     1959-12-03         Parto         Bamford         M 1986-08-28
10004     1954-05-01         Chirstian     Koblick         M 1986-12-01
10005     1955-01-21         Kyoichi       Maliniak        M 1989-09-12
10006     1953-04-20         Anneke        Preusig         F 1989-06-02
10007     1957-05-23         Tzvetan       Zielinski       F 1989-02-10
10008     1958-02-19         Saniya        Kalloufi        M 1994-09-15
10009     1952-04-19         Sumant        Peac            F 1985-02-18
10010     1963-06-01         Duangkaew     Piveteau        F 1989-08-24

-- 導入定長資料
load data infile '/data/mysql/3306/tmp/employees_fixed.txt'
replace into table demo.emp
character set utf8mb4
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(@row)  -- 對一行資料定義為一個整體
set emp_no      = trim(substr(@row,1,10)),-- 使用substr取前10個字元,并去除頭尾空格資料
    birth_date  = trim(substr(@row,11,19)),-- 後續字段以此類推
    first_name  = trim(substr(@row,30,14)),
    last_name    = trim(substr(@row,44,16)),
    fullname    = concat(first_name,' ',last_name),  -- 對first_name和last_name做拼接
    gender      = trim(substr(@row,60,2)),
    hire_date    = trim(substr(@row,62,19)),
    modify_date  = now(),
    delete_flag = if(hire_date<'1988-01-01','Y','N'); -- 對需要生成的值基于某一列做條件運算      

LOAD 總結

  1. 預設情況下導入的順序以文本檔案​

    ​列-從左到右,行-從上到下​

    ​的順序導入
  2. 如果表結構和文本資料不一緻,建議将文本檔案中的各列依次順序編号并與表中字段建立mapping關系,以防資料導入到錯誤的字段
  3. 對于待導入的文本檔案較大的場景,建議将檔案​

    ​按行拆分​

    ​為多個小檔案,如用split拆分
  4. 對檔案導入後建議執行以下語句驗證導入的資料是否有​

    ​Warning​

    ​,​

    ​ERROR​

    ​以及導入的資料量
  • ​GET DIAGNOSTICS @p1=NUMBER,@p2=ROW_COUNT;​

  • ​select @p1 AS ERROR_COUNT,@p2 as ROW_COUNT;​

  1. 文本檔案資料與表結構存在過大的差異或資料需要做清洗轉換,建議還是用專業的ETL工具或先粗略導入MySQL中再進行加工轉換處理