天天看點

飛5的Spring Boot2(29)- Cassandra

Cassandra

Cassandra是一個開源,分布式資料庫管理系統,設計用于處理跨很多商品伺服器的大資料。Spring Boot為Cassandra提供自動配置,Spring Data Cassandra提供在它之上的抽象,還有收集依賴的spring-boot-starter-data-cassandra‘Starter’。

飛5的Spring Boot2(29)- Cassandra

連接配接Cassandra

你可以注入一個自動配置的CassandraTemplate或Cassandra Session執行個體,就像注入其他Spring Bean那樣。spring.data.cassandra.*屬性可用來自定義該連接配接,通常你需要提供keyspace-name和contact-points屬性:

1spring.data.cassandra.keyspace-name=mykeyspace
2spring.data.cassandra.contact-points=cassandrahost1,cassandrahost2      
1@Component
2public class MyBean {
3    private CassandraTemplate template;
4    @Autowired
5    public MyBean(CassandraTemplate template) {
6        this.template = template;
7    }
8    // ...
9}      

如果添加自己的CassandraTemplate類型的@Bean,它将替換預設執行個體。

Spring Data Cassandra倉庫

Spring Data包含的倉庫對Cassandra提供基本支援,目前受到的限制比先前讨論的JPA倉庫要多,并且需要使用@Query注解相應的查找方法。