#安装Python集群支持模块,首先是已经安装了 pip
1.sudo pip install rediscluster
2.建立集群连接
from rediscluster import StrictRedisCluster
#集群节点定义。(我的集群是3个master,6个slave)
cluster_nodes = [{'host': '10.10.53.75', 'port': 6384},
{'host': '10.10.53.75', 'port': 6385},
{'host': '10.10.53.75', 'port': 6386},
{'host': '10.10.53.75', 'port': 6387},
{'host': '10.10.53.75', 'port': 6388},
{'host': '10.10.53.75', 'port': 6389},
{'host': '10.10.62.253', 'port': 6379},
{'host': '10.10.62.253', 'port': 6380},
{'host': '10.10.62.253', 'port': 6381},
]
try:
redis_conn = StrictRedisCluster(startup_nodes=cluster_nodes)
except Exception, e:
print 'Connect Error!'
sys.exit(1)
3.geo命令。
1. geoadd: 增加地理位置的坐标。
2. geodist: 获取两个地理位置的距离。
3. geohash: 获取地理位置的GeoHash值。
4. geopos: 获取地理位置的坐标。
5. georadius: 根据给定经纬度坐标获取指定范围内的地理位置集合。
6. georadiusbymember: 根据给定地理位置获取指定范围内的地理位置集合。
4.代码测试。
# /usr/bin/env python
# coding:utf8
# __*__ author:liuxiankun __*__
'''
测试redis4集群,对geo 命令及python 脚本的支持
'''
from rediscluster import StrictRedisCluster
import sys
def redis_cluster():
cluster_nodes = [{'host': '10.10.53.75', 'port': 6384},
{'host': '10.10.53.75', 'port': 6385},
{'host': '10.10.53.75', 'port': 6386},
{'host': '10.10.53.75', 'port': 6387},
{'host': '10.10.53.75', 'port': 6388},
{'host': '10.10.53.75', 'port': 6389},
{'host': '10.10.62.253', 'port': 6379},
{'host': '10.10.62.253', 'port': 6380},
{'host': '10.10.62.253', 'port': 6381},
]
try:
redis_conn = StrictRedisCluster(startup_nodes=cluster_nodes)
except Exception, e:
print 'Connect Error!'
sys.exit(1)
return redis_conn
if __name__ == '__main__':
redis_conn=redis_cluster()
redis_conn.geoadd('Guangdong', 113.2099647, 23.593675, 'Qingyuan', 113.2278442, 23.1255978,
'Guangzhou', 113.106308, 23.0088312, 'Foshan', 114.0538788, 22.5551603, 'Shenzhen')
print(redis_conn.geopos('Guangdong', 'Qingyuan', 'Guangzhou', 'Foshan', 'Shenzhen'))
distance1 = redis_conn.geodist('Guangdong', 'Guangzhou', 'Foshan', unit='km')
distance2 = redis_conn.geodist('Guangdong', 'Guangzhou', 'Qingyuan', unit='km')
print('Distance of the two cities:%s km' % distance1)
print('Distance of the two cities:%s km' % distance2)
city1 = redis_conn.georadius('Guangdong', 114.0538788, 22.5551603, 120, unit='km', withdist=True)
print(city1)
city2 = redis_conn.georadiusbymember('Guangdong', 'Guangzhou', 30, unit='km', withdist=True)
print(city2)
geohash = redis_conn.geohash('Guangdong', 'Qingyuan', 'Guangzhou', 'Foshan')
print(geohash)