Last active
June 8, 2018 04:27
-
-
Save reddikih/3c49644d409e5991d70033a28ef7538b to your computer and use it in GitHub Desktop.
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
import static java.lang.String.format; | |
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.policies.DCAwareRoundRobinPolicy; | |
import com.datastax.driver.core.policies.DefaultRetryPolicy; | |
import com.datastax.driver.core.policies.TokenAwarePolicy; | |
public class CassandraSample { | |
private static final String USER = "cassandra"; | |
private static final String PASS = "cassandra"; | |
private static final String CLUSTER_NAME = "Test Cluster"; | |
private static final String DEFAULT_ENDPOINT = "localhost"; | |
private static final String TEST_KEYSPACE = "cass_sample"; | |
private Cluster cluster; | |
public static void main(String... args) { | |
CassandraSample app = new CassandraSample(); | |
String endpoints = getEndpoint(args); | |
app.getCluster(endpoints); | |
app.connectionTest(endpoints); | |
app.createKeyspace(endpoints, TEST_KEYSPACE); | |
app.closeCluster(); | |
} | |
static String getEndpoint(String... args) { | |
String endpoints; | |
if (args.length >= 1) { | |
endpoints = args[0]; | |
} else { | |
endpoints = DEFAULT_ENDPOINT; | |
} | |
return endpoints; | |
} | |
public void getCluster(String endpoints) { | |
if (cluster == null) { | |
cluster = Cluster | |
.builder() | |
.addContactPoints(endpoints) | |
.withCredentials(USER, PASS) | |
.withClusterName(CLUSTER_NAME) | |
.withRetryPolicy(DefaultRetryPolicy.INSTANCE) | |
// .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy(DC_NAME))) // for driver 2.1.8 | |
.withLoadBalancingPolicy( | |
new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build())) // for driver 3.1.8 | |
.build(); | |
} | |
} | |
public void closeCluster() { | |
if (cluster != null) { | |
cluster.close(); | |
} | |
} | |
public void connectionTest(String endpoints) { | |
long connStart = System.currentTimeMillis(); | |
Session session = cluster.connect(); | |
System.out.println(format("Cluster#connect: %.3f s", | |
(double) (System.currentTimeMillis() - connStart) / 1000)); | |
long exeStart = System.currentTimeMillis(); | |
ResultSet rs = session.execute( | |
"select key, bootstrapped, broadcast_address, host_id, listen_address, rpc_address from system.local;"); | |
System.out.println( | |
format("select: %.3f s", (double) (System.currentTimeMillis() - exeStart) / 1000)); | |
System.out.println( | |
" key | bootstrapped | broadcast_address | host_id | listen_address | rpc_address"); | |
System.out.println( | |
"-------+--------------+-------------------+--------------------------------------+----------------+-------------"); | |
for (Row row : rs) { | |
System.out.printf( | |
"%-7s|%-14s|%-19s|%-38s|%-16s|%-13s\n", | |
row.getString("key"), | |
row.getString("bootstrapped"), | |
row.getInet("broadcast_address").getHostAddress(), | |
row.getUUID("host_id").toString(), | |
row.getInet("listen_address").getHostAddress(), | |
row.getInet("rpc_address").getHostAddress() | |
); | |
} | |
long scStart = System.currentTimeMillis(); | |
session.close(); | |
System.out.println( | |
format("Session#close: %.3f s", (double) (System.currentTimeMillis() - scStart) / 1000)); | |
long ccStart = System.currentTimeMillis(); | |
System.out.println( | |
format("Cluster#close: %.3f s", (double) (System.currentTimeMillis() - ccStart) / 1000)); | |
} | |
public void createKeyspace(String endpoints, String keyspace) { | |
Session session = cluster.connect(); | |
String createStatement = | |
"CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'SimpleStrategy','replication_factor':'1'}"; | |
session.execute(format(createStatement, keyspace)); | |
System.out.printf("Created keyspace: %s\n", keyspace); | |
String dropStmt = "DROP KEYSPACE %s"; | |
session.execute(format(dropStmt, keyspace)); | |
System.out.printf("Dropped keyspace: %s\n", keyspace); | |
session.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment