无法使用Astyanax连接到Cassandra

时间:2016-11-06 13:15:21

标签: scala cassandra astyanax

我在docker的mac上运行最新版本的cassandra。我在我的mac上安装了cassandra开发中心,我可以使用devcenter连接到cassandra,我可以轻松地查询我的表。

http://i.imgur.com/48MztBM.png

http://i.imgur.com/jPKblly.png

我可以看到cassandra所有必需的端口也是开放的。 (如下图所示)

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                                                      NAMES
e698e61e3bac        cassandra:latest    "/docker-entrypoint.s"   9 

minutes ago       Up 9 minutes        0.0.0.0:7000->7000/tcp, 0.0.0.0:7199-
>7199/tcp, 0.0.0.0:9042->9042/tcp, 0.0.0.0:9160->9160/tcp, 7001/tcp   cassandra

我也登录了我的cassandra容器并执行了以下命令

root@e698e61e3bac:/# nodetool -h 127.0.0.1 enablethrift
root@e698e61e3bac:/# nodetool -h 127.0.0.1 statusthrift
running

但是当我运行下面的代码时

val configImpl = new AstyanaxConfigurationImpl()
configImpl.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
configImpl.setCqlVersion("3.4.2")
configImpl.setTargetCassandraVersion("3.7")
val poolConfig = new ConnectionPoolConfigurationImpl("MyConnectionPool")
poolConfig.setPort(9160)
poolConfig.setMaxConnsPerHost(1)
poolConfig.setSeeds("192.168.1.169")

val context = new AstyanaxContext.Builder()
  .forCluster("localhost")
  .forKeyspace("movielens_small")
  .withAstyanaxConfiguration(configImpl)
  .withConnectionPoolConfiguration(poolConfig)
  .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
  .buildKeyspace(ThriftFamilyFactory.getInstance())
context.start()
val keyspace = context.getClient()
val cf = new ColumnFamily[UUID, String]("cf", UUIDSerializer.get, StringSerializer.get())
val result = keyspace.prepareQuery(cf).withCql("select name from movies").execute()
val data = result.getResult.getRows()
for {
  row <- data
  col <- row.getColumns
} {
  println(col)
}
context.shutdown()

我收到以下错误

07:32:10,606  INFO ConnectionPoolMBeanManager:53 - Registering mbean: com.netflix.MonitoredResources:type=ASTYANAX,name=MyConnectionPool,ServiceType=connectionpool
07:32:10,625  INFO CountingConnectionPoolMonitor:194 - AddHost: 192.168.1.169
07:32:10,748  INFO CountingConnectionPoolMonitor:194 - AddHost: 172.17.0.2
07:32:10,748  INFO CountingConnectionPoolMonitor:205 - RemoveHost: 192.168.1.169
[error] (run-main-0) com.netflix.astyanax.connectionpool.exceptions.PoolTimeoutException: PoolTimeoutException: [host=172.17.0.2(172.17.0.2):9160, latency=2003(2003), attempts=1]Timed out waiting for connection
com.netflix.astyanax.connectionpool.exceptions.PoolTimeoutException: PoolTimeoutException: [host=172.17.0.2(172.17.0.2):9160, latency=2003(2003), attempts=1]Timed out waiting for connection
    at com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.waitForConnection(SimpleHostConnectionPool.java:231)
    at com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.borrowConnection(SimpleHostConnectionPool.java:198)
    at com.netflix.astyanax.connectionpool.impl.RoundRobinExecuteWithFailover.borrowConnection(RoundRobinExecuteWithFailover.java:84)
    at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:117)
    at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:352)
    at com.netflix.astyanax.thrift.AbstractThriftCqlQuery.execute(AbstractThriftCqlQuery.java:41)
    at com.abhi.CassandraScanner$.delayedEndpoint$com$abhi$CassandraScanner$1(CassandraScanner.scala:41)
    at com.abhi.CassandraScanner$delayedInit$body.apply(CassandraScanner.scala:19)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:378)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)

1 个答案:

答案 0 :(得分:0)

我能够解决问题。问题出在NodeDiscoveryType上。我将代码更改为NodeDiscoveryType.NONE,但效果非常好。虽然我仍然不明白为什么NodeDiscoveryType必须为NULL,以及为什么它不适用于RING_DESCRIBE。

希望有人会进一步阐述。