技术标签: hive python技巧 mysql 数据库 数据挖掘
记录几种常见的python连接数据库的代码,方便用的时候直接取。
1.python连接mysql
import pymysql
import pprint
#
# HOST = '127.0.0.1'
# PORT = 3306
# USER = 'root'
# PASSWD = 'root'
# dbName = 'mysql'
def conn_mysql(host,port,user,passwd,dbname):
conn = pymysql.connect(host=host,
port=port,
user=user,
passwd=passwd,
db=dbname,
charset='utf8')
cursor = conn.cursor()
# def show_tables():
sql = 'show tables;'
cursor.execute(sql)
rowList = cursor.fetchall()
tableList = list()
for row in rowList:
tableList.append(row[0])
print('tableList(%d):\n%s\n' % (len(tableList), pprint.pformat(tableList, indent=4)))
# return tableList
2.python连接hive
from pyhive import hive
import pandas as pd
ip=''
conn = hive.connect(ip)
def runhql(sql):
cursor = conn.cursor()
cursor.execute(sql)
logs = cursor.fetch_logs()
datas = cursor.fetchall()
print(len(datas))
return datas
3.python连接德鲁伊库
from pydruid.db import connect
def find_druid(ip,port,sql):
conn = connect(host=ip, port=port)
curs = conn.cursor()
curs.execute(sql)
df = pd.DataFrame(curs.fetchall())
return df
4.python连接sqlsever
import pymssql
import pprint
def conn_sqlserver(host,port,user,password,database):
port = str(port)
host = host+':'+port
connect = pymssql.connect(host=host, user=user, password=password,
database=database, charset='utf8')
if connect:
print("连接成功!")
cursor = connect.cursor() # 创建一个游标对象,python里的sql语句都要通过cursor来执行
cursor.execute("select name from sysobjects where xtype='U'") # 执行sql语句,获取数据库中的表名
rowList = cursor.fetchall()
tableList = []
for row in rowList:
tableList.append(row[0])
print('tableList(%d):\n%s\n' % (len(tableList), pprint.pformat(tableList, indent=4)))
# 处理每个表
for tabName in tableList:
print('table %s ...' % tabName)
sql = "select name from syscolumns where id = object_id('%s')"
sql = sql % (tabName)
cursor.execute(sql)
rowList = cursor.fetchall()
fieldList = list()
for row in rowList:
fieldList.append(row[0])
print('fieldList(%d):\n%s\n' % (len(fieldList), pprint.pformat(fieldList, indent=4)))
cursor.close() # 关闭游标
connect.close() # 关闭连接
5.python连接oracle
import cx_Oracle
import pprint
def conn_oracle(host,port,user,passwd,dbname,sql):
port = str(port)
host_port = user+'/'+passwd+'@'+host+':'+port+'/'+dbname
conn = cx_Oracle.connect("%s" %host_port)
cursor = conn.cursor()
# 获取当前用户下的所有表的信息
# def conn_oracle():
results = cursor.execute(sql)
# 获取所有数据
all_data = cursor.fetchall()
tabList = []
for data in all_data:
tabList.append(data[0])
print('tabList(%d):\n%s\n' % (len(tabList), pprint.pformat(tabList, indent=4)))
# 处理每个表
for tabName in tabList:
print('table %s ...' % tabName)
sql = 'select * from "%s" '
sql = sql % (tabName)
cursor.execute(sql)
rowList = cursor.fetchall()
title = [i[0] for i in cursor.description]
print(title)
# fieldList = list()
# for row in rowList:
# fieldList.append(row[0])
# print('fieldList(%d):\n%s\n' % (len(fieldList), pprint.pformat(fieldList, indent=4)))
cursor.close()
conn.close()
6.python 连接impala查询:
from impala.dbapi import connect
ip=''
port=''
sql=''
conn = connect(host=ip, port=port)
cur = conn.cursor()
cur.execute(sql)
data_list=cur.fetchall()
for data in data_list:
print data
文章浏览阅读748次。总结起来大概有5种做法:将要处理的文字写到一个资源文件,如string.xml(使用html用法格式化)当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,如果是所有的类型都出来就是**android:autoLink="all",当然也可以在java代码里 做,textView01.setAutoLinkMask(Li..._qaction::settext 无法添加下划线
文章浏览阅读6.3k次,点赞2次,收藏10次。摘要: 背景 随着近几年物联网的发展,时序数据迎来了一个不小的爆发。从DB-Engines上近两年的数据库类型增长趋势来看,时序数据库的增长是非常迅猛的。在去年我花了比较长的时间去了解了一些开源时序数据库,写了一个系列的文章(综述、HBase系、Cassandra系、InfluxDB、Prometheus),感兴趣的可以浏览。背景随着近几年物联网的发展,时序数据迎来了一个不小的爆发。从DB..._tablestore 时间类型处理
文章浏览阅读5.7k次,点赞8次,收藏49次。可以编译成功但是运行时段错误查找原因应该是ROS noetic版本中自带的OpenCV4和VINS-mono中需要使用的OpenCV3冲突的问题。为了便于查找问题,我只先编译feature_tracker包。解决思路历程:o想着把OpenCV4相关的库移除掉,但是发现编译feature_tracker的时候仍然会关联到Opencv4的库,查找原因是因为cv_bridge是依赖opencv4的,这样导致同时使用了opencv3和opencv4,因此运行出现段错误。oo进一步想着(1)把vins-mon_uabntu20.04安装vins-mono
文章浏览阅读3.6k次,点赞3次,收藏12次。创龙TL6748开发板中,EMIFA模块使用默认的PLL0_SYSCLK3时钟,使用AISgen for D800K008工具加载C6748配置文件C6748AISgen_456M_config(Configuration files,在TL_TMS6748/images文件夹下),由图可以看到DIV3等于4,注意这里的DIV3就是实际的分频值(x),而不是写入相应PLL寄存器的值(x-1)。_tms 6748
文章浏览阅读5.9k次,点赞4次,收藏13次。转载请说明出处:eigen稀疏矩阵拼接(块操作)eigen稀疏矩阵拼接(块操作)关于稀疏矩阵的块操作:参考官方链接 However, for performance reasons, writing to a sub-sparse-matrix is much more limited, and currently only contiguous sets of columns..._稀疏矩阵拼接
文章浏览阅读946次,点赞19次,收藏19次。波束形成是天线阵列信号处理中的一项关键技术,它通过对来自不同方向的信号进行加权求和,来增强特定方向的信号并抑制其他方向的干扰。本文介绍了两种基于 Capon 和信号子空间的变形算法,即最小方差无失真响应 (MVDR) 算法和最小范数算法,用于实现波束形成。这些算法通过优化波束形成权重向量,来最小化波束形成输出的方差或范数,从而提高波束形成性能。引言波束形成在雷达、声纳、通信和医学成像等众多应用中至关重要。它可以增强目标信号,抑制干扰和噪声,提高系统性能。
文章浏览阅读3.4w次。转自:http://www.linuxidc.com/Linux/2017-07/145335.htm使用Ubuntu开发已经有些时间了。写下这篇文章,希望记录下这一年的小小总结。使用Linux开发有很多坑,同时也有很多有趣的东西,可以编写一些自动化脚本,添加定时器,例如下班定时关机等自动化脚本,同时对于服务器不太了解的朋友,建议也可以拿台Linux来实践下,同时Ubuntu在Androi_ubuntu开发推荐软件
文章浏览阅读2.2k次。一,问题 nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递到应用程序中。二,解决 Nginx服务器增加转发配置 proxy_set_header Host $host;_nginx获取到的是交换机的ip
文章浏览阅读1.4k次。Wireshark TCP数据包跟踪 还原图片 WinHex简单应用 _wireshark抓包还原图片
文章浏览阅读1.5k次。Win8下安装VS2012时,蓝屏,报错WHEA_UNCORRECTABLE_ERROR(P.S.新的BSOD挺有创意":("),Google之,发现[via]需要BIOS中禁用Intel C-State,有严重Bug的嫌疑哦原因有空再看看..._win8.1 whea_uncorrectable_error蓝屏代码
文章浏览阅读919次,点赞21次,收藏22次。科大讯飞是一家专业从事智能语音及语音技术研究、软件及芯片产品开发、语音信息服务的软件企业,语音技术实现了人机语音交互,使人与机器之间沟通变得像人与人沟通一样简单。语音技术主要包括语音合成和语音识别两项关键技术。此外,语音技术还包括语音编码、音色转换、口语评测、语音消噪和增强等技术,有着广阔的应用。_科大讯飞培训案例
文章浏览阅读4.7k次。Perl是一个高阶程式语言,由 Larry Wall和其他许多人所写,融合了许多语言的特性。它主要是由无所不在的 C语言,其次由 sed、awk,UNIX shell 和至少十数种其他的工具和语言所演化而来。Perl对 process、档案,和文字有很强的处理、变换能力,ActivePerl是一个perl脚本解释器。其包含了包括有 Perl for Win32、Perl for ISAPI、PerlScript、Perl。_perl下载