【问题标题】:Using Cassandra Prepared Statement using Cassandra Operations Spring Boot使用 Cassandra 操作 Spring Boot 使用 Cassandra Prepared Statement
【发布时间】:2018-04-16 20:35:34
【问题描述】:

我需要使用 Cassandra 操作界面和会话的 Prepared Statement / 而不是 Query Builder 任何示例或最近的文档。对于使用 java 的 Cassandra

【问题讨论】:

  • 你是spring data cassandra 吗?
  • 是的 Ajit 使用 Spring Data Cassandra

标签: java spring spring-boot cassandra


【解决方案1】:

查看this,了解如何在使用 java datastax 驱动程序时使用准备好的语句。

但是,我建议将所有preparedstatments 存储在缓存中(例如地图),同时应用程序通过创建boundstatment 来初始化和重用它们。 例如:

//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;
    }

【讨论】:

    【解决方案2】:

    使用 spring-data-cassandra,这将为您带来一切。
    应用示例https://github.com/valchkou-app/spring-boot-cassandra-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)
    }
    

    【讨论】:

    • 嘿,谢谢...@Query 注释是使用准备好的语句还是原始的 cql 查询?
    【解决方案3】:

    对于spring-data-cassandra v1.x,org.springframework.cassandra.core.CqlOperationsgetSession()方法可以让你直接访问Session。但是,自 v2.0 起,类似的 API 已被弃用

    这是来自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); 
    } 
    

    【讨论】:

    • 需要使用 CassandraOperations 或 CqlTemplate 接口查询带有 where 子句的表,该子句给出特定行的对象上述 cqlTemplate.execute(insertStatement);返回无效
    猜你喜欢
    • 2019-02-07
    • 2016-05-21
    • 2015-10-04
    • 2015-11-16
    • 1970-01-01
    • 2016-06-28
    • 2017-05-13
    • 2020-09-09
    • 2016-12-07
    相关资源
    最近更新 更多