2017-01-16 58 views
1

我是Cassandra的新手,我正在尝试创建一个用户定义的聚合函数,但在创建函数过程中遇到困难。Java源代码编译失败 - 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 =“无法编译函数'visitor.countssessions'来自Java源:org.apache.cassandra.exceptions.InvalidReq uestException:Java源代码编译失败: 第2行:dataMap无法解析 7号线:数据图无法解决 8号线:数据图不能被解析为一个变量 “

我无法理解有什么错送我的函数的代码。请帮忙。

另外,有人可以建议我任何链接/网站(除了datastax)使用我可以正确理解UDF和UDA。

感谢和问候,

Vibhav

PS - 如果有人选择反对票的问题,请做提意见的原因。

回答

1

这不起作用,因为dataMap正在初始声明中转换为小写。你可以这样做:

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

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

,使这项工作。

+0

谢谢@mikea .. –