NoHostAvailableException in Cassandra

0

I need to make a presentation + connection to Cassandra for class. The fact is that it does not let me connect when I use the datastax drivers. I have gone a thousand times because apparently there are old versions that are incompatible and well, I lost a few hours with this.

My code now looks like this:

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;

public class conectaradb {

    public static void main(String[] args) {
 try {
        Cluster cluster;
         Session sesion;

         cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(9042).build();

         sesion= cluster.connect("ejemplodb");

         ResultSet resultado= sesion.execute("select * from empleados");
         String nombreemp,fecha,cargo;
         double comision,salario;
         int empno;

         for (Row row:resultado) {
             empno=row.getInt("empno");
             cargo=row.getString("cargo");
             comision=row.getDouble("comision");
             fecha=row.getString("fechaing");
             nombreemp=row.getString("nombreempno");
             salario=row.getDouble("salario");
             System.out.println(empno+" "+cargo+" "+comision+" "+fecha+" "+nombreemp+" "+salario);
         }  
         cluster.close();

 }catch(NoHostAvailableException e) {
     System.out.println(e.getErrors());
 }


    }

}

The error appears when connecting to localhost ... I have also been fussing with the .yaml file, so if anyone knows the correct configuration they should have, it would be great.

    
asked by M.M. 17.11.2017 в 17:48
source

1 answer

1

I summarize the possible causes why you may be giving this.

Cassandra gets up and "sticks" to a network interface, which you indicate in the cassandra.yaml file in listen_address . The first thing to keep in mind is that if you have several network interfaces you will only allow messages from the network interface to which you have associated. If you want to be associated with all, you must put 0.0.0.0 .

When you raise Cassandra, at least one of the seed nodes must be up (or your node be a seed node). This is specified in the seeds property. If you have a single node installation, make sure your listen_address and your seeds have the same value.

I assume from your code that everything is running on the same node (both program and Cassandra), for filtering a bit more everything, make sure your firewall allows connections, and look at your hosts file (/ etc / hosts) to confirm that localhost points to your local ip.

Tell us if any of this has helped, or upload the following information

  • Where Cassandra runs (and her cassandra.yaml file)
  • Where your program runs
answered by 09.03.2018 в 12:27