8 Aralık 2020 Salı

Cassandra API

Giriş
Şu satırı dahil ederiz
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.BoundStatement;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
CqlSession Sınıfı
Örnek
Şöyle yaparız
CqlSession session = CqlSession.builder()
  .addContactPoint(new InetSocketAddress("127.0.0.1",90042))
  .withKeyspace("sample").build();

PreparedStatement selectStatement = session.prepare("select * from lock_requests where
resource_id=?"
);
InsertStatement insertStatement = session.prepare("insert into
lock_requests(resource_id, lock_status,created_on) values(?,?,?) if not exists"
);
DeleteStatement deleteStatement = session.prepare("delete from lock_requests where
resource_id=? if exists"
);
  
String key = "...";

boolean insertLock(){
  BoundStatement boundStatement = insertStatement.bind()
    .setString(0,key);
.setString(1,"LOCKED")
.setInstant(2,Instant.now());
  ResultSet resultSet = session.execute(boundStatement);
  return resultSet.wasApplied(); //this is equivalent to row.getBool("applied")  

boolean isLocked(){
  BoundStatement boundStatement = selectStatement.bind().setString(0,key)
  ResultSet resultSet = session.execute(boundStatement);
  Row row = resultSet.one();
  return row != null ? "LOCKED".equals(row.getString("lock_status")) : false;
}

void release(){
  try{
    BoundStatement boundStatement = deleteStatement.bind().setString(0,key);
session.execute(boundStatement);
  } catch(Exception e){
    ...
  }
}  

Hiç yorum yok:

Yorum Gönder