我是cassandra的新手,和docker一样。我的目标是从java连接到在docker中运行的cassandra。我正在使用Windows机器
我通过执行cassandra(也尝试过ver 2.2,2.10而没有任何env.variables)
docker run --name cassandra -d -e CASSANDRA_RPC_ADDRESS=127.0.0.1 cassandra:3.0
目前我有这样的docker ps输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f932d3e7e0dc cassandra:3.0 "/docker-entrypoin..." 5 seconds ago Up 5 seconds 7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp cassandra
我可以用这样的输出“docker exec -it cassandra cqlsh”
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.0.13 | CQL spec 3.4.0 | Native protocol v4]
在java项目中,我有gradle依赖
compile group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version: '3.2.0'
我的测试代码(也未尝试指定任何协议版本,尝试使用V5)
public static void main(String[] args)
{
Cluster cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.withProtocolVersion(ProtocolVersion.V4)
.build();
Session session = cluster.connect();
}
我得到了这个例外:
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [/127.0.0.1:9042] Cannot connect))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:232)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1600)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1518)
at com.datastax.driver.core.Cluster.init(Cluster.java:159)
at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:330)
at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:305)
at com.datastax.driver.core.Cluster.connect(Cluster.java:247)
at com.iter.cassandratest.Cassandratest.main(Cassandratest.java:17)
我做错了什么想法?
编辑1:
,我刚尝试使用-P选项运行现在ps输出为:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9eb54421aac2 cassandra:3.0 "/docker-entrypoin..." 4 seconds ago Up 3 seconds 0.0.0.0:32772->7000/tcp, 0.0.0.0:32771->7001/tcp, 0.0.0.0:32770->7199/tcp, 0.0.0.0:32769->9042/tcp, 0.0.0.0:32768->9160/tcp cassandra
我将代码更改为
Cluster cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.withPort(32769)
.withProtocolVersion(ProtocolVersion.V4)
.build();
异常更改为
All host(s) tried for query failed (tried: /127.0.0.1:32769 (com.datastax.driver.core.exceptions.TransportException: [/127.0.0.1:32769]
Channel has been closed))
答案 0 :(得分:0)
如果您想使用默认端口连接到localhost,则需要转发它。
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?concept ?subClassOf
WHERE
{ GRAPH <http://bioportal.bioontology.org/ontologies/RADLEX_OWL>
{ ?term a <http://bioontology.org/projects/ontologies/radlex/radlexOwl#radlex_metaclass> ;
<http://bioontology.org/projects/ontologies/radlex/radlexOwl#Preferred_name> ?concept
}
?term rdfs:subClassOf ?upperClass.
?upperClass <http://bioontology.org/projects/ontologies/radlex/radlexOwl#Preferred_name> ?subClassOf.
} LIMIT 10 OFFSET 10
使用docker run --name cassandra -d \
-p 9042:9042 \
cassandra:3.0
将根据Dockerfile的-P
选项为您选择随机端口。
答案 1 :(得分:0)
感谢Rober。答案是
docker run --name cassandra -P -d cassandra:3.0
然后连接到docker ps中显示的端口(对我来说是32769)。