技术标签: 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
文章浏览阅读51次。算了还是给个链接吧 他写的比我的好https://www.cnblogs.com/zyaizz/p/3605103.htmlhttps://blog.csdn.net/u012289441/article/details/45154401?ref=myread楼下这个写得更好鉴定完毕转载于:https://www.cnblogs.com/oushiyang/p/8316146.html...
文章浏览阅读315次。浅谈后端语言优缺点 兄弟连教育在前不久,回答了一个关于后端语言选型的问题,那么后端语言都有哪些,存在什么样的优缺点?因此兄弟连与大家分享一下心得。 语言优缺点..._后端开发语言优缺点
文章浏览阅读3.4k次。辅助namenode上的resourcemanager通过start-all.sh可能启动不了,需要进入辅namenode上手动启动(/opt/app/hadoop/sbin/hadoop-daemon.sh startresourcemanager)/opt/app/zookeeper-3.4.6/bin/zkServer.sh start(启动)/opt/app/hadoop/sbin/start-all.sh(启动)/opt/app/hadoop/sbin/start-all.sh(停止)_hadoop集群启动命令
文章浏览阅读718次。打开别人的工程提示IAR版本不兼容问题IAR 版本不兼容问题IAR 版本不兼容问题我使用的是 STM32CUBEMX 生成的IAR工程,我自己的是 IAR 版本 8.4,打开别人的工程 8.3版本,但是提示我打开的工程版本更高,可能是IAR的一个bug吧,把cube 生成的 IAR 工程删除 然后再重新生成一个工程就好了。..._win7 iar 8.4无法加载工程
文章浏览阅读4.8w次,点赞89次,收藏590次。使用 VMware Workstation 16 Pro 安装 Ubuntu 虚拟机 - 完整教程_vmware安装ubuntu
文章浏览阅读116次。在2016中国互联网大会之中国WiFi应用创新大会上,中国互联网协会开发者核心价值小组秘书长于立娟发表了精彩的开幕致辞,她指出随着移动上网终端的多元化,WiFi应用已经成为移动互联网时代继浏览器、超级APP之后的又一大超级入口。 于立娟表示,“WiFi如果仅仅停留在底层入口,就会像3G...
文章浏览阅读2.1k次,点赞2次,收藏21次。本文主要简单介绍了朴素贝叶斯的基本概念,优缺点,应用场景,建模时的注意事项,模型的评价指标,以及朴素贝叶斯在解决分类任务时的python实现方法和示例等。_朴素贝叶斯
文章浏览阅读1.6k次,点赞3次,收藏9次。HashMap是一种非常常见和实用的数据结构,它被广泛应用于Java编程中。在本文中,我们将深入探讨HashMap的工作原理、实现方式和使用案例,以帮助读者更好地理解和应用这一数据结构。_java中hashmap
文章浏览阅读591次,点赞26次,收藏8次。主要概括了hotspot的gc机制
文章浏览阅读2.9k次,点赞3次,收藏10次。病毒分析之撒旦(Satan)勒索病毒分析解密(AES256 ECB算法)0x0病毒概况撒旦勒索病毒主要是针对企业服务器用户进行感染加密其服务器上的文件并勒索用户的一种病毒。撒旦病毒通过大量漏洞利用工具,扫描入侵主机。成功入侵后加密重要文件。并生成文件用中英韩三种语言提示索要比特币赎金解密文件。0x1恶意行为1.主模块会释放大量攻击模块并执行,也会连接恶意IP下载病毒文件..._撒旦查询恶意
文章浏览阅读932次,点赞7次,收藏8次。pwd=5npl 提取码:5npl。找到“软件和更新”,选择aliyun服务器,——>【关闭】——>【重新载入】,等待进度条完毕就更管成功。出现“Configuring incomplete, errors occurred!",这是由于没有将软件源改为国内软件源,如果意外退出下载,第二次进入下载的时候缓存会被锁住。在主目录下打开终端,运行以下命令,_ubuntu 24.04 ppeizhi
文章浏览阅读3.2w次,点赞18次,收藏58次。在遇到上述错误自己无解决时,于是开始了网上大搜索:git push -u origin master 上面命令将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push了首先尝试了 命令git push -u origin master -f,然后惊喜地出现了下面错误,不能强制推送到受保护的分支随后就又找到了一个方法:先使用..._efs/heads/master:refs/heads/master[rejected] (non-fast-forward)