【发布时间】:2020-09-27 18:12:16
【问题描述】:
我想在我的应用程序中执行CQL 时使用准备好的语句。这个功能看起来是由 ReactiveCqlTemplate 类提供的,我已经在我的 Cassandra 配置中将它传递到 ReactiveCassandraTemplate 中:
@Configuration
@EnableReactiveCassandraRepositories(
basePackages = "com.my.app",
includeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {ScyllaPersonRepository.class})
})
public class CassandraConfiguration extends AbstractReactiveCassandraConfiguration {
@Value("${cassandra.host}")
private String cassandraHost;
@Value("${cassandra.connections}")
private Integer cassandraConnections;
@Override
public CassandraClusterFactoryBean cluster() {
PoolingOptions poolingOptions = new PoolingOptions()
.setCoreConnectionsPerHost(HostDistance.LOCAL, cassandraConnections)
.setMaxConnectionsPerHost(HostDistance.LOCAL, cassandraConnections*2);
CassandraClusterFactoryBean bean = super.cluster();
bean.setJmxReportingEnabled(false);
bean.setPoolingOptions(poolingOptions);
bean.setLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
return bean;
}
@Override
public ReactiveCassandraTemplate reactiveCassandraTemplate() {
return new ReactiveCassandraTemplate(reactiveCqlTemplate(), cassandraConverter());
}
@Bean
public CassandraEntityInformation getCassandraEntityInformation(CassandraOperations cassandraTemplate) {
CassandraPersistentEntity<Person> entity =
(CassandraPersistentEntity<Person>)
cassandraTemplate
.getConverter()
.getMappingContext()
.getRequiredPersistentEntity(Person.class);
return new MappingCassandraEntityInformation<>(entity, cassandraTemplate.getConverter());
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
public String getContactPoints() {
return cassandraHost;
}
public String getKeyspaceName() {
return "mykeyspace";
}
}
这是我的 Cassandra 配置过滤器中引用的 ScyllaPersonRepository。
public interface ScyllaPersonRepository extends ReactiveCassandraRepository<Person, PersonKey> {
@Query("select id, name from persons where id = ?0")
Flux<Object> findPersonById(@Param("id") String id);
}
执行几个查询后,Scylla Monitoring Dashboard 中的 CQL Non-Prepared statements 指标显示我根本没有使用准备好的语句。
在遵循文档here 之后,我能够使用准备好的语句,该文档引导我自己创建了CQL。
public class ScyllaPersonRepository extends SimpleReactiveCassandraRepository<Person, PersonKey> {
private final Session session;
private final CassandraEntityInformation<Person, PersonKey> entityInformation;
private final ReactiveCassandraTemplate cassandraTemplate;
private final PreparedStatementCache cache = PreparedStatementCache.create();
public ScyllaPersonRepository(
Session session,
CassandraEntityInformation<Person, PersonKey> entityInformation,
ReactiveCassandraTemplate cassandraTemplate
) {
super(entityInformation, cassandraTemplate);
this.session = session;
this.entityInformation = entityInformation;
this.cassandraTemplate = cassandraTemplate;
}
public Flux<ScyllaUser> findSegmentsById(String id) {
return cassandraTemplate
.getReactiveCqlOperations()
.query(
findPersonByIdQuery(id),
(row, rowNum) -> convert(row)
);
}
private BoundStatement findPersonByIdQuery(String id) {
return CachedPreparedStatementCreator.of(
cache,
QueryBuilder.select()
.column("id")
.column("name")
.from("persons")
.where(QueryBuilder.eq("id", QueryBuilder.bindMarker("id"))))
.createPreparedStatement(session)
.bind()
.setString("id", id);
}
private Person convert(Row row) {
return new Person(
row.getString("id"),
row.getString("name"));
}
}
但是,我真的希望 ORM 为我处理这一切。是否可以开箱即用地配置此行为,这样我就不需要自己手动编写CQL,而只需在我的 Cassandra 配置中将其作为一个选项启用,并让 ORM 在幕后编排它?
【问题讨论】:
标签: java spring-boot cassandra scylla