2016-08-23 14 views
1

Aerospike 데이터베이스에 대한 쿼리를 만들려고합니다. 특정 데이터베이스에서 가장 높은 값을 반환합니다. MAX() 함수가 MySQL에서 작동하는 방식과 비슷합니다. 예를 들어,이 같은 집합을했다 :Aerospike Query Return 가장 높은 값

+--------------+---------+ 
| 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 (사용자 정의 함수)를 적용하여 결과를 효율적으로 필터링 할 수 있습니다.

예. 여기에 max를 사용하여 레코드를 리턴하는 스트림 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