redis的相关操作
1 连接redis
from rediscluster import StrictRedisCluster
from config import REDIS_NODES
redis = StrictRedisCluster(startup_nodes=REDIS_NODES, max_connections=len(REDIS_NODES), decode_responses=True, password='password')
# 其中REDIS_NODES=[{'host': i.split(':')[0], 'port': int(i.split(':')[1])} for i in nodes.split(',')]
# nodes的格式为nodes = 172.1.4.8:6666,171.3.7.7:3333
2 往redis写入数据(批量写入数据)
pipe = self.redis.pipeline()
# table为表名,key(update_time)为表中的key值,最后一个参数为key值对应的value值
pipe.hset(table, "key", json.dumps(rec_sku, separators=(',', ':')))
pipe.hset(table, 'update_time', int(time.time()))
# the old connection maybe closed by server
redis.connection_pool.disconnect()
pipe.execute()
3 读取redis表中的数据
recall_skus = redis.hget(table, "key")
MongoDB相关操作
1 连接MongoDB
import pymongo
m_conn_dev = pymongo.MongoClient('localhost', maxPoolSize=10)
m_db_dev = m_conn_dev.get_database('database')
m_db.authenticate('username', 'password') # 如果没有设置账号密码,可省略这一行
2 MongoDB写入数据,批量写入数据
from pymongo import ReplaceOne
bulk_requests = [ReplaceOne({'_id': id_}, {'info': dict(info), 'update_time': update_time}, upsert=True) for id_, info in id_info.items()]
if bulk_requests: # 写入
m_db_dev.get_collection(table).bulk_write(bulk_requests)
3 从MongoDB读取数据
m_db.get_collection(table).find()
4 插入数据和更新一条数据
m_db_dev.get_collection(table).insert_many(insert_data, ordered=False)
m_db_dev.get_collection(table).update_one({'_id': "id}, {'$set': {'date': update_time}}, True)
mysql的相关操作
连接查询数据
# 打开数据库连接
mysql_db = pymysql.connect("172.8.0.2", "user", "password", "database")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = mysql_db.cursor()
# 使用 execute()方法执行 SQL 查询
cursor.execute(sql)
# 使用 fetchone() 方法获取单条数据
data = cursor.fetchone() # 获取一条数据
# 关闭数据库连接
mysql_db.close()
hive相关操作
创建表,修改表字段,往hive插入数据等操作参考另外一篇博客的详细介绍:https://blog.csdn.net/qq_35549634/article/details/109404756
1 hive的常用操作定义
from pyhive import hive
class db_hive(object):
def __init__(self):
self.cursor = hive.Connection(host="172.1.1.2", port=10000, username='username', auth='user', password='password').cursor()
self.cursor.execute("set mapreduce.job.queuename=queue:a.default") # 设置队列(不同队列速度不同),可删除注释掉
self.arraysize = 1000000
def excute_sql(self,sqlstr):
"""
执行sql查询语句
:param sqlstr:
:return:
"""
self.cursor.execute(sqlstr)
@property
def fetchall(self):
"""
获取hive表全量数据
:return:
"""
return self.cursor.fetchall()
def fetchmany(self,arraysize=None):
"""
迭代器批量获取数据
:param arraysize:
:return:
"""
if arraysize:
self.arraysize=arraysize
return self.__iter__()
def __next__(self):
"""
迭代器修改
:return:
"""
dataccache=self.cursor.fetchmany(self.arraysize)
if not dataccache:
raise StopIteration
return dataccache
def __iter__(self):
return self
2 查询获取数据
test=db_hive()
test.excute_sql('sql')
for x in test.fetchmany(1):
for y in x:
print(y)