python和hive结合使用sorubbish新浪博客 http://blog.sina.com.cn/s/blog_81e6c30b0101a3ju.html
基于hive的transform实现自定义分隔符数据导出 – 奏乐 – 博客园 http://www.cnblogs.com/hankedang/p/5650149.html
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 sysseparator = 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脚本的第一个参数 ^ 为分隔符
导出的结果:
1
2
3
lairin20男
peter19男
ena1女