package javac.db;import com.datastax.driver.core.*;import java.util.Iterator;public class Cassandra { private static final String IP = "127.0.0.1"; private static final String KEY_SPACE_NAME = "mykeyspace"; private static Session session = null; public static Session getKeyspaceSession(){ if(null == session){ QueryOptions options = new QueryOptions(); options.setConsistencyLevel(ConsistencyLevel.QUORUM); Cluster cluster = Cluster.builder() .addContactPoint(IP) .withCredentials("cassandra", "cassandra") .withQueryOptions(options) .build(); session = cluster.connect(KEY_SPACE_NAME); } return session; } public static void main(String[] args){ Session mykeyspaceSession = getKeyspaceSession(); // 这里执行SQL(增删改查) ResultSet result = mykeyspaceSession.execute("select * from users;"); Iteratoriterator = result.iterator(); while(iterator.hasNext()) { Row row = iterator.next(); String fname = row.getString("fname"); System.out.println(fname); } }}
datastax maven key:
"com.datastax.cassandra" % "cassandra-driver-core" % "3.0.0-alpha3",