【问题标题】:Passing parameter to Cassandra CQL query using DataStax client使用 DataStax 客户端将参数传递给 Cassandra CQL 查询
【发布时间】:2013-06-29 10:33:52
【问题描述】:

我使用 datastax 作为连接到 cassandra 的客户端。我已经通过 Java 成功连接到 cassandra 集群/键空间/列族。我正在尝试通过 java 对 cassandra 列族进行一些查询。对我来说,它适用于简单的查询,例如

ResultSet results = session.execute("select * from demodb.customer where id = 1");

现在我想从用户那里获取 id 参数并将其传递给 session.execute(); 语句。 我该怎么办?

【问题讨论】:

    标签: java cassandra cql datastax-java-driver


    【解决方案1】:

    这是一个使用准备好的语句插入图像数据的代码示例。

    PreparedStatement statement = getSession().prepare(
                                   "INSERT INTO pixelstore.image " +
                                   "(image_name, " +
                                   " upload_time, " + 
                                   " upload_by, " + 
                                   " file_type, " + 
                                   " file_size" +
                                   ") VALUES (?, ?, ?, ?, ?);"); 
    
    // create the bound statement and initialise it with your prepared statement
    BoundStatement boundStatement = new BoundStatement(statement);
    
    session.execute( // this is where the query is executed
      boundStatement.bind( // here you are binding the 'boundStatement'
        "background", TimeUtil.getTimeUUID(),  "lyubent", "png", "130527"));
    

    最近在 cassandra 星球上有两篇博客文章,其中包含驱动程序可以做什么的演示,它们包含代码示例,因此请查看:

    1. Materialized View with Cassandra and DataStax Java Driver
    2. Small Java Application using DataStax Java Driver and Cassandra 1.2 working

    【讨论】:

      【解决方案2】:

      您需要创建一个准备好的语句。然后,您需要将该语句与您从用户那里获得的 ID 值绑定。然后就可以执行绑定语句了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-16
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2019-09-03
        • 2018-07-04
        相关资源
        最近更新 更多