If it is a MAVEN project, add dependencies to the Pom.xml. If not, download the appropriate jar package and put it in the Lib directory. The version of the driver package here is consistent with the large version of your Cassandra. My Cassandra version here is the latest 3.9, the driver is 3.0
1 <Dependency>2 <groupId>Com.datastax.cassandra</groupId>3 <Artifactid>Cassandra-driver-core</Artifactid>4 <version>3.0.0</version>5 </Dependency>
Create a new class of cassandratest.
Connection Cassandra
1 PublicCluster Cluster;2 3 PublicSession session;4 5 Public voidConnect ()6 {7 //addcontactpoints:cassandra node IP withport:cassandra node port default 90428 //Withcredentials:cassandra User name password if Cassandra.yaml authenticator:allowallauthenticator can not be configured9cluster = Cluster.builder (). Addcontactpoints ("192.168.3.89"). Withport (9042)Ten. Withcredentials ("Cassandra", "Cassandra"). Build (); OneSession =Cluster.connect (); A}
The driver is equipped with a Cassandra Connection pool configuration, the above is slightly modified
1 PublicCluster Cluster;2 3 PublicSession session;4 5 Public voidConnect ()6 {7Poolingoptions poolingoptions =Newpoolingoptions ();8 //The maximum number of requests per connection, 2.0, does not seem to have this method9Poolingoptions.setmaxrequestsperconnection (Hostdistance.local, 32);Ten //indicates that there are at least 2 connections to the machine in the cluster with a maximum of 4 connections OnePoolingoptions.setcoreconnectionsperhost (hostdistance.local, 2). Setmaxconnectionsperhost (HostDistance.LOCAL, 4) A. Setcoreconnectionsperhost (Hostdistance.remote, 2). Setmaxconnectionsperhost (Hostdistance.remote, 4); - - //addcontactpoints:cassandra node IP withport:cassandra node port default 9042 the //Withcredentials:cassandra User name password if Cassandra.yaml authenticator:allowallauthenticator can not be configured -cluster = Cluster.builder (). Addcontactpoints ("192.168.3.89"). Withport (9042) -. Withcredentials ("Cassandra", "Cassandra"). Withpoolingoptions (poolingoptions). build (); - //Establish a connection + //session = Cluster.connect ("Test"), connecting the existing key space -Session =Cluster.connect (); + A}
Create a key space and table (these 2 are best done when building Cassandra)
1 /**2 * Create a key space3 */4 Public voidcreatekeyspace ()5 {6 //Single Data Center replication strategy: 17String cql = "CREATE keyspace if not exists mydb with replication = {' class ': ' Simplestrategy ', ' replication_factor ': ' 1 '} ";8 Session.execute (CQL);9 }Ten One /** A * Create a table - */ - Public voidcreatetable () the { - //A, B is the composite primary key A: partition key, b. Cluster key -String cql = "CREATE TABLE if not exists mydb.test (a text,b int,c text,d int,primary KEY (A, b))"; - Session.execute (CQL); +}
Curd operation on test table
1 /**2 * Insert3 */4 Public voidInsert ()5 {6String CQL = "INSERT into Mydb.test (A, B, C, D) VALUES (' A2 ', 4, ' C2 ', 6);";7 Session.execute (CQL);8 }9 Ten /** One * Modify A */ - Public voidUpdate () - { the //A, B is a composite primary key, so the condition should be taken, less one will error, and update cannot modify the value of the primary key, which should be related to the Cassandra storage mode -String cql = "UPDATE mydb.test SET d = 1234 WHERE a= ' AA ' and b=2;"; - //It is also possible to Cassandra the inserted data if the primary key already exists, which is actually the update operation -String cql2 = "INSERT into Mydb.test (a,b,d) VALUES (' AA ', 2,1234);"; + //CQL and Cql2 are actually doing the same thing . - Session.execute (CQL); + } A at /** - * Delete - */ - Public voidDelete () - { - //Deleting a single field in a record only removes the non-primary key, with the primary key condition inString CQL = "DELETE d from mydb.test WHERE a= ' AA ' and b=2;"; - //Delete one or more records in a table must have a partition key in the condition toString cql2 = "DELETE from mydb.test WHERE a= ' AA ';"; + Session.execute (CQL); - Session.execute (CQL2); the } * $ /**Panax Notoginseng * Enquiry - */ the Public voidquery () + { AString CQL = "SELECT * from Mydb.test;"; theString cql2 = "Select A,b,c,d from Mydb.test;"; + -ResultSet ResultSet =Session.execute (CQL); $System.out.print ("Here is the field name:"); $ for(Definition definition:resultSet.getColumnDefinitions ()) - { -System.out.print (Definition.getname () + ""); the } - System.out.println ();WuyiSystem.out.println (String.Format ("%s\t%s\t%s\t%s\t\n%s", "a", "B", "C", "D", the"--------------------------------------------------------------------------")); - for(Row row:resultset) Wu { -System.out.println (String.Format ("%s\t%d\t%s\t%d\t", Row.getstring ("a"), Row.getint ("B"), AboutRow.getstring ("C"), Row.getint ("D")))); $ } -}
Query display:
Java Operations Cassandra Database