我是否必須手動查看輸出並嘗試確定每個字段int new SQL table?有沒有使用這個輸出的快捷方式?
一個好的文本編輯器是你最好的朋友。以下是我在一兩分鐘,想出了對TextMate
CREATE TABLE import_table
(
nim_event_id INT,
nim_event_data_type_id INT,
nim_event_type_id INT,
event_timestamp DATETIME,
event_gmt_timestamp DATETIME,
product_issue_id INT,
nim_title_id INT,
nim_issue_id INT,
nim_user_id INT,
record_id INT,
transaction_id INT,
dwell_minutes INT,
free_trial_flag CHAR(1),
unlimited_flag CHAR(1),
application_version_id VARCHAR(128),
device_guid CHAR(37),
device_info_id VARCHAR(128),
device_brand VARCHAR(128),
device_product VARCHAR(128),
device_type VARCHAR(128),
device_hardware VARCHAR(128),
device_manufacturer VARCHAR(128),
device_model VARCHAR(128),
device_os VARCHAR(128),
device_os_version VARCHAR(128),
device_display_info VARCHAR(128),
device_display_size VARCHAR(128),
device_display_orientation_id INT,
event_version INT,
source_create_timestamp DATETIME,
source_last_modified_timestamp DATETIME,
workflow_run_id INT,
etl_create_date DATETIME,
etl_update_date DATETIME,
source_file_id INT
);
我們能夠控制插入您可以利用會話變量和LOAD DATA INFILE
SET
子句值。例如。設置爲NULL
缺席值在你的表,你可以做
LOAD DATA INFILE '/tmp/data.txt'
INTO TABLE import_table
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n' -- or '\r\n' if the file has been created on a Windows machine
IGNORE 1 LINES -- skip the header line
(
@nim_event_id,
@nim_event_data_type_id,
@nim_event_type_id,
@event_timestamp,
@event_gmt_timestamp,
@product_issue_id,
@nim_title_id,
@nim_issue_id,
@nim_user_id,
@record_id,
@transaction_id,
@dwell_minutes,
@free_trial_flag,
@unlimited_flag,
@application_version_id,
@device_guid,
@device_info_id,
@device_brand,
@device_product,
@device_type,
@device_hardware,
@device_manufacturer,
@device_model,
@device_os,
@device_os_version,
@device_display_info,
@device_display_size,
@device_display_orientation_id,
@event_version,
@source_create_timestamp,
@source_last_modified_timestamp,
@workflow_run_id,
@etl_create_date,
@etl_update_date,
@source_file_id
)
SET
nim_event_id = NULLIF(@nim_event_id, ''),
nim_event_data_type_id = NULLIF(@nim_event_data_type_id, ''),
nim_event_type_id = NULLIF(@nim_event_type_id, ''),
event_timestamp = NULLIF(@event_timestamp, ''),
event_gmt_timestamp = NULLIF(@event_gmt_timestamp, ''),
product_issue_id = NULLIF(@product_issue_id, ''),
nim_title_id = NULLIF(@nim_title_id, ''),
nim_issue_id = NULLIF(@nim_issue_id, ''),
nim_user_id = NULLIF(@nim_user_id, ''),
record_id = NULLIF(@record_id, ''),
transaction_id = NULLIF(@transaction_id, ''),
dwell_minutes = NULLIF(@dwell_minutes, ''),
free_trial_flag = NULLIF(@free_trial_flag, ''),
unlimited_flag = NULLIF(@unlimited_flag, ''),
application_version_id = NULLIF(@application_version_id, ''),
device_guid = NULLIF(@device_guid, ''),
device_info_id = NULLIF(@device_info_id, ''),
device_brand = NULLIF(@device_brand, ''),
device_product = NULLIF(@device_product, ''),
device_type = NULLIF(@device_type, ''),
device_hardware = NULLIF(@device_hardware, ''),
device_manufacturer = NULLIF(@device_manufacturer, ''),
device_model = NULLIF(@device_model, ''),
device_os = NULLIF(@device_os, ''),
device_os_version = NULLIF(@device_os_version, ''),
device_display_info = NULLIF(@device_display_info, ''),
device_display_size = NULLIF(@device_display_size, ''),
device_display_orientation_id = NULLIF(@device_display_orientation_id, ''),
event_version = NULLIF(@event_version, ''),
source_create_timestamp = NULLIF(@source_create_timestamp, ''),
source_last_modified_timestamp = NULLIF(@source_last_modified_timestamp, ''),
workflow_run_id = NULLIF(@workflow_run_id, ''),
etl_create_date = NULLIF(@etl_create_date, ''),
etl_update_date = NULLIF(@etl_update_date, ''),
source_file_id = NULLIF(@source_file_id, '')
;
您可以進一步調整它根據需要。上述語句成功加載自己張貼
這裏有兩個樣本行的結果:
+--------------+------------------------+-------------------+---------------------+---------------------+------------------+--------------+--------------+-------------+-----------+----------------+---------------+-----------------+----------------+------------------------+--------------------------------------+----------------+--------------+----------------+-------------+-----------------+---------------------+--------------+-----------+-------------------+---------------------+----------------------------------------------------------------------+-------------------------------+---------------+-------------------------+--------------------------------+-----------------+---------------------+---------------------+----------------+
| nim_event_id | nim_event_data_type_id | nim_event_type_id | event_timestamp | event_gmt_timestamp | product_issue_id | nim_title_id | nim_issue_id | nim_user_id | record_id | transaction_id | dwell_minutes | free_trial_flag | unlimited_flag | application_version_id | device_guid | device_info_id | device_brand | device_product | device_type | device_hardware | device_manufacturer | device_model | device_os | device_os_version | device_display_info | device_display_size | device_display_orientation_id | event_version | source_create_timestamp | source_last_modified_timestamp | workflow_run_id | etl_create_date | etl_update_date | source_file_id |
+--------------+------------------------+-------------------+---------------------+---------------------+------------------+--------------+--------------+-------------+-----------+----------------+---------------+-----------------+----------------+------------------------+--------------------------------------+----------------+--------------+----------------+-------------+-----------------+---------------------+--------------+-----------+-------------------+---------------------+----------------------------------------------------------------------+-------------------------------+---------------+-------------------------+--------------------------------+-----------------+---------------------+---------------------+----------------+
| 103008184 | 879 | 883 | 2013-12-07 00:30:35 | 2013-12-07 05:30:35 | -1 | 4800010 | 36000003 | 57700008 | 61906389 | 21385144 | 0 | Y | Y | NULL | 4c22c4a3-094a-476f-9a48-c67b15c2d3f7 | -2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 887 | 2 | 2013-12-06 21:35:39 | 2013-12-06 21:35:39 | NULL | 2013-12-07 03:31:34 | 2013-12-07 03:31:34 | NULL |
| 98500241 | 875 | 880 | 2013-12-07 15:43:37 | 2013-12-07 20:43:37 | -1 | -2 | -2 | 82700012 | 63226292 | NULL | NULL | N | N | -i6.1.3-APPLE-3.10.0 | 45F4BCE1-A6DC-4A66-A071-7D628FC7522E | 21 | Apple | iPhone OS | iPad | iPad | Apple | iPad | iOS | 6.1.3 | iPad | DisplayMetrics{width=768.000000, height=1024.000000, scale=2.000000} | 887 | 2 | 2013-12-07 12:45:50 | 2013-12-07 12:45:50 | NULL | 2013-12-07 19:31:34 | 2013-12-07 19:31:34 | NULL |
+--------------+------------------------+-------------------+---------------------+---------------------+------------------+--------------+--------------+-------------+-----------+----------------+---------------+-----------------+----------------+------------------------+--------------------------------------+----------------+--------------+----------------+-------------+-----------------+---------------------+--------------+-----------+-------------------+---------------------+----------------------------------------------------------------------+-------------------------------+---------------+-------------------------+--------------------------------+-----------------+---------------------+---------------------+----------------+
快速僅供參考 - 這會更普遍被稱爲是管道分隔 – Dan
它是一個數據庫轉儲或表傾倒?你能讀/查看文件的尾部或頭部嗎? – crnlx
@crnlx我有大約10個表,我必須作爲數據庫加載。我打算把每一個單獨做成一張桌子。你是說有一種方法可以將一組文本文件作爲數據庫導入,其中每個文本文件都是一個表格? –