我是Cassandra的新手,我正在尝试创建一个用户定义的聚合,但坚持创建函数 .
表中的数据是 -
count| host
-----+-----------
102 | test_host1
100 | test_host2
101 | test_host2
101 | test_host3
104 | test_host3
101 | test_host1
100 | test_host3
103 | test_host3
102 | test_host3
100 | test_host1
我写的函数将计算我的表中有特定主机的行数 . 如果我将test_host1提供给聚合,那么理想的结果将是3 .
找到下面的函数代码 -
CREATE FUNCTION countSessions(dataMap map<text,int>,host text)
RETURNS NULL ON NULL INPUT
RETURNS map<text, int>
LANGUAGE java as
'
Integer countValue = dataMap.get(host);
if(countValue == null) {
countValue = 1;
} else {
countValue++;
}
dataMap.put(host,countValue);
return dataMap;
';
在cqlsh上执行此操作时,我收到以下错误 -
InvalidRequest:code = 2200 [无效查询] message =“无法从Java源代码编译函数'visitor.countsessions':org.apache.cassandra.exceptions.InvalidReq uestException:Java源代码编译失败:第2行:无法解析dataMap 7:无法解析dataMap第8行:dataMap无法解析为变量“
我无法理解我的函数代码发生错误的原因 . 请帮忙 .
另外,有人可以建议我使用哪些链接/站点(除数据集之外),我可以正确理解UDF和UDA .
感谢致敬,
Vibhav
PS - 如果有人选择对该问题进行投票,请在评论中提及原因 .
1 回答
这不起作用,因为
dataMap
在初始声明中被转换为小写 . 你可以这样做:要么
使这项工作 .