python连接redis(MongoDB, mysql,hive)操作,读取和写入数据

本文介绍了如何使用Python进行Redis、MongoDB及MySQL的操作,包括连接数据库、批量写入数据、读取数据等常见操作,并提供了Hive SQL查询的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值