2017-01-16 47 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 .. –