Connect and Fetch data from Cassendra Using Java Code

1 Connector Class :

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;

import static java.lang.System.out;

/**
* Class used for connecting to Cassandra database.
*/
public class CassandraConnector
{
/** Cassandra Cluster. */
private Cluster cluster;

/** Cassandra Session. */
private Session session;

/**
* Connect to Cassandra Cluster specified by provided node IP
* address and port number.
*
* @param node Cluster node IP address.
* @param port Port of cluster host.
*/
public void connect(final String node, final int port)
{
this.cluster = Cluster.builder().addContactPoint(node).withPort(port).build();
final Metadata metadata = cluster.getMetadata();
out.printf(“Connected to cluster: %s\n”, metadata.getClusterName());
for (final Host host : metadata.getAllHosts())
{
out.printf(“Datacenter: %s; Host: %s; Rack: %s\n”,
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}

/**
* Provide my Session.
*
* @return My session.
*/
public Session getSession()
{
return this.session;
}

/** Close cluster. */
public void close()
{
cluster.close();
}

/**
* Main function for demonstrating connecting to Cassandra with host and port.
*
* @param args Command-line arguments; first argument, if provided, is the
* host and second argument, if provided, is the port.
*/
public static void main(final String[] args)
{
final CassandraConnector client = new CassandraConnector();
final String ipAddress = args.length > 0 ? args[0] : “localhost”;
final int port = args.length > 1 ? Integer.parseInt(args[1]) : 9160;
out.println(“Connecting to IP Address ” + ipAddress + “:” + port + “…”);
client.connect(ipAddress, port);
client.close();
}

}

2 Client Class

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;

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

public class CassendraClient {
//private Cluster cluster;

private Cluster cluster;
private Session session;

public void connect(String node) {

cluster = Cluster.builder().addContactPoint(“127.0.0.1”).build();
session = cluster.connect(“appsonekeyspace”);

ResultSet results = session.execute(“SELECT * FROM users”);
for (Row row : results) {
System.out.format(“%s %s\n”, row.getString(“user_name”), row.getString(“password”));
}
}

public void close() {
//cluster.shutdown();
}

public static void main(String[] args) {
CassendraClient client = new CassendraClient();
client.connect(“10.1.1.249”);
//client.close();
}
}

Advertisements

About ashokabhat

I am a C,C ,JAVA,Adobe Flex,.NET Programmer Currently working as a Software Developer
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s