码迷,mamicode.com
首页 > 编程语言 > 详细

Hive 11、Hive嵌入Python

时间:2019-09-11 23:35:42      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:sys   一个   oca   strip()   类型   java   多列   用法   int   

Hive嵌入Python

Python的输入输出都是\t为分隔符,否则会出错,python脚本输入print出规定格式的数据

 

用法为先add file,使用语法为TRANSFORM (name, items)   USING ‘python test.py‘  AS (name string, item1 string,item2 string,item3 string),这里后面几个字段对应python的类型

 下面是一个将一列转成多列表小案例:

create table test (name string,items string) 

ROW FORMAT DELIMITED 

FIELDS TERMINATED BY ‘\t‘;

  

LOAD DATA local INPATH /opt/data/tt.txt OVERWRITE INTO TABLE test ;

tt.txt的内容:

tom	shu fa,wei qi,chang ge
jack	game,kan shu,shang wang
lusi	lv you,guang jie,gou wu

  表2:

create table test2 (name string,item1 string,item2 string,item3 string) 

ROW FORMAT DELIMITED 

FIELDS TERMINATED BY ‘\t‘;

  

-- 将python脚本上传到Hive
Hive> add file /root/test.py

  

-- 将结果放到test2中
INSERT OVERWRITE TABLE test2  

SELECT  TRANSFORM (name, items)  
USING ‘python test.py‘  
AS (name string, item1 string,item2 string,item3 string)  
FROM test;

  

#!/usr/bin/python  

import sys  
for line in sys.stdin:  
     line = line.strip()    
     name,it = line.split(‘\t‘)  
     count = it.count(‘,‘)+1
     for i in range(0,3-count):
          it = it+‘,NULL‘
     result = it.split(‘,‘)[0:3]
     print ‘%s\t%s‘%(name,‘\t‘.join(result))

  

结果:
-- 表1
hive> select * from test;
OK
tom    shu fa,wei qi,chang ge
jack    game,kan shu,shang wang
lusi    lv you,guang jie,gou wu
Time taken: 0.07 seconds, Fetched: 3 row(s)


 hive> desc test2;
 OK
 name                	string              	                    
 item1               	string              	                    
 item2               	string              	                    
 item3               	string              	                    
 Time taken: 0.141 seconds, Fetched: 4 row(s)
-- 表2
hive> select * from test2;
OK
tom    shu fa    wei qi    chang ge
jack    game    kan shu    shang wang
lusi    lv you    guang jie    gou wu
Time taken: 1.368 seconds, Fetched: 3 row(s)

  

Hive 11、Hive嵌入Python

标签:sys   一个   oca   strip()   类型   java   多列   用法   int   

原文地址:https://www.cnblogs.com/tesla-turing/p/11509344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!