Five minutes learn to use Spring-data-cassandra to quickly implement data access

Source: Internet
Author: User
Tags cassandra

Spring data gives us a lot of access to the data, and then we combine Spring-data-cassandra to see how to quickly implement access to Cassandra data.

Of course, the Official Handbook is a must-see, official 1.2.0RELEASE document. Prepare for the basic use of dependency:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

SETP1: Defines 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 a friend who is familiar with JPA, which is really like the definition of entity. But it is different, and the JPA is not allowed to have constructors, but there is no such restriction in Spring-data-cassandra. After all, Spring-data-cassandra's mapping relationship is different from JPA. See, it is not @id but the use of @primarykey.

STEP2: Defines the 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 understand how to use this, of course, for the composite primary key or to add a little attention.


STEP3: The key configuration file, in fact, the above content for spring data has a basic understanding of people will get. The only thing to note is that this configuration file, 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 definition has some problems, after practice only need to adjust to the above name. Of course, the properties file is used here, Cassandra.properties:

cassandra.contactpoints=127.0.0.1

cassandra.port=9042
Cassandra.keyspace=mykeyspace

STEP4: 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 person = new person (integer.tostring (i), "name", I);
Persons.add (person);
}
Repository.save (persons);
Ct.close ();
}
}

Ok,everything is OK. It's very simple indeed.


Five minutes learning to use Spring-data-cassandra to quickly implement data access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.