技术标签: 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
文章浏览阅读7.1k次,点赞3次,收藏30次。SSH 是 Linux 下进行远程连接的基本工具,但是如果仅仅用它来登录那可是太浪费啦!SSH 命令可是完成远程操作的神器啊,借助它我们可以把很多的远程操作自动化掉!下面就对 SSH 的远程操作功能进行一个小小的总结。远程执行命令如果我们要查看一下某台主机的磁盘使用情况,是不是必须要登录到目标主机上才能执行 df 命令呢?当然不是的,我们可以使用 ssh 命令在远程的主机上执行 df 命令,然后直接把结果显示出来。整个过程就像是在本地执行了一条命令一样:$ ssh [email protected]_ssh 执行命令
文章浏览阅读9.4k次。mtools 是一组非常好用的 MongoDB 日志分析工具_mtools mongo3.4
文章浏览阅读673次。环境配置先放官网地址:ijkplayer此次编译是在ubuntu下编译的,需要下载VMware虚拟机和ubuntu系统。 编译ijkplayer时是需要SDK和NDK的(linux版本),SDK和NDK环境需要在ubuntu上配置好。其实这些文件是可以在windows上下载好,然后复制到ubuntu上的,但不能直接复制,需要下载WinSCP和PuTTY(它的主要功能就是在本地与远程计算机间安全的复_ijkplayer android ssl
文章浏览阅读300次。String dateStr = “Fri Feb 19 17:32:34 CST 2021”;SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);Date date = new Date();try{ date = sdf.parse(dateStr); }catch (Exception e){ e.printStackTrace();}String_java string转date
文章浏览阅读170次。display: -webkit-box;text-overflow: ellipsis;overflow: hidden;-webkit-box-orient: vertical;-webkit-line-clamp: 2;就五行代码效果如下_css超过五行后打省略
文章浏览阅读249次。要收集整理技术资源,可以尝试以下几个步骤:确定需求:首先明确自己需要什么样的技术资源,比如编程语言、软件工具、编程框架等。寻找来源:可以通过各种途径寻找技术资源,比如搜索引擎、GitHub、技术论坛、社交媒体等。筛选资源:在找到的资源中,筛选出最适合自己的资源,比如看其适用性、评价、更新频率等。分类整理:将筛选出的资源按照不同分类进行整理,比如按照编程语言、功能模块、难度等。存..._先进的技术手段收集整理
文章浏览阅读1w次,点赞7次,收藏48次。MCS-51单片机内的中断系统主要用于实时测控,即要求单片机能及时地响应和处理单片机外部或内部事件所提出的中断请求。由于这些中断请求都时随机发出的,如果采用定时查询方式来处理这些中断请求,则单片机的工作效率低,且得不到实时处理。因此,MCS-51单片机要实时处理这些中断请求,就必须采用具有中断处理功能的部件——中断系统来完成。* 为什么用中断,因为定时查询的效率低。(注意这里讲的定时查询不是定时器查询)1 中断的概念 当MCS-51单片机的CPU正在处理某件事情(..._mcs51单片机中断优先级寄存器
文章浏览阅读1.3w次,点赞50次,收藏318次。利用爬虫爬取天气数据,并且进行数据清理,之后进行模型预测,预测明天气温还进行了数据图的绘制。_利用python爬取网站上面的历史天气,并且使用正则表达式针对于网站上面的数据进行
文章浏览阅读4.3w次,点赞51次,收藏441次。通过wx.login获取 临时登录凭证code,向后端换取token。 可以做到无感登录;过button按钮的bindgetphonenumber事件,弹出手机号授权,获取到加密数据后,向后端换取token;通过button按钮的click事件,调用 wx.getUserProfile() 弹出授权框,获取到用户加密数据后,向后端换取token。_微信小程序授权登录
文章浏览阅读1.4k次。引言百度慧眼迁徙3.0上线,在选择某一城市的“迁出目的地”或“迁入来源地”后,即可查看该城市迁出、迁入人口的迁徙来源与迁徙时间趋势。对城市大数据研究有所帮助。本文采取Python爬虫抓取百度慧眼数据。image准备工作工具:Chrome2.查找数据源。使用开发者工具(F12),打开Network,搜索关键字json。image右侧preview预览,打开data内的list,即可看到json格式的..._百度慧眼人口热力图数据爬取
文章浏览阅读6.5w次,点赞13次,收藏26次。欢迎加入我们的学习提升群523988350,里面有京东、美团网的技术人员,可以相互交流Activiti的流程 部署方式有很多种方式,我们可以根据activit工作流引擎提供的ap方式进行部署。当然了实际需求决定你要使用哪一种api操作,后面的总结详细介绍了使用场景。下面看一下部署方式。流程部署的方式在类org.activiti.engine.repository.Deplo_addclasspathresource
文章浏览阅读285次。不知不觉,已经到了9月。近几个月的找工作也快接近尾声了。能够找到心仪的企业已经是同学们最大的期望。我也不例外。今天算是一个阶段的转折,准备开始新一轮的努力。好好奋斗,一切为了自己,为了家人。秋天是一个可爱的季节,我经常能够看到北京美丽的晚霞。虽然这种晴朗的天气比较少,但偶尔的出现总会让你心动不已。人人都说程序猿不会说话,程序猿不懂的生活,只会写代码。我们要做懂生活,热爱生活的程序猿~今天,一篇《一个_brightlee什么意思