使用Cassandra使用Cassandra操作编写的语句Spring Boot

使用Cassandra使用Cassandra操作编写的语句Spring Boot

问题描述:

我需要使用Cassandra操作接口和会话的Prepared Statement /而不是Query Builder 任何示例或最近的文档。对于卡桑德拉用java使用Cassandra使用Cassandra操作编写的语句Spring Boot

+0

你'春数据cassandra'? –

+0

yes Ajit使用Spring数据Cassandra –

一个例子见this检查如何同时使用Java datastax驱动程序使用准备statment。

但是,我会建议将所有准备好的状态存储在缓存中(例如映射),而应用程序通过创建绑定状态来重新初始化并重用它。 例如:

//Here Key is query string 
private static final Map<String, PreparedStatement> psMap = new ConcurrentHashMap<String, PreparedStatement>(); 

//Will be invoked @ initialization 
public void init(Session session) { 
     this.session = session; 
     for (QuerySetEnum cql : QuerySetEnum.values()) { 

      psMap.put(cql.getStatement(), session.prepare(cql.getStatement())); 
     } 


     //In Dao Impl class 
     //Get bounded statment + execute by passing the value 
     @Override 
    public void decreaseStats(long size, long count, String mapname, 
      int bucketId) { 
     BoundStatement boundStatement = getBoundStatement(QuerySetEnum.DECREASE_STATS); 
     metaTemplate.execute(boundStatement.bind(size, count, mapname, 
       bucketId)); 

    } 
//Below is the implementation how to get BoundStatement out to prepared statment cache 
    private BoundStatement getBoundStatement(QuerySetEnum query) { 
     PreparedStatement preparedStatement = queryPool 
       .getPreparedStatement(query); 
     BoundStatement boundStatement = new BoundStatement(preparedStatement); 
     return boundStatement; 
    } 

对于弹簧数据卡桑德拉1.x版中,的getSession()的org.springframework.cassandra.core.CqlOperations方法可以让你直接访问会议。但是,类似的API,因为2.0

这里弃用是https://github.com/opencredo/spring-data-cassandra-example/

@Autowired 
private CqlOperations cqlTemplate;//or inherited interface, like CassandraOperations 

private void insertEventUsingPreparedStatement() { 
    PreparedStatement preparedStatement = cqlTemplate.getSession().prepare("insert into event (id, type, bucket, tags) values (?, ?, ?, ?)"); 
    Statement insertStatement = preparedStatement.bind(UUIDs.timeBased(), "type2", TIME_BUCKET, ImmutableSet.of("tag1", "tag2")); 
    cqlTemplate.execute(insertStatement); 
} 
+0

需要使用CassandraOperations或CqlTemplate接口来查询具有特定行对象的where子句的表上面的cqlTemplate.execute(insertStatement);返回void –

使用spring-data-cassandra,这将为你做所有的魔法。
应用样本http://valchkou.com/spring-boot-cassandra.html#simple

@Repository 
interface ISensorMeasureRepository extends CassandraRepository<SensorMeasureEntity> { 

    @Query('select * from sensor_measures_simple where sensor_id=?0 and measure_time>=?1 and measure_time<=?2') 
    List<SensorMeasureEntity> getBySensorAndDateRange(int sensorId, Date start, Date end) 

    @Query('select * from sensor_measures_simple where sensor_id=?0 ALLOW FILTERING') 
    Stream<SensorMeasureEntity> getAllBySensor(int sensorId) 
} 
+0

嘿,谢谢... @Query注释使用准备好的语句或原始的cql查询吗? –