Search code examples
javaluastreamudfaerospike

How to write an Aerospike stream UDF for a range Query


I wrote a Stream UDF for a range Query and it doesn't work properly. have you any idea how to set many filters with lua ?

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>;

Java code to invoke this lua function:

stmt = new Statement();
stmt.setNamespace(dbid);
stmt.setSetName("links");
stmt.setIndexName("time");
stmt.setFilters(Filter.range("time", minTimestamp, maxTimestamp));
stmt.setAggregateFunction("linkbench", "check_id1", Value.get(id1));
stmt.setAggregateFunction("linkbench", "check_linktype", Value.get(link_type));
resultSet = client.queryAggregate(null, stmt, "linkbench", "check_visibility", Value.get(VISIBILITY_DEFAULT));

Lua Script:

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

Any idea how to write the filter for all the query restrictions ?

Thank you!


Solution

  • Since release 3.12 a predicate filter would be the correct approach, avoiding Lua completely for better performance and scalability.

    Take a look at the PredExp class of the Java client and its examples for building complex filters. Predicate filtering also currently exists for the C, C# and Go clients.