Search code examples
cassandracql

Cassandra insert data into table column with data type set<text>


We're trying to setup an INSERT statement on a table with a set<text> column data type in Cassandra, but have come up with no luck. Here's an example:

INSERT INTO test_table
    (my_items)
VALUES
    ([{"test1":"test val 1","test2":"test val 2"}])

The result is always something like:

no viable alternative at input for [test val ]2

Solution

  • Enclosing values in curly brackets and separate by comma

    Example :

    Let's we have the schema :

    CREATE TABLE users (
      user_id text PRIMARY KEY,
      first_name text,
      last_name text,
      emails set<text>
    );
    

    Insert :

    INSERT INTO users (user_id, first_name, last_name, emails)
        VALUES('frodo', 'Frodo', 'Baggins', {'f@baggins.com', 'baggins@gmail.com'});
    

    Update :

    UPDATE users SET emails = emails + {'fb@friendsofmordor.org'} 
         WHERE user_id = 'frodo';
    

    More Using the set type


    Edited

    If you want to insert value "test1":"test val 1" and "test2":"test val 2" into set then enclose each value with single quote

    Example :

    INSERT INTO users (user_id, first_name, last_name, emails)
        VALUES('2011331035', 'Md Ashraful', 'Islam', {'"test1":"test val 1"', '"test2":"test val 2"'});