登录
首页 >  Golang >  Go问答

使用迭代方法从 Aerospike 中检索记录的方法

来源:stackoverflow

时间:2024-02-13 20:27:23 261浏览 收藏

本篇文章给大家分享《使用迭代方法从 Aerospike 中检索记录的方法》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

我有数据库结构,接近于此:

+--------------+---------+------------------------------+
| hostname     |   cont  |         numbers              |
+--------------+---------+------------------------------+
|   host01     |{"k":"v"}|list(`[["1", "123456", ...]]`)|
|   host02     |{"k":"v"}|list(`[["1", "654321", ...]]`)|
+--------------+---------+------------------------------+

我想通过过滤“数字”和“主机名”来获取某些记录。 它应该包含一个具有特定数字的字符串,该字符串是我从其他组件获得的。在上面的代码中,它是“123456”

基本上我都是这样搜索的,直到需要二维数组:

stmt := aerospike.newstatement(namespacename, setname)
stmt.setpredexp(
 aerospike.newpredexpstringbin("hostname"),
            aerospike.newpredexpstringvalue(inputhostname),
            aerospike.newpredexpstringregex(0),
            aerospike.newpredexpstringbin("deleted"),
            aerospike.newpredexpstringvalue("true"),
            aerospike.newpredexpstringequal(),
            aerospike.newpredexpnot(),
            aerospike.newpredexpand(2))
)

我一直在寻找可能的方法来过滤数组,但没有得到任何解决方案。

我在网上找到的方法是这样的:

stmt := aerospike.newstatement("test", "settest")
qpolicy := aerospike.newquerypolicy()
qpolicy.predexp = append(qpolicy.predexp,
            aerospike.newpredexpstringvar("v"),
            aerospike.newpredexpstringvalue(id),
            aerospike.newpredexpstringequal(),
            aerospike.newpredexplistbin("numbers"),
            aerospike.newpredexplistiterateor("v"))
recordset, err := aerospikeclient.client.query(qpolicy, stmt)

但是这不起作用。

如果我使用主机名提供谓词表达式并以某种方式解析数组,是否可以搜索记录?

upd

我发现,这种情况可以通过使用过滤器表达式来解决,特别是使用 cdtcontext。但是 aerospike 文档中关于该工具使用的信息非常少,尤其是关于 go 代码的信息。 我只找到了这个方法,它可以提供帮助,但我不明白如何在我的情况下正确使用它:

func aerospike.explistgetbyvalue(returntype aerospike.listreturntype, value *aerospike.expression, bin *aerospike.expression, ctx ...*aerospike.cdtcontext) *aerospike.expression

upd2 通过使用 explistgetbyvalue 我尝试进行查询,但找不到任何内容。它看起来像这样:

stmt := aerospike.newstatement(namespacename, setname)
qpolicy := aerospike.newquerypolicy()
qpolicy.filterexpression = aerospike.expeq(
aerospike.explistgetbyvalue(
    aerospike.listreturntypecount,
    aerospike.expstringval(numbval),
    aerospike.explistbin("numbers")),
aerospike.expintval(1))

recordset, err := aerospikeclient.client.query(qpolicy, stmt)

recordset 为空,但“数字”箱确实包含此 numval

upd3 问题解决了。我添加了嵌套数组的上下文。完整的过滤器表达式将如下所示

qPolicy.FilterExpression = aerospike.ExpEq(
            aerospike.ExpListGetByValue(
                aerospike.ListReturnTypeCount,
                aerospike.ExpStringVal("123456"),
                aerospike.ExpListBin("numbers"),
                aerospike.CtxListIndex(-1)),
            aerospike.ExpIntVal(1))

正确答案


我正在使用您在第二次更新中创建的新的和修订的过滤器来运行您的测试,它似乎有效。以下是我使用的完整代码(基于 git 存储库中的 aerospike 示例目录):

/*
 * copyright 2014-2022 aerospike, inc.
 *
 * portions may be licensed to aerospike, inc. under one or more contributor
 * license agreements.
 *
 * licensed under the apache license, version 2.0 (the "license"); you may not
 * use this file except in compliance with the license. you may obtain a copy of
 * the license at http://www.apache.org/licenses/license-2.0
 *
 * unless required by applicable law or agreed to in writing, software
 * distributed under the license is distributed on an "as is" basis, without
 * warranties or conditions of any kind, either express or implied. see the
 * license for the specific language governing permissions and limitations under
 * the license.
 */

package main

import (
    "log"

    as "github.com/aerospike/aerospike-client-go/v5"
    shared "github.com/aerospike/aerospike-client-go/v5/examples/shared"
)

func main() {
    testlistexp(shared.client)

    log.println("example finished successfully.")
}

func testlistexp(client *as.client) {
    log.printf("test list expression")
    key, _ := as.newkey(*shared.namespace, *shared.set, "mapkey1")
    client.delete(shared.writepolicy, key)

    binhostname := as.newbin("hostname", "host01")

    amap := map[string]string{
        "k": "v",
    }

    list := []string{
        "1",
        "123456",
        "1111",
        "222",
    }

    bincont := as.newbin("cont", amap)
    binnumbers := as.newbin("numbers", list)

    client.putbins(shared.writepolicy, key, binhostname, bincont, binnumbers)

    key, _ = as.newkey(*shared.namespace, *shared.set, "mapkey2")
    binhostname = as.newbin("hostname", "host02")
    list = []string{
        "1",
        "654321",
        "2222",
        "3333",
    }
    binnumbers = as.newbin("numbers", list)
    client.putbins(shared.writepolicy, key, binhostname, bincont, binnumbers)

    // numbval := "654321"

    getpolicy := as.newquerypolicy()
    getpolicy.filterexpression =
        as.expeq(
            as.explistgetbyvalue(
                as.listreturntypecount,
                as.expstringval("654321"),
                as.explistbin("numbers")),
            as.expintval(1))

    stmt := as.newstatement(*shared.namespace, *shared.set)
    recordset, err := client.query(getpolicy, stmt)
    shared.paniconerror(err)

    for res := range recordset.results() {
        log.println(res.record.bins)
    }

}

输出是:

$ go run listExpression.go  -h 172.17.0.3
2022/06/08 12:00:19 hosts:              172.17.0.3
2022/06/08 12:00:19 port:               3000
2022/06/08 12:00:19 user:
2022/06/08 12:00:19 password:           *
2022/06/08 12:00:19 namespace:          test
2022/06/08 12:00:19 set:                testset
2022/06/08 12:00:19 Test List Expression
2022/06/08 12:00:19 map[cont:map[k:v] hostname:host02 numbers:[1 654321 2222 3333]]
2022/06/08 12:00:19 Example finished successfully.

以上就是《使用迭代方法从 Aerospike 中检索记录的方法》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>