0

我正在尝试使用地址转换映射连接到 compose 上的 scylladb 集群。

只有在实例化集群实例时对端口进行硬编码,才能使代码正常工作:

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 方法进行端口翻译?您可以在下面的调试输出中看到,传递给 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
4

1 回答 1

1

其他 Cassandra 驱动程序(例如节点驱动程序)支持端口转换。nodejs翻译文档

MyAddressTranslator.prototype.translate = function (address, port, callback) {
   // Your custom translation logic.
}; 

上面你可以看到翻译器接收到了ip地址和端口。

但是,我不相信当前的 Cassandra python 驱动程序支持端口地址转换

translate(addr)

接受节点 ip 地址,并返回转换后的地址以用于连接到该节点。

在这里可以看到翻译器只接收到ip地址。

于 2017-06-22T10:09:05.380 回答