如何使用astyanax和复合列在cassandra中执行范围查询

如何使用astyanax和复合列在cassandra中执行范围查询,cassandra,composite-key,astyanax,Cassandra,Composite Key,Astyanax,我正在使用cassandra和astyanax开发一个博客。当然,这只是一个练习 我以这种方式对CF_POST_INFO列族进行了建模: private static class PostAttribute { @Component(ordinal = 0) UUID postId; @Component(ordinal = 1) String category; @Component String name; public Pos

我正在使用cassandra和astyanax开发一个博客。当然,这只是一个练习

我以这种方式对CF_POST_INFO列族进行了建模:

private static class PostAttribute {

    @Component(ordinal = 0)
    UUID postId;

    @Component(ordinal = 1)
    String category;

    @Component
    String name;

    public PostAttribute() {}

    private PostAttribute(UUID postId, String category, String name) {
        this.postId = postId;
        this.category = category;
        this.name = name;
    }

    public static PostAttribute of(UUID postId, String category, String name) {
        return new PostAttribute(postId, category, name);
    }
}

    private static AnnotatedCompositeSerializer<PostAttribute> postSerializer = new AnnotatedCompositeSerializer<>(PostAttribute.class);

private static final ColumnFamily<String, PostAttribute> CF_POST_INFO =
        ColumnFamily.newColumnFamily("post_info", StringSerializer.get(), postSerializer);
        MutationBatch m = keyspace().prepareMutationBatch();

    ColumnListMutation<PostAttribute> clm = m.withRow(CF_POST_INFO, "posts")
            .putColumn(PostAttribute.of(post.getId(), "author", "id"), post.getAuthor().getId().get())
            .putColumn(PostAttribute.of(post.getId(), "author", "name"), post.getAuthor().getName())
            .putColumn(PostAttribute.of(post.getId(), "meta", "title"), post.getTitle())
            .putColumn(PostAttribute.of(post.getId(), "meta", "pubDate"), post.getPublishingDate().toDate());

    for(String tag : post.getTags()) {
        clm.putColumn(PostAttribute.of(post.getId(), "tags", tag), (String) null);
    }

    for(String category : post.getCategories()) {
        clm.putColumn(PostAttribute.of(post.getId(), "categories", category), (String)null);
    }
私有静态类PostAttribute{
@组件(序号=0)
UUID-posted;
@组件(序号=1)
字符串类别;
@组成部分
字符串名;
公共PostAttribute(){}
私有PostAttribute(UUID postId、字符串类别、字符串名称){
this.postId=postId;
this.category=类别;
this.name=名称;
}
的公共静态PostAttribute(UUID postId、字符串类别、字符串名称){
返回新的PostAttribute(postId、类别、名称);
}
}
私有静态AnnotatedCompositeSerializer postSerializer=新的AnnotatedCompositeSerializer(PostAttribute.class);
私有静态最终列系列CF\u POST\u信息=
newColumnFamily(“post_info”,StringSerializer.get(),postSerializer);
并以这种方式保存帖子:

private static class PostAttribute {

    @Component(ordinal = 0)
    UUID postId;

    @Component(ordinal = 1)
    String category;

    @Component
    String name;

    public PostAttribute() {}

    private PostAttribute(UUID postId, String category, String name) {
        this.postId = postId;
        this.category = category;
        this.name = name;
    }

    public static PostAttribute of(UUID postId, String category, String name) {
        return new PostAttribute(postId, category, name);
    }
}

    private static AnnotatedCompositeSerializer<PostAttribute> postSerializer = new AnnotatedCompositeSerializer<>(PostAttribute.class);

private static final ColumnFamily<String, PostAttribute> CF_POST_INFO =
        ColumnFamily.newColumnFamily("post_info", StringSerializer.get(), postSerializer);
        MutationBatch m = keyspace().prepareMutationBatch();

    ColumnListMutation<PostAttribute> clm = m.withRow(CF_POST_INFO, "posts")
            .putColumn(PostAttribute.of(post.getId(), "author", "id"), post.getAuthor().getId().get())
            .putColumn(PostAttribute.of(post.getId(), "author", "name"), post.getAuthor().getName())
            .putColumn(PostAttribute.of(post.getId(), "meta", "title"), post.getTitle())
            .putColumn(PostAttribute.of(post.getId(), "meta", "pubDate"), post.getPublishingDate().toDate());

    for(String tag : post.getTags()) {
        clm.putColumn(PostAttribute.of(post.getId(), "tags", tag), (String) null);
    }

    for(String category : post.getCategories()) {
        clm.putColumn(PostAttribute.of(post.getId(), "categories", category), (String)null);
    }
MutationBatch m=keyspace().prepareMutationBatch();
ColumnListCLM=m.withRow(CF_POST_INFO,“posts”)
.putColumn(PostAttribute.of(post.getId(),“author”,“id”),post.getAuthor().getId().get())
.putColumn(posttribute.of(post.getId(),“author”,“name”),post.getAuthor().getName())
.putColumn(PostAttribute.of(post.getId(),“meta”,“title”),post.getTitle())
.putColumn(posttribute.of(post.getId(),“meta”,“publidate”),post.getPublishingDate().toDate());
for(字符串标记:post.getTags()){
clm.putColumn(posattribute.of(post.getId(),“tags”,tag),(String)null);
}
for(字符串类别:post.getCategories()){
clm.putColumn(posttribute.of(post.getId(),“categories”,category),(String)null);
}
我们的想法是在某个时间有一些类似桶的行(例如每月或每年一行)

现在,如果我想得到最后5篇文章,我怎么能做一个愤怒的查询呢?我可以根据帖子id(UUID)执行rage查询,但如果不进行另一次查询以获取可用的帖子id,我就不知道可用的帖子id。卡桑德拉的最佳实践是什么


当然,欢迎对数据模型提出任何建议,我是cassandra的新手。

如果您的用例按照我认为的方式工作,您可以修改PostAttribute,使第一个组件是TimeUUID,这样您就可以将其存储为时间序列数据,并且可以使用标准技术轻松提取最旧的5或最新的5。不管怎样,这里有一个例子,它对我来说是什么样的,因为如果你已经在使用复合材料的话,你实际上不需要制作多个列

公共类PostInfo{
@组件(序号=0)
受保护的UUID timeUuid;
@组件(序号=1)
受保护的UUID posted;
@组件(序号=2)
受保护字符串类别;
@组件(序号=3)
受保护的字符串名称;
@组件(序号=4)
受保护的UUID作者;
@组件(序号=5)
受保护的字符串authorName;
@组件(序号=6)
受保护的字符串标题;
@组件(序号=7)
公布的受保护日期;
public PostInfo(){}
私有PostInfo(最终UUID postId、最终字符串类别、最终字符串名称、最终UUID authorId、最终字符串authorName、最终字符串标题、最终发布日期){
this.timeUuid=TimeUUIDUtils.getUniqueTimeuuidMillis();
this.postId=postId;
this.category=类别;
this.name=名称;
this.authorId=authorId;
this.authorName=authorName;
this.title=标题;
this.published=published;
}
的公共静态PostInfo(最终UUID postId、最终字符串类别、最终字符串名称、最终UUID authorId、最终字符串authorName、最终字符串标题、最终发布日期){
返回新的PostInfo(postId、类别、名称、作者、作者姓名、标题、已发布);
}
}
私有静态AnnotatedCompositeSerializer postInfoSerializer=新的AnnotatedCompositeSerializer(PostInfo.class);
私有静态最终列系列CF\u POSTS\u时间表=
newColumnFamily(“post_info”,StringSerializer.get(),postInfoSerializer);
您应该这样保存它:

MutationBatch m=keyspace().prepareMutationBatch();
ColumnListCLM=m.withRow(CF_发布时间线,“全部”/*或任何对您有意义的内容,如年或月或任何*/)
.putColumn(PostInfo.of(post.getId()、post.getCategory()、post.getName()、post.getAuthor().getId()、post.getAuthor().getName()、post.getPublishedOn()),/*作为列值,可能只有空字节*/)
m、 执行();
然后您可以这样查询:

OperationResult=getKeyspace()
.prepareQuery(参考发布时间线)
.getKey(“全部”/*或任何有意义的东西,如月份、年份等*/)
.withColumnRange(新RangeBuilder()
.setLimit(5)
.setReversed(真)
.build())
.execute();
ColumnList columns=result.getResult();
用于(列:列){
//在这里做你需要的
}

谢谢,是的,这正是我要找的。