在golang中执行Aerospike查询

huangapple go评论111阅读模式
英文:

Aerospike query execution in golang

问题

在Golang中,可以通过使用mapvalues函数来实现以下查询:

package main

import (
	"fmt"
)

type Demo struct {
	ShoppingCart string
	// other fields
}

func main() {
	demos := map[string]Demo{
		"1": Demo{ShoppingCart: "FL"},
		"2": Demo{ShoppingCart: "NY"},
		"3": Demo{ShoppingCart: "FL"},
	}

	var results []Demo
	for _, demo := range demos {
		if demo.ShoppingCart == "FL" {
			results = append(results, demo)
		}
	}

	fmt.Println(results)
}

上述代码创建了一个名为Demo的结构体,其中包含了ShoppingCart字段。然后,我们创建了一个名为demos的映射,其中包含了一些示例数据。接下来,我们使用循环遍历映射中的每个元素,并检查ShoppingCart字段是否等于"FL"。如果是,则将该元素添加到结果切片中。最后,我们打印出结果切片。

请注意,这只是一种实现方式,具体的实现方式可能因你的需求和数据结构而有所不同。

英文:

Is there any way to implement below query in golang :

select * from test.demo in mapvalues where shopping_cart = "FL"

答案1

得分: 3

请查看sandbox中的示例:https://developer.aerospike.com/client/usage/queries/basic/secondary

以下是在sandbox中对canonical ufodata数据集进行范围查询的示例代码(当然,你首先需要创建二级索引):

// 创建新的查询策略
queryPolicy := aerospike.NewQueryPolicy()
queryPolicy.FilterExpression = aerospike.ExpGeoCompare(
    aerospike.ExpGeoBin("location"),
    aerospike.ExpGeoVal(region))

// 创建语句
stmt := aerospike.NewStatement("sandbox", "ufodata")

// 创建索引过滤器
stmt.SetFilter(aerospike.NewRangeFilter("occurred", 20210101, 20211231))

// 执行查询
recordSet, err := client.Query(queryPolicy, stmt)

// 获取结果
for records := range recordSet.Results() {
    if records != nil {
        // 做一些操作
        fmt.Printf("Record: %v \n", records.Record.Bins)
    }
}

recordSet.Close()

// 关闭与服务器的连接
client.Close()
英文:

Check out the examples in the sandbox: https://developer.aerospike.com/client/usage/queries/basic/secondary

Here is the sample code for a range query on the canonical ufodata set in the sandbox (you would first have to create the secondary index of course):

// Create new query policy
queryPolicy := aerospike.NewQueryPolicy()
queryPolicy.FilterExpression = aerospike.ExpGeoCompare(
    aerospike.ExpGeoBin("location"), 
    aerospike.ExpGeoVal(region))

// Create statement
stmt := aerospike.NewStatement("sandbox", "ufodata")

// Create index filter
stmt.SetFilter(aerospike.NewRangeFilter("occurred", 20210101, 20211231))

// Execute the query
recordSet, err := client.Query(queryPolicy, stmt)

// Get the results
for records := range recordSet.Results() {
    if records != nil { 
        // Do something
        fmt.Printf("Record: %v \n", records.Record.Bins)
    }
}

recordSet.Close()

// Close the connection to the server
client.Close()

答案2

得分: 0

你应该可以使用Aerospike的Go客户端。https://github.com/aerospike/aerospike-client-go

英文:

You should be able to use the Aerospike client for Go. https://github.com/aerospike/aerospike-client-go

huangapple
  • 本文由 发表于 2023年1月23日 18:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75208313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定