首页 > > > python与hive
Hive算是大数据数据仓库的事实标准吧。Hive可以方法HDFS和Hbase上的数据,impala、spark sql、Presto完全能读取hive建立的数据仓库了的数据。一般情况在批处理任务中还在使用Hive,而在热查询做数据展示中大量使用impala、spark sql或Presto。
Hive提供三种访问接口:Cli,web Ui,HiveServer2。
使用python访问Hive则有两种主要的方式:
本文主要介绍Hive自带的python库和pyhive模块
注意最好在linux下进行测试,在windows下sasl安装不成功有编译错误。
pip install sasl
pip install thrift
pip install thrift-sasl
pip install pyhive# !/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 2018.3.6
@author: laofeng
'''
from pyhive import hive # or import hive
#第一个步,连接到hive
conn = hive.connect(host='172.16.182.82', port=10000,username='root',database='gdl')
#第二步,建立一个游标
cursor = conn.cursor(arraysize=100)
#第三部,执行查询语句
cursor.execute('SELECT * FROM gdl.gdl_cust_info_all limit 20')
#第四步,获取结果集(如果有)
#获取一行数据
print cursor.fetchone()
#获取所有行数据
print cursor.fetchall()
# 返回的查询结果的metadata
#获取结果的meta,字段列表(包括数据类型)
print cursor.description
#返回当前的行号
print cursor.rownumber
#第五步,关闭游标
cursor.close()
#第六步,关闭连接
conn.close()pyhive执行sql的api有两个,cursor.execute(sql, parameters=None), cursor.executemany(sql, [[]])
使用params参数,执行operation。params可以是单个值,元组,列表或者字典。
使用多组params参数,执行operation,只有最后一个执行的结果集会返回。
注意sql只有一个,参数有多组(params必须元祖或列表),循环params的每个元素(元素可以是单个值,元组或字典),每个元素都要和sql匹配,执行一次。
由游标获取数据,cursor.next() ,cursor.fetchone(), cursor.fetchmany(),cursor.fetchall()
返回当前查询结果集中的下一条数据,如果到达结果集结尾会抛出StopIteration的异常
返回当前查询结果集中的下一条数据,如果到达结果集的尾部返回None
返回指定数量的查询结果,如果没有给出size参数,则使用cursor.arraysize(默认值是1000行).如果结果集数据条数
返回查询结果的余下的所有数据,使用此函数要评估数据量,可能会因为数据量太大,导致本地程序内存问题。
每一行数据是一个元组,按照sql列的顺序排列数据。
获取hive执行的日志,输出内容如下:
INFO : Number of reduce tasks is set to 0 since there's no reduce operator
INFO : number of splits:12
INFO : Submitting tokens for job: job_1517024603417_67495
INFO : The url to track the job: http://M21-36-182.82.hadoop:8088/proxy/application_1517024603417_67495/
INFO : Starting Job = job_1517024603417_67495, Tracking URL = http://M21-36-182.82.hadoop:8088/proxy/application_1517024603417_67495/
INFO : Kill Command = /opt/cloudera/parcels/CDH-5.6.1-1.cdh5.6.1.p0.3/lib/hadoop/bin/hadoop job -kill job_1517024603417_67495
INFO : Hadoop job information for Stage-1: number of mappers: 12; number of reducers: 0
INFO : 2018-03-06 10:19:57,429 Stage-1 map = 0%, reduce = 0%
INFO : 2018-03-06 10:20:09,688 Stage-1 map = 21%, reduce = 0%, Cumulative CPU 52.97 sec
INFO : 2018-03-06 10:20:12,749 Stage-1 map = 34%, reduce = 0%, Cumulative CPU 90.87 sec
INFO : 2018-03-06 10:20:13,769 Stage-1 map = 35%, reduce = 0%, Cumulative CPU 100.04 sec
INFO : 2018-03-06 10:20:15,808 Stage-1 map = 44%, reduce = 0%, Cumulative CPU 119.3 sec
INFO : 2018-03-06 10:20:17,848 Stage-1 map = 45%, reduce = 0%, Cumulative CPU 122.07 sec
INFO : 2018-03-06 10:20:18,869 Stage-1 map = 57%, reduce = 0%, Cumulative CPU 142.32 sec
INFO : 2018-03-06 10:20:19,892 Stage-1 map = 59%, reduce = 0%, Cumulative CPU 144.48 sec
INFO : 2018-03-06 10:20:20,926 Stage-1 map = 62%, reduce = 0%, Cumulative CPU 149.68 sec
INFO : 2018-03-06 10:20:21,950 Stage-1 map = 72%, reduce = 0%, Cumulative CPU 168.17 sec
INFO : 2018-03-06 10:20:23,998 Stage-1 map = 75%, reduce = 0%, Cumulative CPU 174.58 sec
INFO : 2018-03-06 10:20:25,022 Stage-1 map = 86%, reduce = 0%, Cumulative CPU 192.39 sec
INFO : 2018-03-06 10:20:26,049 Stage-1 map = 87%, reduce = 0%, Cumulative CPU 193.26 sec
INFO : 2018-03-06 10:20:27,069 Stage-1 map = 90%, reduce = 0%, Cumulative CPU 199.28 sec
INFO : 2018-03-06 10:20:28,090 Stage-1 map = 94%, reduce = 0%, Cumulative CPU 206.96 sec
INFO : 2018-03-06 10:20:29,108 Stage-1 map = 95%, reduce = 0%, Cumulative CPU 208.52 sec
INFO : 2018-03-06 10:20:30,127 Stage-1 map = 98%, reduce = 0%, Cumulative CPU 214.14 sec
INFO : 2018-03-06 10:20:32,162 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 217.24 sec
INFO : MapReduce Total cumulative CPU time: 3 minutes 37 seconds 240 msec
INFO : Ended Job = job_1517024603417_67495当读取数据的条数,☞使用cursor.netx(),cursor.fetchone(),cursor.fetchall(),cursor.fetchmany()等方法。
当前还没有实现,返回-1
查询结果集的元数据,返回结果集有多少列,每个列的列名,数据类型等数据。
结果是一个列表,每个元素是长度为7的tuple。tuple值的内容如下:
取消当前执行的sql
关闭当前游标
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/818/viewspace-3166378/ ,如需转载,请注明出处,否则将追究法律责任。