Aerospike查询返回最高值

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

Aerospike Query Return Highest Value

问题

我正在尝试为我的Aerospike数据库创建一个查询,该查询将返回特定bin中的最高值,类似于MySQL中MAX()函数的工作方式。例如,如果我有一个这样的集合:

+--------------+---------+
| filename     | version |
+--------------+---------+
| alphabet.doc | 4       |
| people.doc   | 2       |
| alphabet.doc | 6       |
| people.doc   | 3       |
+--------------+---------+

我需要的是只返回具有最高版本号的文件名。目前,我可以添加一个类似这样的过滤器:

stmt := db.NewStatement(DBns, DBset, "filename", "version")
stmt.Addfilter(db.NewEqualFilter("filename", "alphabet.doc"))

// 运行数据库查询
records := runQuery(stmt)

有人知道如何做到这一点吗?

英文:

I'm trying to create a query for my Aerospike database, that would return the highest value in a specific bin; similar to the way that the MAX() function works in MySQL. For example, if I had a set like this:

+--------------+---------+
| filename     | version |
+--------------+---------+
| alphabet.doc | 4       |
| people.doc   | 2       |
| alphabet.doc | 6       |
| people.doc   | 3       |
+--------------+---------+

What I need is to only return the filename with the highest version number. At the moment I can add a filter like this:

    stmt := db.NewStatement(DBns, DBset, "filename", "version")
    stmt.Addfilter(db.NewEqualFilter("filename", "alphabet.doc"))

    // run database query
    records := runQuery(stmt)

Anyone know how to do this?

答案1

得分: 4

您可以将Lua用户定义的函数(UDF)应用于查询,以高效地过滤结果。

例如,这是一个流式UDF示例,它将返回具有最大版本号的记录:

function maxVersion(stream, bin)
  -- 流函数无法直接返回记录对象,因此我们首先需要映射到Map数据类型。
  local function toArray(rec)
    local result = map()
    result['filename'] = rec['filename']
    result['version'] = rec['version']
    return result
  end
  local function findMax(a, b)
    if a.version > b.version then
      return a
    else
      return b
    end
  end
  return stream : map(toArray) : reduce(findMax)
end

使用Go客户端,您可以像这样执行该函数:

stmt := NewStatement(ns, set)
recordset, _ := client.QueryAggregate(nil, stmt, "udfFilter", "maxVersion")

for rec := range recordset.Results() {
  res := rec.Record.Bins["SUCCESS"].(map[interface{}]interface{})
  fmt.Printf("filename with max. version: %s (ver. %d)\n", res["filename"], res["version"])
}

我已经将一个完整可工作的示例上传到了Gist上,链接在这里:https://gist.github.com/jhecking/b98783bea7564d610ea291b5ac47808c

您可以在这里找到有关如何使用流式UDF进行查询聚合的更多信息:http://www.aerospike.com/docs/guide/aggregation.html

英文:

You can apply a Lua user-defined function (UDF) to the query to filter the results efficiently.

E.g. here is a Stream UDF that would return the record with the max. version number:

function maxVersion(stream, bin)
  -- The stream function cannot return record objects directly,
  -- so we have to map to a Map data type first.
  local function toArray(rec)
    local result = map()
    result['filename'] = rec['filename']
    result['version'] = rec['version']
    return result
  end
  local function findMax(a, b)
    if a.version > b.version then
      return a
    else
      return b
    end
  end
  return stream : map(toArray) : reduce(findMax)
end

Using the Go client you would execute the function like this:

  stmt := NewStatement(ns, set)
  recordset, _ := client.QueryAggregate(nil, stmt, "udfFilter", "maxVersion")

  for rec := range recordset.Results() {
    res := rec.Record.Bins["SUCCESS"].(map[interface{}]interface{})
    fmt.Printf("filename with max. version: %s (ver. %d)\n", res["filename"], res["version"])
}

I've uploaded a fully working example as a Gist here: https://gist.github.com/jhecking/b98783bea7564d610ea291b5ac47808c

You can find more information about how to work with Stream UDFs for query aggregation here: http://www.aerospike.com/docs/guide/aggregation.html

huangapple
  • 本文由 发表于 2016年8月23日 20:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/39100766.html
匿名

发表评论

匿名网友

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

确定