我正在尝试使用地址转换地图连接到一个scylladb群集。
如果我在实例化Cluster实例时对端口进行硬编码,我只能使代码正常工作:
from cassandra.cluster import Cluster
from cassandra.policies import AddressTranslator
from cassandra.auth import PlainTextAuthProvider
################################################################################
# our variables
address_map = {
"10.0.24.69:9042": "sl-eu-lon-2-portal.3.dblayer.com:15227",
"10.0.24.71:9042": "sl-eu-lon-2-portal.2.dblayer.com:15229",
"10.0.24.70:9042": "sl-eu-lon-2-portal.1.dblayer.com:15228"
}
username = 'scylla'
password = 'changeme'
port = 15227
################################################################################
接下来是翻译地址的类:
class ComposeAddressTranslator(AddressTranslator):
def set_map(self, address_map):
# strip ports from both source and destination as the cassandra python
# client doesn't appear to support ports translation
self.address_map = {key.split(':')[0]: value.split(':')[0] for (key, value) in address_map.items()}
def contact_points(self):
return [value.split(':')[0] for (key, value) in address_map.items()]
def translate(self, addr):
# print some debug output
print('in translate(self, addr) method', type(addr), addr)
trans_addr = self.address_map[addr]
return trans_addr
现在让我们连接:
compose_translator = ComposeAddressTranslator()
compose_translator.set_map(address_map)
auth_provider = PlainTextAuthProvider(
username=username,
password=password
)
# if the port parameter value is removed from below, we are unable
# to establish a connection
cluster = Cluster(
contact_points = compose_translator.contact_points(),
address_translator = compose_translator,
auth_provider = auth_provider,
cql_version = '3.2.1',
protocol_version = 2,
port = port
)
session = cluster.connect()
session.execute("USE my_keyspace;")
session.shutdown()
似乎cassandra python库不支持translate method的端口转换?你可以在我的调试输出中看到,传入translate方法的addr是没有端口的字符串ip地址值:
in translate(self, addr) method <class 'str'> 10.0.24.69
in translate(self, addr) method <class 'str'> 10.0.24.71
我的环境:
$ pip freeze | grep cassandra
cassandra-driver==3.10
$ pip freeze | grep cassandra
cassandra-driver==3.10
答案 0 :(得分:0)
其他Cassandra驱动程序(如节点驱动程序)支持端口转换。 nodejs translator documentation:
MyAddressTranslator.prototype.translate = function (address, port, callback) {
// Your custom translation logic.
};
上面你可以看到翻译器同时接收到ip地址和端口。
但是,我不相信当前的Cassandra python驱动程序支持端口地址translation:
translate(addr)
接受节点ip地址,并返回连接到此节点的转换地址。
在这里你可以看到翻译只收到ip地址。