Skip to content

Instantly share code, notes, and snippets.

@bsbodden
Created December 3, 2014 04:20
Show Gist options
  • Save bsbodden/8ff6067f9f63c409ce13 to your computer and use it in GitHub Desktop.
Save bsbodden/8ff6067f9f63c409ce13 to your computer and use it in GitHub Desktop.
HelloCassandra.java
package org.integrallis.cassandra.oink;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
public class HelloCassandra {
public static void main(String[] args) {
Cluster cluster = null;
Session session = null;
try {
cluster = Cluster.builder().addContactPoints("localhost").build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect("oink");
for (Row row : session.execute("SELECT oink_id, dateOf(oink_id), user_id, body FROM oinks")) {
System.out.println("========================================================");
System.out.println("OINK_ID: " + row.getUUID("oink_id"));
System.out.println("TIMESTAMP: " + row.getDate("dateOf(oink_id)"));
System.out.println("USER_ID: " + row.getString("user_id"));
System.out.println("BODY: " + row.getString("body"));
}
System.out.println("========================================================");
} catch (NoHostAvailableException nhae) {
System.err.println("No alive hosts to use:" + nhae.getMessage());
System.exit(1);
} catch (Exception e) {
System.err.println("Unexpected error: " + e.getMessage());
e.printStackTrace();
System.exit(1);
} finally {
session.close();
cluster.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment