[python]python和hive结合使用(hive的transform实现)

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脚本

《[python]python和hive结合使用(hive的transform实现)》 复制代码

”’@author: lairin”’import sysseparator = sys.argv[1]for line in sys.stdin: value = line.split() print separator.join(value)

《[python]python和hive结合使用(hive的transform实现)》 复制代码

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

    原文作者:葡萄喃喃呓语
    原文地址: https://www.jianshu.com/p/be3e772dc67c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