当前位置:   article > 正文

Python脚本之操作Redis Cluster【二】_python rediscluster

python rediscluster

本文为博主原创,未经授权,严禁转载及使用。
本文链接:https://blog.csdn.net/zyooooxie/article/details/112484045

之前写过一篇 使用redis-py来操作redis集群, https://blog.csdn.net/zyooooxie/article/details/123760358 ,这期来分享下 使用redis-py-cluster;

【实际这篇博客推迟发布N个月】

个人博客:https://blog.csdn.net/zyooooxie

【以下所有内容仅为个人项目经历,如有不同,纯属正常】

redis-py-cluster

https://pypi.org/project/redis-py-cluster/

This major version of redis-py-cluster supports redis-py >=3.0.0, <4.0.0.

代码

"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""

import traceback

from rediscluster import ClusterConnectionPool
from rediscluster import RedisCluster

from xxx_test.user_log import Log


host2 = ''
p1wd = ''
port = 1234
gl_key_name = 'TEST_xie*'

Log.info('------')

gl_real_string = ''
gl_real_hash = ''
gl_real_list = ''
gl_real_set = ''
gl_no_exist = 'TEST_zyooooxie'

gl_test_str = 'test_str'
gl_test_hash = 'test_hash'
gl_test_list = 'test_list'
gl_test_set = 'test_set'

Log.info('------')


# pip install redis-py-cluster==2.1.3
# https://redis-py-cluster.readthedocs.io/en/2.1.3/index.html

def redis_py_cluster_connect_1():
    rc = RedisCluster(startup_nodes=[{'host': host2, 'port': port}],
                      decode_responses=True, password=pwd)

    Log.info('{}'.format(rc))
    Log.info(type(rc))

    Log.error('已连接')

    return rc


def redis_py_cluster_connect_2():
    ccp = ClusterConnectionPool(startup_nodes=[{'host': host2, 'port': port}],
                                decode_responses=True, password=pwd)

    rc = RedisCluster(connection_pool=ccp)

    Log.info('{}'.format(rc))
    Log.info(type(rc))

    Log.error('已连接')
    return rc


def redis_py_cluster_connect_3():
    rc = RedisCluster(host=host2, port=port,
                      decode_responses=True, password=pwd)

    Log.info('{}'.format(rc))
    Log.info(type(rc))

    Log.error('已连接')
    return rc


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""


def cluster_commands(rc: RedisCluster):
    Log.info(rc.cluster_info())

    Log.info(rc.cluster_slots())

    Log.info(rc.cluster_nodes())

    Log.info('------')

    exist_key_slot = rc.cluster_keyslot(gl_real_string)  # 计算key 应该被放置在哪个slot
    Log.info(exist_key_slot)

    Log.info(rc.cluster_countkeysinslot(exist_key_slot))  # 返回  slot 目前包含的键值对数量

    Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 0))  # 返回 n 个 slot的键
    Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 1))
    Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 2))
    Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 3))

    Log.info('------')

    Log.info(rc.cluster_keyslot(gl_real_hash))
    Log.info(rc.cluster_keyslot(gl_real_list))
    Log.info(rc.cluster_keyslot(gl_real_set))

    Log.info(rc.cluster_keyslot(gl_no_exist))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""


def keys_commands(rc: RedisCluster):
    data_list = rc.keys(gl_key_name)
    Log.info(len(data_list))
    Log.info(type(data_list))

    Log.error('------')


def scan_commands(rc: RedisCluster):
    data_list = list()
    cursor = 0

    # args = rc.scan(cursor, gl_key_name, count=5000)
    # Log.info(args)  # 返回值有问题

    # https://redis-py-cluster.readthedocs.io/en/2.1.3/commands.html#keys-generic

    # SCAN command has currently a buggy client side implementation.
    #
    # It is not recommended to use any *SCAN methods.
    # 不建议使用任何*SCAN方法。

    Log.error('------')


def scan_iter_method(rc: RedisCluster):
    args = rc.scan_iter(gl_key_name, count=5000)
    Log.info(args)
    Log.info(type(args))

    data = list(args)
    Log.info(len(data))
    Log.info(data[-10:])

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""


def cluster_str(rc: RedisCluster):
    """

    :param rc:
    :return:
    """
    # https://redis.io/docs/data-types/strings/

    Log.info(rc.delete(gl_test_str))

    Log.info(rc.set(gl_test_str, 'https://blog.csdn.net/zyooooxie', ex=1000))
    Log.info(rc.get(gl_test_str))

    key1 = 'external:customer:xxx_1'
    key2 = 'external:customer:xxx_2'
    key3 = 'external:customer:xxx_3'
    key4 = 'external:TEST'

    Log.info(rc.mset(
        {key1: 'value 1', key2: 'value 2', key3: '3个确定都是相同slot',
         key4: 'redis-py-cluster的mget、mset 支持 不同slot的key'}))

    Log.info(rc.mget(key1, key3, key2))
    Log.info(rc.mget(key1, key4))

    Log.info('------')

    Log.info('redis-py-cluster的unlink 必须是 the same slot的key')
    Log.info(rc.unlink(key1, key3))
    # Log.info(rc.unlink(key1, key4, gl_no_exist))  # Keys in request don't hash to the same slot

    Log.info(rc.exists(gl_test_str))
    Log.info(rc.type(gl_test_str))
    Log.info(rc.ttl(gl_test_str))
    Log.info(rc.expire(gl_test_str, 2 * 60 * 60))


