user14761354
user14761354

Reputation:

How to check for the cassandra data base table have data or not in java

I have code for creting a table. i need to call insert only when there is no data available.

        public void createGeoConfigDB(){
                CqlSession session = null;
                try {
                        session = CassandraConnector.getInstance().connect();
                        session.execute(table_query);
                    } catch(Exception e){
                        log.error("Exception in creating db:: "+e);
                } finally {
                        CassandraConnector.getInstance().closeSession(session);
                }
//i need to check condition whether database table have data or not
                insertConfig();
        }

Upvotes: 0

Views: 113

Answers (2)

Erick Ramirez
Erick Ramirez

Reputation: 16343

In Cassandra, there is a concept of conditional writes called "lightweight transactions".

You could check for the existence or non-existence of data with the following CQL statement:

INSERT INTO table_name (...) VALUES (...) IF EXISTS

The negative IF NOT EXISTS also works. Cheers!

Upvotes: 1

mammad
mammad

Reputation: 207

You can use lightweight transactions for conditional writes in cassandra.

cqlsh> INSERT INTO cycling.cyclist_name (id, lastname, firstname)
  VALUES (4647f6d3-7bd2-4085-8d6c-1229351b5498, 'KNETEMANN', 'Roxxane')
  IF NOT EXISTS;

To see further information visit https://docs.datastax.com/en/cql-oss/3.3/cql/cql_using/useInsertLWT.html

Upvotes: 1

Related Questions