1、建表語句
create table test(name string, age int, sex string) row format delimited fields terminated by '|' stored as textfile;
2、數據准備
lairin|20|男 peter|19|男 ena|1|女
3、加載數據
load data local inpath '/home/test/test.txt' overwrite into table test;
4、編寫transform腳本
''' @author: lairin ''' import sys separator = sys.argv[1] for line in sys.stdin: value = line.split() print separator.join(value)
5、使用 hive -e 'sql' > result.data 進行數據導出
hive -e " add file /home/test/demo.py; select transform(u.name, u.age, u.sex) using 'python demo.py ^' as (all) from (select * from test) as u; " > result.data
注: using 字句后面執行的python腳本的第一個參數 ^ 為分隔符
導出的結果:
lairin^20^男 peter^19^男 ena^1^女