Cassandra Spring 数据多租户

发布于 2025-01-16 07:37:23 字数 481 浏览 4 评论 0原文

因此,我即将将多租户(每个租户一个键空间)方面与 spring-boot-starter-data-cassandra-reactive 集成,我尝试找到一种解决方案来访问正确的键空间,我发现了这种使用查询构建器:

Select select = QueryBuilder.select().from(tenantId,"counter");
select.where(QueryBuilder.eq("id", 1));
Mono<Counter> flux = reactiveCassandraOperations.selectOne(select.toString(), Counter.class);

我发现它在开发时间方面成本高昂,特别是如果我们有插入请求.. 根据您的经验,有一个实用的解决方案,我可以使用 ReactiveCassandraRepository 方法(保存、更新、findById ..)插入键空间名称,

请您支持并提前致谢。

So, I'm about to integrate the Multitenancy (one keyspace per tenant) aspect with spring-boot-starter-data-cassandra-reactive, I tried to find a solution to access into the right keyspace and I found this approach of using the query-builder:

Select select = QueryBuilder.select().from(tenantId,"counter");
select.where(QueryBuilder.eq("id", 1));
Mono<Counter> flux = reactiveCassandraOperations.selectOne(select.toString(), Counter.class);

I found it costly in term of development time, especially if we have an insert request ..
Within your experience is there a practical solution where I can insert the keyspace name with the using of the ReactiveCassandraRepository methods ( save, update, findById ..)

Can you support please and thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2025-01-23 07:37:23

不是 Cassandra 或 Spring 专家,但我被告知您应该使用 ReactiveCqlTemplate (或 ReactiveCqlOperations)而不是 ReactiveCassandraTemplate (或 ReactiveCassandraOperations)。请参阅此链接 https://github.com/spring-projects/spring -data-cassandra/issues/1218 并点击此页面中的链接。

从链接中,您可以从 CassandraTemplate 获取 CqlOperations,如下所示

private final ReactiveCqlOperations cqlOps;
private final YourRepository repository;

@Autowired
public YourService(YourRepository repository, ReactiveCassandraTemplate reactiveCassandraTemplate){
    this.repository = repository;
    this.cqlOps = reactiveCassandraTemplate.getReactiveCqlOperations();
}

然后在您的存储库中

public interface YourRepository extends ReactiveCassandraRepository<YourData, Long> {

default Flux<YourData> getCQL(ReactiveCqlOperations cqlOps, YourData yourData) {
    return cqlOps.query("SELECT * FROM genericdatadb.YourData WHERE val0 = ? AND val1 = ?;",
            YourRepository.YourDataMapper.INSTANCE,
            yourData.getVal0(),
            yourData.getVal1());
}

在查询中 genericdatadb 是键空间,YourData 是表。从这里,您可以添加“?”其中 genericdatadb 是或使用 String.format 语句创建查询字符串。也许 Cassandra 专家有更好的解决方案?

Not a Cassandra or Spring expert but I am told that you should use ReactiveCqlTemplate (or ReactiveCqlOperations) rather than ReactiveCassandraTemplate (or ReactiveCassandraOperations). See this link https://github.com/spring-projects/spring-data-cassandra/issues/1218 and follow the links in this page.

From the link, you may get the CqlOperations fronm the CassandraTemplate as follows

private final ReactiveCqlOperations cqlOps;
private final YourRepository repository;

@Autowired
public YourService(YourRepository repository, ReactiveCassandraTemplate reactiveCassandraTemplate){
    this.repository = repository;
    this.cqlOps = reactiveCassandraTemplate.getReactiveCqlOperations();
}

And then in your repository

public interface YourRepository extends ReactiveCassandraRepository<YourData, Long> {

default Flux<YourData> getCQL(ReactiveCqlOperations cqlOps, YourData yourData) {
    return cqlOps.query("SELECT * FROM genericdatadb.YourData WHERE val0 = ? AND val1 = ?;",
            YourRepository.YourDataMapper.INSTANCE,
            yourData.getVal0(),
            yourData.getVal1());
}

In the query genericdatadb is the keyspace and YourData is the table. From here, you can add a '?' where genericdatadb is or use a String.format statement to create the query String. Maybe there are Cassandra experts with better solutions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文