Created
December 3, 2014 04:20
-
-
Save bsbodden/8ff6067f9f63c409ce13 to your computer and use it in GitHub Desktop.
HelloCassandra.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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