当前位置:首页 » 编程语言 » pythonodbchive

pythonodbchive

发布时间: 2022-04-29 22:21:01

‘壹’ 如何用python链接hive

看你的需求应该是一个python新手,建议你看一下django这个python的web框架,它应该能满足你的需求,同时也能让你对python的web开发有一个了解。如果解决了您的问题请采纳!如果未解决请继续追问

‘贰’ python 怎么调用odbc

入门
连接到数据库
调用connect方法并传入ODBC连接字符串,其会返回一个connect对象。通过connect对象,调用cursor()方法,可以获取一个游标cursor。如下代码示例:
import pyodbc
#连接示例: Windows系统, 非DSN方式, 使用微软 sql Server 数据库驱动
cnxn =pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')
#连接示例: Linux系统, 非DSN方式, 使用FreeTDS驱动
cnxn =pyodbc.connect('DRIVER={FreeTDS};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass;TDS_Version=7.0')
#连接示例:使用DSN方式
cnxn = pyodbc.connect('DSN=test;PWD=password')
# 打开游标
cursor =cnxn.cursor()
以上示例只是标准示例,具体的ODBC连接字符串以你自己使用的驱动为准。
查询一些数据
所有SQL语句都使用Cursor.execute()方法执行。比如select语句会返回一些结果行,你可以使用游标(Cursor)相关的函数功能(fetchone,fetchall,fetchmany)对结果进行检索。
Cursor.fetchone 用于返回一个单行( Row)对象:
cursor.execute("selectuser_id, user_name from users")
row =cursor.fetchone()
if row:
print(row)
Row 对象是类似一个python元组(tuples),不过也可以通过列名称来访问,例如:
cursor.execute("selectuser_id, user_name from users")
row =cursor.fetchone()
print('name:',row[1]) # 使用列索引号来访问数据
print('name:',row.user_name) # 或者直接使用列名来访问数据
当所有行都已被检索,则fetchone返回None.
while 1:
row = cursor.fetchone()
if not row:
break
print('id:', row.user_id)
Cursor.fetchall方法返回所有剩余行并存储于一个列表中。如果没有行,则返回一个空列表。(注意:如果有很多行,会造成大量内存占用。Fetchall会一次性将所有数据查询到本地,然后再遍历)
cursor.execute("selectuser_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
print(row.user_id, row.user_name)
如果并不在意数据处理时间,可以使用光标本身作为一个迭代器,逐行迭代。这样可以节省大量的内存开销,但是由于和数据来回进行通信,速度会相对较慢:
cursor.execute("selectuser_id, user_name from users"):
for row in cursor:
print(row.user_id, row.user_name)
由于Cursor.execute总是返回游标(cursor), 所以也可以简写成:
for row incursor.execute("select user_id, user_name from users"):
print(row.user_id, row.user_name)
我们可以在execut中使用”””三重引号,来应用多行SQL字符串。这样sql的可读性大大增强。这是python特有的特性:
cursor.execute(
"""
select user_id, user_name
from users
where last_logon < '2001-01-01'
and bill_overe = 1
""")
SQL参数
ODBC支持使用问号作为SQL的查询参数占位符。可以在execute方法的SQL参数之后,提供SQL参数占位符的值:
cursor.execute(
"""
select user_id, user_name
from users
where last_logon < ?
and bill_overe = ?
""", '2001-01-01', 1)
这样做可以防止SQL注入攻击,提高安全性。如果使用不同的参数反复执行相同的SQL它效率会更高,这种情况下该SQL将只预装(prepared )一次。(pyodbc只保留最后一条编写的语句,所以如果程序在语句之间进行切换,每次都会预装,造成多次预装。)
Python的DB API指定参数应以序列(sequence)对象传递,所以pyodbc也支持这种方式:
cursor.execute(
"""
select user_id, user_name
from users
where last_logon < ?
and bill_overe = ?
""", ['2001-01-01', 1])
插入数据
插入数据使用相同的函数 - 通过传入insert SQL和相关占位参数执行插入数据:
cursor.execute("insertinto procts(id, name) values ('pyodbc', 'awesome library')")
cnxn.commit()
cursor.execute("insertinto procts(id, name) values (?, ?)", 'pyodbc', 'awesome library')
cnxn.commit()
注意:调用cnxn.commit()。发成错误可以回滚。具体需要看数据库特性支持情况。如果数据发生改变,最好进行commit。如果不提交,则在连接中断时,所有数据会发生回滚。
更新和删除数据
更新和删除工作以同样的方式:通过特定的SQL来执行。通常我们都想知道更新和删除的时候有多少条记录受到影响,可以使用Cursor.rowcount来获取值:
cursor.execute("deletefrom procts where id <> ?", 'pyodbc')
print('Deleted {}inferior procts'.format(cursor.rowcount))
cnxn.commit()
由于execute 总是返回游标(允许你调用链或迭代器使用),有时我们直接这样简写:
deleted =cursor.execute("delete from procts where id <> 'pyodbc'").rowcount
cnxn.commit()
注意一定要调用commit。否则连接中断时会造成改动回滚。
技巧和窍门
引号
于单引号SQL是有效的,当值需要使用单引号时,使用用双引号包围的SQL:
cursor.execute("deletefrom procts where id <> 'pyodbc'")
如果使用三重引号, 我们可以这样使用单引号:
cursor.execute(
"""
delete
from procts
where id <> 'pyodbc'
""")
列名称
Microsoft SQLServer之类的一些数据库不会产生计算列的列名,在这种情况下,需要通过索引来访问列。我们也可以使用sql列别名的方式,为计算列指定引用名称:
row =cursor.execute("select count(*) as user_count fromusers").fetchone()
print('{}users'.format(row.user_count)
当然也可以直接使用列索引来访问列值:
count =cursor.execute("select count(*) from users").fetchone()[0]
print('{}users'.format(count)
注意,上例的首列不能是Null。否则fetchone方法将返回None并且会报NoneType不支持索引的错误。如果有一个默认值,经常可以是ISNULL或合并:
maxid =cursor.execute("select coalesce(max(id), 0) fromusers").fetchone()[0]
自动清理
连接(默认)在一个事务中。如果一个连接关闭前没有提交,则会进行当前事务回滚。很少需要finally或except 语句来执行人为的清理操作,程序会自动清理。
例如,如果下列执行过程中任何一条SQL语句出现异常,都将引发导致这两个游标执行失效。从而保证原子性,要么所有数据都插入发生,要么所有数据都不插入。不需要人为编写清理代码。
cnxn =pyodbc.connect(...)
cursor = cnxn.cursor()
cursor.execute("insertinto t(col) values (1)")
cursor.execute("insertinto t(col) values (2)")
cnxn.commit()

‘叁’ hive中如何调用python函数

ADD FILE /home/taobao/dw_hive/hivelets/smoking/ext/tsa/hivesql/bjx_topic_t1/splitsysin.py.bak;
create table if not exists splittest_t1
(
topic_id string,
topic_title string,
topic_desc string,
biz_date string,
gmt_create string
) PARTITIONED BY(pt string)
row format delimited fields terminated by '\001'
lines terminated by '\n'
STORED AS textfile;

select TRANSFORM(topic_id,topic_title,topic_desc,biz_date,gmt_create)
USING 'splitsysin.py'
as topic_id,topic_title,topic_desc,biz_date,gmt_create
from r_bjx_dim_topic_t1;

‘肆’ windows下怎么用python连接hive数据库

#!/usr/bin/python2.7
#hive--servicehiveserver>/dev/null2>/dev/null&
#/opt/cloudera/parcels/CDH/lib/hive/lib/pyimportsys

#python与hiveserver交互
sys.path.append('C:/hadoop_jar/py')
fromhive_serviceimportThriftHive
fromhive_service.
fromthrift.transportimportTSocket
fromthriftimportThrift
fromthrift.transportimportTTransport
fromthrift.protocolimportTBinaryProtocol

if__name__=='__main__':
try:
socket=TSocket.TSocket('10.70.50.111',10000)
transport=TTransport.TBufferedTransport(socket)
protocol=TBinaryProtocol.TBinaryProtocol(transport)
client=ThriftHive.Client(protocol)
sql='select*fromtest'
transport.open()
client.execute(sql)
withopen('C:/Users/DWJ/Desktop/python2hive.txt','w')asout_file:
whileclient.fetchOne():
out_file.write(client.fetchOne())
transport.close()
exceptThrift.TException,tx:
print'%s'%(tx.message)

其中,C:/hadoop_jar/py里的包来自于hive安装文件自带的py,如:/opt/cloudera/parcels/CDH/lib/hive/lib/py,将其添加到python中即可。

热点内容
抖音电脑后台服务器中断 发布:2025-05-15 11:11:59 浏览:307
sql2008服务器 发布:2025-05-15 11:03:27 浏览:306
我的世界pe服务器创造 发布:2025-05-15 10:51:17 浏览:608
移动端打吃鸡要什么配置 发布:2025-05-15 10:48:16 浏览:756
我的世界哪五个服务器被炸了 发布:2025-05-15 10:36:16 浏览:994
ehcache存储对象 发布:2025-05-15 10:35:31 浏览:528
搭建虚拟电脑的服务器 发布:2025-05-15 10:29:31 浏览:270
湖人双核配置哪个最好 发布:2025-05-15 10:09:48 浏览:980
手机热点密码怎么查看 发布:2025-05-15 09:54:47 浏览:109
生意发力云存储 发布:2025-05-15 09:54:45 浏览:617