亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過遍歷 LIST 從 Aerospike 獲取記錄

如何通過遍歷 LIST 從 Aerospike 獲取記錄

Go
小怪獸愛吃肉 2022-12-19 17:51:40
我有數據庫結構,接近于此:+--------------+---------+------------------------------+| 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)但它不起作用。如果我使用主機名和某種方式解析數組來提供謂詞表達式,是否可以搜索記錄?更新:我發現,這種情況可以通過使用過濾器表達式來解決,尤其是使用 CDTContext。但是 Aerospike 文檔中關于此工具的使用信息非常少,尤其是關于 Go 代碼的信息。我只找到了這種方法,它可以提供幫助,但我不明白如何在我的情況下正確使用它:func aerospike.ExpListGetByValue(returnType aerospike.ListReturnType, value *aerospike.Expression, bin *aerospike.Expression, ctx ...*aerospike.CDTContext) *aerospike.ExpressionUPD2 通過使用 ExpListGetByValue 我試圖進行查詢,但它找不到任何東西。它看起來像這樣:stmt := aerospike.NewStatement(namespaceName, setName)qPolicy := aerospike.NewQueryPolicy()qPolicy.FilterExpression = aerospike.ExpEq(aerospike.ExpListGetByValue(這recordSet是空的,雖然“數字”箱確實包含這個numVal
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

我正在使用您在第二次更新中創建的新的和修改過的過濾器運行您的測試,它似乎有效。這是我使用的完整代碼(基于 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.


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號