Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

9/6/2018

Reading time:N/A min

Inserting JSON data into a table

by John Doe

Inserting JSON data with the INSERT command for testing queries.In a production database, inserting columns and column values programmatically is more practical than using cqlsh, but often, testing queries using this SQL-like shell is very convenient. For JSON data all values will be inserted as a string if they are not a number, but will be stored using the column data type. For example, the id below is inserted as a string, but is stored as a UUID. For more information, see What's New in Cassandra 2.2: JSON Support. To insert JSON data, add JSON to the INSERT command.. Note the absence of the keyword VALUES and the list of columns that is present in other INSERT commands. cqlsh> INSERT INTO cycling.cyclist_category JSON '{ "category" : "GC", "points" : 780, "id" : "829aa84a-4bba-411f-a4fb-38167a987cda", "lastname" : "SUTHERLAND" }'; A null value will be entered if a defined column like lastname, is not inserted into a table using JSON format. cqlsh> INSERT INTO cycling.cyclist_category JSON '{ "category" : "Sprint", "points" : 700, "id" : "829aa84a-4bba-411f-a4fb-38167a987cda"}';

Illustration Image

Inserting JSON data with the INSERT command for testing queries.

In a production database, inserting columns and column values programmatically is more practical than using cqlsh, but often, testing queries using this SQL-like shell is very convenient. For JSON data all values will be inserted as a string if they are not a number, but will be stored using the column data type. For example, the id below is inserted as a string, but is stored as a UUID. For more information, see What's New in Cassandra 2.2: JSON Support.

  • To insert JSON data, add JSON to the INSERT command.. Note the absence of the keyword VALUES and the list of columns that is present in other INSERT commands.
    cqlsh> INSERT INTO cycling.cyclist_category JSON '{
      "category" : "GC", 
      "points" : 780, 
      "id" : "829aa84a-4bba-411f-a4fb-38167a987cda",
      "lastname" : "SUTHERLAND" }';
      
  • A null value will be entered if a defined column like lastname, is not inserted into a table using JSON format.
    cqlsh> INSERT INTO cycling.cyclist_category JSON '{
      "category" : "Sprint", 
      "points" : 700, 
      "id" : "829aa84a-4bba-411f-a4fb-38167a987cda"
    }';
    image

Related Articles

migration
proxy
cassandra

GitHub - datastax/cql-proxy: A client-side CQL proxy/sidecar.

datastax

11/1/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

cassandra