Aerospike:lua udf总是返回一个空结果,即使udf返回流没有任何过滤等

时间:2016-03-30 15:27:09

标签: lua udf aerospike

无法理解为什么aggregateQuery总是返回一个空结果。试图在aql中测试,同样的问题:集合中的0行。

索引就在那里。

aql> show indexes
+---------------+-------------+-----------+------------+-------+------------------------------+-------------+------------+-----------+
| ns            | bin         | indextype | set        | state | indexname                    | path        | sync_state | type      |
+---------------+-------------+-----------+------------+-------+------------------------------+-------------+------------+-----------+
| "test"        | "name"      | "NONE"    | "profiles" | "RW"  | "inx_test_name"              | "name"      | "synced"   | "STRING"  |
| "test"        | "age"       | "NONE"    | "profiles" | "RW"  | "inx_test_age"               | "age"       | "synced"   | "NUMERIC" |


aql> select * from test.profiles
+---------+-----+
| name    | age |
+---------+-----+
| "Sally" | 19  |
| 20      |     |
| 22      |     |
| 28      |     |
| "Ann"   | 22  |
| "Bob"   | 22  |
| "Tammy" | 22  |
| "Ricky" | 20  |
| 22      |     |
| 19      |     |
+---------+-----+
10 rows in set (0.026 secs)


aql>  AGGREGATE mystream.avg_age() ON test.profiles WHERE age BETWEEN 20 and 29
0 rows in set (0.004 secs)

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试示例here 关于udf脚本有两个问题。我粘贴了lua脚本的代码:

function avg_age(stream)

  local function female(rec)
    return rec.gender == "F"
  end

  local function name_age(rec)
    return map{ name=rec.name, age=rec.age }
  end

  local function eldest(p1, p2)
    if p1.age > p2.age then 
      return p1
    else
      return p2
    end
  end

  return stream : filter(female) : map(name_age) : reduce(eldest)
end

首先,没有名为'性别'在你的集合中,所以你在aggregateQuery之后有0行。
其次,这个脚本并没有完全按照功能名称' avg_age'意思是,它只返回姓名和年龄最长的记录。

我粘贴我的代码,它只是替换reduce func,并提醒地图并过滤功能以满足需求。您可以跳过过滤过程。

function avg_age(stream)
  count = 0
  sum = 0

  local function female(rec)
    return true
  end

  local function name_age(rec)
    return rec.age
  end

  local function avg(p1, p2)
    count = count + 1
    sum = sum + p2
    return sum / count
  end

  return stream : filter(female) : map(name_age) : reduce(avg)
end

输出如下:

AGGREGATE mystream.avg_age() ON test.avgage WHERE age BETWEEN 20 and 29
+---------+
| avg_age |
+---------+
| 22      |
+---------+
1 row in set (0.001 secs)