首页>>后端>>Spring->Spring整合Cassandra的简单使用

Spring整合Cassandra的简单使用

时间:2023-11-30 本站 点击:0

之前写了JAVA操作cassandra驱动包,现在来看看spring-data对cassandra的支持。这里是spring-data-cassandra的官方文档:http://docs.spring.io/spring-data/cassandra/docs/1.5.0.M1/reference/html/ 这个目录下还有api、版本日志等:http://docs.spring.io/spring-data/cassandra/docs/1.5.0.M1/

引入jar包

<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-cassandra</artifactId><version>1.5.0.M1</version></dependency>

定义bean

importorg.springframework.data.cassandra.mapping.Column;importorg.springframework.data.cassandra.mapping.PrimaryKey;importorg.springframework.data.cassandra.mapping.Table;@TablepublicclassPerson{//主键@PrimaryKeyprivateStringid;//列名与数据库列名一致时可不加@Column(value="name")privateStringname;privateintage;//支持构造函数publicPerson(Stringid,Stringname,intage){this.id=id;this.name=name;this.age=age;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){return"Person[id="+id+",name="+name+",age="+age+"]";}}

对应的CQL建表语句

CREATETABLEmydb.person(idtextPRIMARYKEY,ageint,nametext)

可以看出和JPA的注解很类似,不同的是cassandra主键用的是@PrimaryKey,而且允许使用构造函数。如果存在复合主键,则要先映射一个主键的实体类,再映射一个包含这个主键的实体类。

例如:

importorg.springframework.cassandra.core.Ordering;importorg.springframework.cassandra.core.PrimaryKeyType;importorg.springframework.data.cassandra.mapping.PrimaryKeyClass;importorg.springframework.data.cassandra.mapping.PrimaryKeyColumn;@PrimaryKeyClasspublicclassPerson2Key{//分区键@PrimaryKeyColumn(name="id",ordinal=0,type=PrimaryKeyType.PARTITIONED)privateStringid;//集群键@PrimaryKeyColumn(name="name",ordinal=1,type=PrimaryKeyType.CLUSTERED,ordering=Ordering.DESCENDING)privateStringname;publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@OverridepublicStringtoString(){return"Person2Key[id="+id+",name="+name+"]";}}
importorg.springframework.data.cassandra.mapping.PrimaryKey;importorg.springframework.data.cassandra.mapping.Table;@Table(value="person2")publicclassPerson2{@PrimaryKeyprivatePerson2KeypKey;privateintage;publicPerson2KeygetpKey(){returnpKey;}publicvoidsetpKey(Person2KeypKey){this.pKey=pKey;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}@OverridepublicStringtoString(){return"Person2[pKey="+pKey+",age="+age+"]";}}

对应的CQL建表语句

CREATETABLEmydb.person2(idtext,nametext,ageint,PRIMARYKEY(id,name))WITHCLUSTERINGORDERBY(nameDESC)

其中的WITH CLUSTERING ORDER BY (name DESC) 对应主键类里的ordering = Ordering.DESCENDING,按照name降序存储,只有集群键才能在建表时设置降序存储。

定义spring-data接口

importjava.util.List;importorg.springframework.data.cassandra.repository.Query;importorg.springframework.data.repository.CrudRepository;importorg.springframework.stereotype.Repository;importcom.my.domin.pojo.Person2;@RepositorypublicinterfacePersonRepositoryextendsCrudRepository<Person2,String>{@Query("select*fromPerson2whereid=?1andname=?2")List<Person2>findByIdAndName(Stringid,Stringname);}

这里面实现了一组CURD方法,如果要写一些条件查询的话可以参考

@Query("select*fromPersonwhereid=?1andname=?2ALLOWFILTERING")List<Person>findByIdAndName(Stringid,Stringname);

spring-data-cassandra文档里还提到一个分页的仓库接口类PagingAndSortingRepository,这个继承自CrudRepository,而且提供了2个分页方法。

测试方法:

importjava.util.Iterator;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.cassandra.core.CassandraOperations;importorg.springframework.stereotype.Service;importcom.datastax.driver.core.querybuilder.QueryBuilder;importcom.datastax.driver.core.querybuilder.Select;importcom.my.domin.pojo.Person;importcom.my.repository.PersonRepository;importcom.my.service.PersonService;@ServicepublicclassPersonServiceImplimplementsPersonService{@AutowiredprivatePersonRepositorypersonRepository;@AutowiredprivateCassandraOperationscassandraOperations;@Overridepublicvoidtest(){//通过Repository查询Iterable<Person>iterable=personRepository.findAll();Iterator<Person>it=iterable.iterator();System.out.println("==>findAll:");while(it.hasNext()){Personp=it.next();System.out.println(p.toString());}//通过Repository自定义查询查询List<Person>list=personRepository.findByIdAndName("1","one");System.out.println("==>findByIdAndName:");for(Personperson:list){System.out.println(person.toString());}//通过cassandraOperations查询Selectselect=QueryBuilder.select().from("person");select.where(QueryBuilder.eq("id","1"));Personperson=cassandraOperations.selectOne(select,Person.class);System.out.println("==>cassandraOperations:");System.out.println(person.toString());}}

输出结果


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:/Spring/4450.html