Skip to content

Instantly share code, notes, and snippets.

@vvgsrk
Created January 30, 2019 19:45
Show Gist options
  • Save vvgsrk/6326cce195450d3ed6985bf35390a9e3 to your computer and use it in GitHub Desktop.
Save vvgsrk/6326cce195450d3ed6985bf35390a9e3 to your computer and use it in GitHub Desktop.
Create UDF and UDA in Cassandra
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