Use java to operate cassandra

    There are many java drivers for cassandra, and the datastax driver is used here. Add dependencies to maven's pom.xml:

<dependency>
	<groupId>com.datastax.cassandra</groupId>
	<artifactId>cassandra-driver-core</artifactId>
	<version>3.1.0</version>
</dependency>

    1. Connect to the cassandra cluster (single machine as well)

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

    2. Get the connection

Session session = cluster.connect();

   3. Execute cql query

ResultSet rs = session.execute("select release_version from system.local");

   4. Get the return value

Row row = rs.one();
System.out.println(row.getString("release_version"));

    5. Close the connection

session.close();

    6. Shut down the cluster

cluster.close();

    It's that simple, the overall operation is similar to the mysql operation of jdbc, and the api in it is also very similar to jdbc.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326476852&siteId=291194637