我想在执行时使用准备好的语句 CQL
在我的申请表里。这个功能看起来是由 ReactiveCqlTemplate
我已经通过了 ReactiveCassandraTemplate
在我的Cassandra配置中:
@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";
}
}
这就是 ScyllaPersonRepository
在我的cassandra配置过滤器中引用。
public interface ScyllaPersonRepository extends ReactiveCassandraRepository<Person, PersonKey> {
@Query("select id, name from persons where id = ?0")
Flux<Object> findPersonById(@Param("id") String id);
}
在执行了一些查询之后,我的scylla监控 Jmeter 板中的cql non prepared statements指标显示我根本没有使用prepared语句。
我能够使用事先准备好的语句,在这里的文档指导我创建 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在幕后协调它?
1条答案
按热度按时间r7knjye21#
坦白地说,我认为这是一个错误(增强请求),它应该在springs jira中提交。似乎存储库根本不支持这种开箱即用(我也没有找到任何配置选项如何翻转它,但我可能错过了它)。
实际上,我的理论是正确的:https://jira.spring.io/projects/datacass/issues/datacass-578?filter=allopenissues 所以只要加上你自己,试着向他们寻求解决方案。
hth卢布斯