Passing parameter to Cassandra CQL query using DataStax client

旧时模样 提交于 2019-11-29 01:50:12

Here is a code example of inserting data about an image using a prepared statements.

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"));

There have been two recent blog posts on planet cassandra with a demo of what the driver can do, they contain code examples so check them out:

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

You need to create a prepared statement. Then you need to bind that statement with the ID value you got from the user. Then you can execute the bound statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!