Spring data brings us a lot of convenience in accessing the data, and then we'll combine Spring-data-cassandra to see how to quickly access the Cassandra data.
Of course, the Official Handbook is sure to look at the official 1.2.0RELEASE document. Prepare the dependency for basic use:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
SETP1: Define a domain model (called an entity in JPA), such as Person:
Import Org.springframework.data.cassandra.mapping.PrimaryKey;
Import org.springframework.data.cassandra.mapping.Table;
@Table
public class Person {
@PrimaryKey
Private String ID;
private String name;
private int age;
Public person (String id,string name,int age) {
This.id = ID;
THIS.name = name;
This.age=age;
}
}
It's not hard to find friends who are familiar with JPA, it's really like the entity definition. But it is different, and JPA is not allowed to have constructors, but there is no such restriction in the Spring-data-cassandra. After all, Spring-data-cassandra's mapping relationship is different from JPA's. See, no longer is @id but use the @primarykey.
STEP2: Define related repository, such as personrepository:
Import Org.springframework.data.repository.CrudRepository;
Import Demo.domain.Person;
Public interface Personrepository extends Crudrepository<person, string>{
}
Friends who have used SPRING-DATA-JPA should be very aware of how this is used, of course, for composite primary key or to add a bit of attention.
STEP3: The key configuration file, in fact, the above content for spring data has a basic understanding of people will be. The only point to note is that this profile, absolutely has its own characteristics:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns:cassandra= "Http://www.springframework.org/schema/data/cassandra"
xmlns:context= "Http://www.springframework.org/schema/context"
xsi:schemalocation= "Http://www.springframework.org/schema/cql http://www.springframework.org/schema/cql/spring-cql-1.0.xsd
Http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/ Spring-cassandra-1.0.xsd
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
<context:property-placeholder location= "Classpath:cassandra.properties"/>
<cassandra:cluster contact-points= "${cassandra.contactpoints}" port= "${cassandra.port}"/>
<cassandra:session keyspace-name= "${cassandra.keyspace}"/>
<cassandra:mapping/>
<cassandra:converter/>
<cassandra:template id= "Cqltemplate"/>
<cassandra:repositories base-package= "Demo"/>
</beans>
Official information <cassandra:template id= "cqltemplate"/> ID has some problems, after practice only need to adjust to the name above. Of course, the properties file is used here, Cassandra.properties:
cassandra.contactpoints=127.0.0.1
cassandra.port=9042
Cassandra.keyspace=mykeyspace
STEP4: Interspersed up to run:
public class Demo {
public static void Main (string[] args) {
Configurableapplicationcontext ct = new Classpathxmlapplicationcontext (
"Beans.xml");
Personrepository repository = Ct.getbean (Personrepository.class);
list<person> persons = new arraylist<person> ();
for (int i=0;i<10000;i++) {
person who = new Person (integer.tostring (i), "name", I);
Persons.add (person);
}
Repository.save (persons);
Ct.close ();
}
}
Ok,everything is OK. It's very simple indeed.