如何在cassandra中使用java datastax进行筛选

falq053o  于 2021-06-14  发布在  Cassandra
关注(0)|答案(2)|浏览(497)
I created a table and two indexes on it i am not able to do filter query from my spring application

首先我创建了一个表

CREATE TABLE keyspace.favourite_setting (
id uuid,
user_name text,
filter_name text,
columns LIST<FROZEN<columns_def>>,
filters text,
PRIMARY KEY (id)

);
其次,我添加了两个索引

CREATE INDEX IF NOT EXISTS user_name_index ON keyspace.favourite_setting (user_name)
CREATE INDEX IF NOT EXISTS filter_name_index ON keyspace.favourite_setting (filter_name);

然后我试图过滤显示我的错误,你必须允许过滤,虽然我已经这样做了。。。
我的配饰

@Accessor
 public interface FavouriteSettingAccessor {

@Query("select * from favourite_setting where user_name =:userName allow filtering  " )
Result<FavouriteSetting> getAllByUserName(@Param("userName") String userName );

      }

我的服务实现

@Override
public List<FavouriteSetting> getAllFilterNames(String userName) throws  Exception {

    session = cassandraFactory.getDataSource();

    favouriteSettingAccessor =
            new MappingManager(session).createAccessor(FavouriteSettingAccessor.class);

    return favouriteSettingAccessor.getAllByUserName(userName).all();
}

我最喜欢的模特

@Table(keyspace = "keyspace", name = "favourite_setting")
public class FavouriteSetting {

@PartitionKey
@Column(name = "id")
private UUID id;

@Column(name = "user_name")
private String userName;

@Column(name = "filter_name")
private String filterName;

@Column(name = "columns")
private List<ColumnsDef> columns;

@Column(name = "filters")
private String filters;

 public FavouriteSetting(UUID id, String userName, String filterName, 
 List<ColumnsDef> columns, String filters) {
    this.id = id;
    this.userName = userName;
    this.filterName = filterName;
    this.columns = columns;
    this.filters = filters;
}

public FavouriteSetting() {
}

public UUID getId() {
    return id;
}

public void setId(UUID id) {
    this.id = id;
}

public String getFilterName() {
    return filterName;
}

public void setFilterName(String filterName) {
    this.filterName = filterName;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public List<ColumnsDef> getColumns() {
    return columns;
}

public void setColumns(List<ColumnsDef> columns) {
    this.columns = columns;
}

public String getFilters() {
    return filters;
}

public void setFilters(String filters) {
    this.filters = filters;
}

}
无法执行此查询,因为它可能涉及数据筛选,因此可能具有不可预知的性能。如果要在性能不可预测的情况下执行此查询,请使用“允许筛选”

fcg9iug3

fcg9iug31#

用于查询

@Query("select * from favourite_setting where user_name =:userName ... " )

用户名不是主键的一部分,可以将其设置为群集键。有关更多信息,请查看此处或此处。
请注意,没有alter语句可添加群集键,您需要使用以下语句重新创建表:

CREATE TABLE keyspace.favourite_setting (
id uuid,
user_name text,
filter_name text,
columns LIST<FROZEN<columns_def>>,
filters text,
PRIMARY KEY (id, username)

为username列创建的索引被认为是索引的反模式之一,因为它应该是唯一的(因此,它将具有很高的基数);你可以在这里找到更多关于为什么这是一个问题的信息 allow filtering 不应该使用,如这里所解释的 you should resist the urge to just add ALLOW FILTERING to it. You should think about your data, your model and what you are trying to do.

zyfwsgd6

zyfwsgd62#

它现在的工作方式是添加用户名作为集群键,并添加到查询允许筛选的末尾。。谢谢:)

相关问题