def cluster_hash(rc: RedisCluster):
    """

    :param rc:
    :return:
    """
    # https://redis.io/docs/data-types/hashes/

    Log.info(rc.delete(gl_test_hash))

    Log.info(rc.hset(gl_test_hash, mapping={'hash_key0': 'hash_value0', 'hash_key1': 'hash_value1',
                                            'hash_key2': 'hash_value2', 'hash_key3': 'hash_value3',
                                            'hk4': 'hv4', 'hk5': 'hv5',
                                            'hk6': 'hv6'
                                            }))

    Log.info(rc.hget(gl_test_hash, 'hash_key0'))

    Log.info(rc.hlen(gl_test_hash))

    Log.info(rc.hexists(gl_test_hash, 'hash_key2222'))

    Log.info(rc.hkeys(gl_test_hash))
    Log.info(rc.hvals(gl_test_hash))

    Log.info(rc.hdel(gl_test_hash, 'hash_key2222', 'hash_key0', 'hk6'))

    Log.info(rc.hmget(gl_test_hash, 'hash_key2222', 'hash_key2'))
    Log.info(rc.hmget(gl_test_hash, ['hash_key2222', 'hash_key2']))

    Log.info(rc.hmset(gl_test_hash, {'test': 'test_value', 'test2': 'test_value2'}))

    Log.info(rc.hgetall(gl_test_hash))

    Log.info('------')

    Log.info(rc.hset(gl_no_exist, mapping={'test': 'test_value', 'test2': 'test_value2'}))
    Log.info(rc.unlink(gl_no_exist))

    Log.info(rc.exists(gl_test_hash))
    Log.info(rc.type(gl_test_hash))
    Log.info(rc.ttl(gl_test_hash))
    Log.info(rc.expire(gl_test_hash, 2 * 60 * 60))


def cluster_list(rc: RedisCluster):
    """

    :param rc:
    :return:
    """
    # https://redis.io/docs/data-types/lists/

    Log.info(rc.delete(gl_test_list))

    Log.info(rc.rpush(gl_test_list, 'list1', 'list2', 'list3'))

    Log.info(rc.lindex(gl_test_list, 1))
    Log.info(rc.llen(gl_test_list))

    Log.info(rc.lpush(gl_test_list, 'list0', 'list0'))
    Log.info(rc.linsert(gl_test_list, 'BEFORE', 'list0', 'BEFORE__'))
    Log.info(rc.linsert(gl_test_list, 'AFTER', 'list0', 'AFTER__'))  # 放在第一个list0 之后
    Log.info(rc.lrange(gl_test_list, 0, -1))

    Log.info(rc.lpop(gl_test_list))
    Log.info(rc.rpop(gl_test_list))
    Log.info(rc.lrem(gl_test_list, 1, 'list0'))
    Log.info(rc.lset(gl_test_list, 0, '新的_0'))

    Log.info('------')

    Log.info(rc.lpush(gl_no_exist, 0, 'list_0', 1, 'list_1'))
    Log.info(rc.unlink(gl_no_exist))

    Log.info(rc.type(gl_test_list))
    Log.info(rc.exists(gl_test_list))
    Log.info(rc.ttl(gl_test_list))
    Log.info(rc.expire(gl_test_list, 2 * 60 * 60))


def cluster_set(rc: RedisCluster):
    """

    :param rc:
    :return:
    """
    # https://redis.io/docs/data-types/sets/

    Log.info(rc.delete(gl_test_set))

    Log.info(rc.sadd(gl_test_set, 'set1', 'set2', 'set3', 'set3', 'set3', 'set3', 'set4'))

    Log.info(rc.sismember(gl_test_set, 'set1111'))
    Log.info(rc.srem(gl_test_set, 'set1'))
    Log.info(rc.scard(gl_test_set))
    Log.info(rc.smembers(gl_test_set))

    Log.info('------')

    Log.info(rc.sadd(gl_no_exist, 'set3', 'set3', 'set3', 'set3'))
    Log.info(rc.unlink(gl_no_exist))

    Log.info(rc.type(gl_test_set))
    Log.info(rc.exists(gl_test_set))
    Log.info(rc.ttl(gl_test_set))
    Log.info(rc.expire(gl_test_set, 2 * 60 * 60))


if __name__ == '__main__':
    Log.error('------')

    # rc_m = redis_py_cluster_connect_1()
    rc_m = redis_py_cluster_connect_2()
    # rc_m = redis_py_cluster_connect_3()

    # cluster_commands(rc=rc_m)

    try:

        # cluster_str(rc_m)
        # cluster_hash(rc_m)
        # cluster_list(rc_m)
        # cluster_set(rc_m)

        Log.error(gl_key_name)
        scan_commands(rc_m)

        keys_commands(rc_m)
        # scan_iter_method(rc_m)

    except Exception as e:
        Log.error(e.args)
        Log.info(traceback.format_exc())

    rc_m.close()

    Log.error('------')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183

本文链接:https://blog.csdn.net/zyooooxie/article/details/112484045

个人博客 https://blog.csdn.net/zyooooxie

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/774589
推荐阅读
相关标签
  

闽ICP备14008679号