I have a range query with three restrictions. i wrote a StreamUDF with one filter function for every condition and it didn´t work.
The query:
SELECT id1, id2, link_type, visibility, data, time, version FROM linktable
WHERE id1 = <id1> AND
link_type = <link_type> AND
time >= <minTime> AND
time <= <maxTimestamp> AND
visibility = VISIBILITY_DEFAULT
ORDER BY time DESC LIMIT <offset>, <limit>;
local function map_links(record)
-- Add user and password to returned map.
-- Could add other record bins here as well.
return record.id2
end
function check_id1(stream,id1)
local function filter_id1(record)
return record.id1 == id1
end
return stream : filter(filter_id1) : map(map_links)
end
function check_linktype(stream,link_type)
local function filter_linktype(record)
return record.link_type == link_type
end
return stream : filter(filter_linktype) : map(map_links)
end
function check_visibility(stream,visibility)
local function filter_visibility(record)
return record.visibility == visibility
end
return stream : filter(filter_visibility) : map(map_links)
end
is there any solution to filter the stream record in one lua function with 3 filters ? this will be than called with three parameters in java (Value.get()). Is there any tutorial ?
You have to have all your filter functions chained together. Here is a sample data set I created for your problem to illustrate the solution, followed by the UDF and the results. I tested via AQL.
AGGREGATE filterRecords.check_visibility(1,0,3,1515,1545) ON test.links
±-----------------------------------------------------------------------------+
| check_visib… |
±-----------------------------------------------------------------------------+
| MAP(‘{“data”:“abc11”, “time”:1520, “visibility”:1, “id”:3, “link_type”:0}’) |
±-----------------------------------------------------------------------------+
1 row in set (0.064 secs)
Here is the UDF used:
pgupta@ubuntu:~/testProjects/discuss3637$ more filterRecords.lua
local function rec_to_map(rec)
local xrec = map()
for i, bin_name in ipairs(record.bin_names(rec)) do
xrec[bin_name] = rec[bin_name]
end
return xrec
end
function check_visibility(stream, vis, linktype, id, mintime, maxtime)
local function range_filter(rec)
local val = rec['time']
if val >= mintime and val <= maxtime then
return true
else
return false
end
end
local function idcheck(rec)
local val = rec['id']
if val == id then
return true
else
return false
end
end
local function linktypecheck(rec)
local val = rec['link_type']
if val == linktype then
return true
else
return false
end
end
local function vischeck(rec)
local val = rec['visibility']
if val == vis then
return true
else
return false
end
end
return stream:filter(range_filter):filter(idcheck):filter(linktypecheck):filter(vischeck):map(rec_to_map)
end
Note: You could have used a secondary index on timestamp and filtered the initial stream in your query Statement being fed to the stream udf. Your java implementation should either specify one stream udf in the setAggregationFuntion() or call one stream UDF in queryAggregate() - use the correct overloaded function.
I’m searching some solutions for multi-filter.I read your solution,It seem to the all relationship among the filter is and.How can I combine them with logic or flexibly.From the official document,th section the Predicate Filtering seem to be helpful.But I can’t find any detail for Java client.Is it available for C client only?
I am trying to use the API you mentioned, but I run into some trouble at present. When I query with two conditions, I can find out the data, but when I used three conditions, it doesn’t work.