Redis4cluster Python支持模块。Geo命令测试

#安装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)






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值