1. 程式人生 > 其它 >MySQL Load Data 多種用法

MySQL Load Data 多種用法

MySQL Load Data 多種用法

1、
--匯出基礎引數 (以為,逗號作為分隔符,以"雙引號作為界定符)

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;
select * from new into outfile '/tmp/new_files.txt' character set utf8mb4 fields terminated by '
,' enclosed by '"';

--匯入基礎引數

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';

2、檔案中的欄位比資料表中的欄位多
-- 匯入資料語句

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;

示例:

檢視new表字段
mysql> desc new;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| user_id | varchar(200
) | YES | | NULL | | | bike_sn | varchar(200) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+ 建立new_one表: mysql> desc new_one; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | user_id | varchar(200) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+

--只匯入user_id欄位資料

mysql> load data infile '/tmp/new_files.txt' replace into table new_one character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n'(@c1,@c2) set user_id=@c1;
Query OK, 182 rows affected (0.00 sec)

3、檔案中的欄位比資料表中的欄位少
-- 匯入資料語句

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;

4、Load 生成自定義的資料
表中新增的欄位 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');  -- 對需要生成的值基於某一列做條件運算

5、匯出定長資料

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;

    匯入定長資料 

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 Data 總結:
1. 預設情況下匯入的順序以文字檔案 列-從左到右,行-從上到下 的順序匯入
2. 如果表結構和文字資料不一致,建議將文字檔案中的各列依次順序編號並與表中欄位建立 mapping 關 系,以防資料匯入到錯誤的欄位
3. 對於待匯入的文字檔案較大的場景,建議將檔案 按行拆分 為多個小檔案,如用 split 拆分
4. 對檔案匯入後建議執行以下語句驗證匯入的資料是否有 Warning ,ERROR 以及匯入的資料量:
    GET DIAGNOSTICS @p1=NUMBER,@p2=ROW_COUNT; select @p1 AS ERROR_COUNT,@p2 as ROW_COUNT;
5. 文字檔案資料與表結構存在過大的差異或資料需要做清洗轉換,建議還是用專業的 ETL 工具或先粗 略匯入 MySQL 中再進行加工轉換處理。