javascript
cassandra使用心得_使用Spring Data Cassandra缓存的预备语句
cassandra使用心得
今天,我有一篇簡短的文章,內容涉及在Spring Data Cassandra中使用Prepared Statements。 Spring為您提供了一些實用程序,使您可以更輕松地使用“預備語句”,而不是依靠自己使用Datastax Java驅動程序手動注冊查詢。 Spring代碼提供了一個緩存來存儲經常使用的準備好的語句。 允許您通過緩存執行查詢,緩存可以從緩存中檢索準備好的查詢,也可以在執行之前添加一個新查詢。
為了簡短起見,我們可能應該開始看一些代碼。
依存關系
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version> </parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-cassandra</artifactId></dependency> </dependencies>使用Spring Boot 2.0.5.RELEASE將拉入Spring Data Cassandra的2.0.10.RELEASE 。
使用準備好的語句
讓我們直接進入:
@Repository public class PersonRepository extends SimpleCassandraRepository<Person, PersonKey> {private final Session session;private final CassandraOperations cassandraTemplate;private final PreparedStatementCache cache = PreparedStatementCache.create();public PersonRepository(Session session,CassandraEntityInformation entityInformation,CassandraOperations cassandraTemplate) {super(entityInformation, cassandraTemplate);this.session = session;this.cassandraTemplate = cassandraTemplate;}// using ORMpublic List<Person> findByFirstNameAndDateOfBirth(String firstName, LocalDate dateOfBirth) {return cassandraTemplate.getCqlOperations().query(findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),(row, rowNum) -> cassandraTemplate.getConverter().read(Person.class, row));}private BoundStatement findByFirstNameAndDateOfBirthQuery(String firstName, LocalDate dateOfBirth) {return CachedPreparedStatementCreator.of(cache,select().all().from("people_by_first_name").where(eq("first_name", bindMarker("first_name"))).and(eq("date_of_birth", bindMarker("date_of_birth")))).createPreparedStatement(session).bind().setString("first_name", firstName).setDate("date_of_birth", toCqlDate(dateOfBirth));}private com.datastax.driver.core.LocalDate toCqlDate(LocalDate date) {return com.datastax.driver.core.LocalDate.fromYearMonthDay(date.getYear(), date.getMonth().getValue(), date.getDayOfMonth());}// without ORMpublic List<Person> findByFirstNameAndDateOfBirthWithoutORM(String firstName, LocalDate dateOfBirth) {return cassandraTemplate.getCqlOperations().query(findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),(row, rowNum) -> convert(row));}private Person convert(Row row) {return new Person(new PersonKey(row.getString("first_name"),toLocalDate(row.getDate("date_of_birth")),row.getUUID("person_id")),row.getString("last_name"),row.getDouble("salary"));}private LocalDate toLocalDate(com.datastax.driver.core.LocalDate date) {return LocalDate.of(date.getYear(), date.getMonth(), date.getDay());} }這里有相當數量的樣板代碼,因此我們可以訪問Spring Data的ORM。 我還提供了代碼來演示如何在不使用ORM的情況下實現相同的目標(無論如何,它還是直接從查詢直接映射到對象)。
讓我們更仔細地研究一種方法:
public List<Person> findByFirstNameAndDateOfBirth(String firstName, LocalDate dateOfBirth) {return cassandraTemplate.getCqlOperations().query(findByFirstNameAndDateOfBirthQuery(firstName, dateOfBirth),(row, rowNum) -> cassandraTemplate.getConverter().read(Person.class, row)); }private BoundStatement findByFirstNameAndDateOfBirthQuery(String firstName, LocalDate dateOfBirth) {return CachedPreparedStatementCreator.of(cache,select().all().from("people_by_first_name").where(eq("first_name", bindMarker("first_name"))).and(eq("date_of_birth", bindMarker("date_of_birth")))).createPreparedStatement(session).bind().setString("first_name", firstName).setDate("date_of_birth", toCqlDate(dateOfBirth)); }CachedPreparedStatementCreator完全按照其說的進行操作…它創建緩存的Prepared Statements。 of方法采用實例化Bean時定義的cache ,并創建第二個參數定義的新查詢。 如果查詢是最近已經注冊的查詢,即它已經在緩存中。 然后,從那里開始查詢,而不是完成注冊新語句的整個過程。
傳入的查詢是一個RegularStatement ,可以通過調用createPreparedStatement將它轉換為PreparedStatement (我猜是吧)。 現在,我們可以將值綁定到查詢,因此它實際上可以做一些有用的事情。
就緩存Prepared Statements而言,這就是您要做的全部。 還有其他方法可以執行此操作,例如,您可以手動使用PreparedStatementCache或定義自己的緩存實現。 無論您的船浮在水上。
您現在已經到了這篇簡短文章的結尾,希望它實際上包含了足夠有用的信息……
在本文中,我們介紹了如何使用CachedPreparedStatementCreator創建和將Prepared Statements放入高速緩存中,以便在以后更快地執行。 使用Spring Data提供的類,我們可以減少需要編寫的代碼量。
這篇文章中使用的代碼可以在我的GitHub上找到 。
如果您發現此帖子有幫助,可以在Twitter上@LankyDanDev關注我,以了解我的新帖子。
翻譯自: https://www.javacodegeeks.com/2018/10/cached-prepared-statements-cassandra.html
cassandra使用心得
總結
以上是生活随笔為你收集整理的cassandra使用心得_使用Spring Data Cassandra缓存的预备语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机不支持NFC功能怎么办
- 下一篇: pivotal_Spring Data