Created
January 30, 2019 19:45
-
-
Save vvgsrk/6326cce195450d3ed6985bf35390a9e3 to your computer and use it in GitHub Desktop.
Create UDF and UDA in Cassandra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cqlsh:test_ks> | |
CREATE TABLE emp_dept_info(emp_id int, | |
emp_name text, | |
dept_id int, | |
created_time timeuuid, | |
PRIMARY KEY((emp_id), created_time)); | |
# Insert data in emp_dept_info Table | |
INSERT INTO emp_dept_info(emp_id, emp_name, dept_id, created_time) | |
VALUES(1, 'Venkat', 30, NOW()); | |
INSERT INTO emp_dept_info(emp_id, emp_name, dept_id, created_time) | |
VALUES(2, 'Gowri', 30, NOW()); | |
INSERT INTO emp_dept_info(emp_id, emp_name, dept_id, created_time) | |
VALUES(2, 'Sai', 30, NOW()); | |
INSERT INTO emp_dept_info(emp_id, emp_name, dept_id, created_time) | |
VALUES(2, 'Rakesh', 40, NOW()); | |
INSERT INTO emp_dept_info(emp_id, emp_name, dept_id, created_time) | |
VALUES(2, 'Kumar', 40, NOW()); | |
INSERT INTO emp_dept_info(emp_id, emp_name, dept_id, created_time) | |
VALUES(2, 'Varanasi', 50, NOW()); | |
# Created the user-defined function | |
CREATE FUNCTION state_count_per_group( state map<int, int>, type int ) | |
CALLED ON NULL INPUT | |
RETURNS map<int, int> | |
LANGUAGE java AS ' | |
Integer count = (Integer) state.get(type); | |
if (count == null) count = 1; | |
else count++; state.put(type, count); | |
return state; ' ; | |
# Created the below aggregate function using above function | |
CREATE OR REPLACE AGGREGATE agg_count_per_group(int) | |
SFUNC state_count_per_group | |
STYPE map<int, int> | |
INITCOND {}; | |
cqlsh> SELECT agg_count_per_group(dept_id) FROM emp_dept_info; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment