尝试使用Astyanax连接到EC2实例上托管的Cassandra时出错

时间:2013-10-29 16:25:11

标签: amazon-ec2 cassandra astyanax

我在尝试使用客户端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),attempts = 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)         at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:50)         at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:229)         在com.netflix.astyanax.thrift.ThriftColumnFamilyQueryImpl $ 1.execute(ThriftColumnFamilyQueryImpl.java:180)         在com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:57)     线程“main”java.lang.NullPointerException中的异常         在com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:62)

3 个答案:

答案 0 :(得分:0)

您的问题不在您的代码中。您指定为种子的节点存在连接问题。因此,该节点未运行,或者您无法从运行客户端的计算机上访问该节点。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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();
}