2016-08-23 10 views
1

Aerospikeデータベースのクエリを作成しようとしていますが、特定のビンで最も高い値を返します。 MAX()関数がMySQLで動作する方法に似ています。たとえば、次のようなセットがあるとします。Aerospike Query Return Highest Value

+--------------+---------+ 
| 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")) 

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

誰でもこれを行う方法はありますか?

+0

私はAerospikeについてよくわかりませんが、ドキュメントをすばやく見ていますUDF(User Defined Function)を指摘した。 http://www.aerospike.com/docs/client/go/usage/query/query_udf.html – jnmoal

答えて

4

クエリにLuaユーザー定義関数(UDF)を適用して、結果を効率的にフィルタリングできます。

など。ここではストリームUDFを使用して、最大値のレコードを返します。あなたは見つけることができますhttps://gist.github.com/jhecking/b98783bea7564d610ea291b5ac47808c

:私はここに要旨として完全に動作する例をアップロードした

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"]) 
} 

:バージョン番号:あなたはこのように関数を実行でしょう囲碁クライアントを使用して

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 

クエリ集約のためにストリームUDFを使用する方法の詳細はこちらhttp://www.aerospike.com/docs/guide/aggregation.html

+1

ありがとうございました。私は静かに恐れていましたが、私はUDFになる必要がありましたが、最初に機能が準備されていないことを確かめたいと思っていました。私はこれまでに航空宇宙でこれほど遠くに行かず、私のルアの経験は文字通りゼロであるため、これは大きな助けになるでしょう:) – Cory

関連する問題