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
Post a Comment