微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Aerospike aql:如何使用基于 Map 字段的谓词从 aerospike 获取记录

如何解决Aerospike aql:如何使用基于 Map 字段的谓词从 aerospike 获取记录

下面是数据传输对象的类定义。我使用 spring-data-aerospike 来持久化。

    import org.springframework.data.annotation.Id;
    public class User implements Serializable {

        @Id
        String uid;

        Map<String,Object> ext = new HashMap<String,Object>();

        // getters & setters    
    }

数据库中的样本数据就像-

select ext  from department.User;
+-------+-------------------------+----------------------------------+
| PK    | ext                     || @_class                         |
+-------+-------------------------+----------------------------------+
| "123" | MAP('{"idfa": "xyz"}')  | "com.tut.dto.User"               |
| "234" | MAP('{}')               | "com.tut.dto.User"               |
+-------+-------------------------+----------------------------------+

我现在需要查询数据库,因为它应该只返回在 ext 字段列中具有“idfa”键字符串的记录。

我尝试了以下方法。但是没用

1.

 select * from test.UserRecord where ext.idfa is not null;
 Unsupported command format with token -  '.'
 Make sure string values are enclosed in quotes.
 Type " aql --help " from console or simply "help" from within the aql-prompt.
 ```
2. 

select * from test.UserRecord where ext contains 'idfa';
Unsupported command format with token -  ''idfa''
Make sure string values are enclosed in quotes.
Type " aql --help " from console or simply "help" from within the aql-prompt.

How can I make it work?


解决方法

尝试执行:

select * from department.user in mapkeys where ext = "idfa"

基于以下结构:

SELECT <bins> FROM <ns>[.<set>] IN <indextype> WHERE <bin> = <value>

假设您已经创建了一个集合类型为“MAPKEYS”的索引,您可以在 ext 类的 User 字段上使用 @Indexed 注释创建它。

如果您使用的是 Spring Data Aerospike 最新版本,它应该看起来像这样:

@Indexed(name = "indexName",type = IndexType.STRING,collectionType = IndexCollectionType.MAPKEYS)
Map<String,Object> ext = new HashMap<String,Object>();

如果您有兴趣通过代码(而不是 AQL)与 Aerospike 列表/地图箱交互,您应该阅读 CDT(集合数据类型)操作。

CDT 文档:

https://docs.aerospike.com/docs/guide/cdt.html

Map 操作类(包括方法文档):

https://github.com/aerospike/aerospike-client-java/blob/master/client/src/com/aerospike/client/cdt/MapOperation.java

以及 Aerospike Java 客户端中的一些示例:

https://github.com/aerospike/aerospike-client-java/blob/master/test/src/com/aerospike/test/sync/basic/TestOperateMap.java

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。