0

我在尝试使用客户端 Astyanax 连接到 EC2 实例上的 Cassandra 时收到以下错误“astyanax.connectionpool.exceptions.PoolTimeoutException:”。需要帮忙

Following is my code snippet.
   import org.mortbay.jetty.servlet.Context;

  import com.netflix.astyanax.AstyanaxContext;
  import com.netflix.astyanax.Keyspace;
  import com.netflix.astyanax.MutationBatch;
  import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
  import com.netflix.astyanax.connectionpool.OperationResult;
  import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
  import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
  import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
  import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
  import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
  import com.netflix.astyanax.model.Column;
  import com.netflix.astyanax.model.ColumnFamily;
  import com.netflix.astyanax.model.ColumnList;
  import com.netflix.astyanax.model.CqlResult;
  import com.netflix.astyanax.serializers.StringSerializer;
  import com.netflix.astyanax.thrift.ThriftFamilyFactory;


  public class MetadataRS {


    public static void main(String args[]){
    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .forCluster("ClusterName")
    .forKeyspace("KeyspaceName")
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()   
        .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
        .setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN)
    )
    .withConnectionPoolConfiguration(new     ConnectionPoolConfigurationImpl("MyConnectionPool")
        .setPort(9042)
        .setMaxConnsPerHost(40)
        .setSeeds("<EC2-IP>:9042")
        .setConnectTimeout(5000)
    )
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    Keyspace keyspace = context.getEntity();
    System.out.println(keyspace);

    ColumnFamily<String, String> CF_USER_INFO =
              new ColumnFamily<String, String>(
                "Standard1",              // Column Family Name
                StringSerializer.get(),   // Key Serializer
                StringSerializer.get());  // Column 

    OperationResult<ColumnList<String>> result = null;
    try {
        result = keyspace.prepareQuery(CF_USER_INFO)
            .getKey("user_id_hash")
            .execute();
    } catch (ConnectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            ColumnList<String> columns = result.getResult();

            // Lookup columns in response by name 

            String uid   = columns.getColumnByName("user_id_hash").getStringValue();

            System.out.println(uid);
            // Or, iterate through the columns
            for (Column<String> c : result.getResult()) {
              System.out.println(c.getName());
            }
 }
 }

错误 com.netflix.astyanax.thrift.ThriftKeyspaceImpl@1961f4 com.netflix.astyanax.connectionpool.exceptions.PoolTimeoutException:PoolTimeoutException:[host=():9042,latency=5001(5001),尝试=1] 等待连接超时在 com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.waitForConnection(SimpleHostConnectionPool.java:201) 在 com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.borrowConnection(SimpleHostConnectionPool.java:158) 在 com.netflix.astyanax.connectionpool .impl.RoundRobinExecuteWithFailover.borrowConnection(RoundRobinExecuteWithFailover.java:60) 在 com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:50) 在 com.netflix.astyanax.connectionpool.impl。AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:229) at com.netflix.astyanax.thrift.ThriftColumnFamilyQueryImpl$1.execute(ThriftColumnFamilyQueryImpl.java:180) at com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:57)在 com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:62) 的线程“主”java.lang.NullPointerException

4

3 回答 3

0

您的问题不在您的代码中。您指定为种子的节点存在连接问题。因此,要么该节点未运行,要么您无法从运行客户端的机器访问它。

于 2013-10-29T17:20:40.257 回答
0

由于您在 EC2 实例上运行 cassandra,因此请检查 cassandra 的端口号。(您选择为 9042)在 ec2 安全组的允许列表中,并且您可以访问它。如果没有添加端口号。在 ec2 安全组的入站列表中,并将 IP 范围设置为 0.0.0.0。还检查了ec2上的防火墙是否关闭。默认情况下它是假的,但无论如何检查它都很好。

如果您这样做了,那么您的客户端可能位于防火墙后面,该防火墙阻止了到您选择的端口 (9042) 的出站流量。

最后,如果您没有使用任何弹性 ip,最好在 setSeeds 部分和 cassandra.yaml 的 rpc_address 中使用 ec2 实例 dns 名称

于 2013-10-29T18:59:22.590 回答
0

我终于将 libthrift 升级到 0.9 并将我的代码更改为以下代码,现在它工作正常。

public Keyspace getDBConnection() {



    if (poolConfig == null) {

        poolConfig = new ConnectionPoolConfigurationImpl(
                "CassandraPool").setPort(port).setMaxConnsPerHost(1)
                .setSeeds((new StringBuilder(seedHost).append(":").append(port).toString()))
                .setLatencyAwareUpdateInterval(latencyAwareUpdateInterval) // Will resort hosts per
                                                        // token partition every
                                                        // 10 seconds
                .setLatencyAwareResetInterval(latencyAwareResetInterval) // Will clear the latency
                                                        // every 10 seconds. In
                                                        // practice I set this
                                                        // to 0 which is the
                                                        // default. It's better
                                                        // to be 0.
                .setLatencyAwareBadnessThreshold(latencyAwareBadnessThreshold) // Will sort hosts if a host
                                                    // is more than 100% slower
                                                    // than the best and always
                                                    // assign connections to the
                                                    // fastest host, otherwise
                                                    // will use round robin
                .setLatencyAwareWindowSize(latencyAwareWindowSize) // Uses last 100 latency
                                                // samples. These samples are in
                                                // a FIFO q and will just cycle
                                                // themselves.
                .setTimeoutWindow(60000)
        ;
    }

    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
            .forCluster(clusterName)
            .forKeyspace(keyspaceName)
            .withAstyanaxConfiguration(
                    new AstyanaxConfigurationImpl().setDiscoveryType(
                            NodeDiscoveryType.NONE)
                            .setConnectionPoolType(
                                    ConnectionPoolType.ROUND_ROBIN)
                            .setCqlVersion("3.0.0")
                            .setTargetCassandraVersion("2.0"))
            .withConnectionPoolConfiguration(poolConfig)
            .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
            .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    return context.getClient();
}
于 2013-11-13T17:58:34.807 回答