Search code examples
javacassandradatastax

cannot insert list of maps to Apache Cassandra


This is my schema

CREATE TABLE app_category_agg (
    category text,
    app_count int,
    sp_count int,
    subscriber_count int,
    window_revenue bigint,
    top_apps frozen <list<map<text,int>>>,
    PRIMARY KEY (category)
);

insert query generated by java is in this form

INSERT INTO analytics_info.app_category_agg (category,app_count,sp_count,subscriber_count, window_revenue,top_apps) VALUES ('Entertainment',3,2,65,1620,[{APP_992984515=30}, {APP_991415478=23}, {APP_999095235=12}]);

issue is with '=' sign in the map objects in the list of maps. How can I insert it correctly? in java.


Solution

  • map value should be specified as {key1:val1, key2:val2} (see docs), in your case it should be a

    [{'APP_992984515':30}, {'APP_991415478':23}, {'APP_999095235':12}]
    

    but really, you shouldn't generate literal query yourself, as it could be a complex task - you need to handle quotes correctly, etc.

    More appropriate will be to use prepared statements, and bind values - in this case, all syntax will be handled by driver.

    Another possibility is to use Object Mapper (doc for driver 3.x, driver 4.x) - in this case, you may work with rows in the table using the Java objects, all conversion will be handled by driver as well.