2013-10-29 43 views
0

当尝试使用客户端Astyanax连接到EC2实例上的Cassandra时,出现以下错误“astyanax.connectionpool.exceptions.PoolTimeoutException:”。需要帮助尝试使用Astyanax连接到承载在EC2实例上的Cassandra时出错

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()); 
      } 
} 
} 

错误 [email protected] com.netflix.astyanax.connectionpool.exceptions.PoolTimeoutException:PoolTimeoutException:主机=():9042,等待时间= 5001(5001 ),attempts = 1]超时等待连接 at com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.waitForConnection(SimpleHostConnectionPool.java:201) at com.netflix.astyanax.connectionpool.impl.SimpleHostConnectionPool.borrowConnection(SimpleHostConnectionPool .java:158) at 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) 在com.netflix。 astyanax.thrift.ThriftColumnFamilyQueryImpl $ 1.execute(ThriftColumnFamilyQueryImpl.java:180) at com.rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:57) 线程“main”中的异常java.lang.NullPointerException at com .rjil.jiodrive.rs.MetadataRS.main(MetadataRS.java:62)

回答

0

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

如果已完成此操作,那么您的客户端可能位于防火墙的后面,以防止出站流量流向您选择的端口(9042)。

最后,如果你还没有使用任何弹性IP,它能够更好地使用EC2实例的DNS名称,无论是在你的setSeeds部分和cassandra.yaml的rpc_address

+0

我已经安装了Datastax,并且能够使用提到的相同EC2主机IP和端口从它运行CQL。您是否仍然认为EC2入站安全组中的设置可能存在问题? –

0

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

+0

节点正在运行我能够通过SSH进行连接,甚至可以使用相同的IP和端口号从Datastax运行CQL?那么为什么不从astyanax –

+0

你在cassandra.yaml中设置了哪些RPC端口?这应该与您给Astyanax的港口相匹配。 –

0

我终于升级libthrift 0.9和改变了我代码到以下nd它现在工作正常。

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(); 
} 
相关问题