cassandra - How will i know that record was duplicate or it was inserted successfully? -


here cql table:

create table user_login (     username varchar primary key,     userid uuid,     fullname varchar,     password text,     blocked boolean ); 

i have datastax java driver code

preparedstatement preparestmt= instances.getcqlsession().prepare("insert "+ appconstants.keyspace+".user_info(userid, username, fullname, bizzcateg, usertype, blocked) values(?, ?, ?, ?, ?, ?);");  batch.add(preparestmt.bind(userid, userdata.getemail(), userdata.getname(), userdata.getbizzcategory(), userdata.getusertype(), false));   preparedstatement pstmtuserlogin = instances.getcqlsession().prepare("insert "+ appconstants.keyspace+".user_login(username, userid, fullname, password, blocked) values(?, ?, ?, ?, ?) if not exist");  batch.add(pstmtuserlogin.bind(userdata.getemail(), userid, userdata.getname(), passwordencoder.encode(userdata.getpwd()), false));             instances.getcqlsession().executeasync(batch); 

here problem if remove if not exist work fine if put not insert records in table nor throw error.

so how know inserting duplicate username ?

i using cassandra 2.0.1

use insert... if not exists, can use resultset#wasapplied() check outcome:

resultset rs = session.execute("insert user (name) values ('foo') if not exists"); system.out.println(rs.wasapplied()); 

notes:

  • this cql query lightweight transaction, carries performance implications. see this article more information.
  • your example has 1 statement, don't need batch

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -