使用DataStax客户端将参数传递到Cassandra CQL查询 [英] Passing parameter to Cassandra CQL query using DataStax client
问题描述
我使用datastax作为连接到cassandra的客户端。我已经成功地通过Java连接到cassandra cluster / keyspace /列族。我试着,发射一些查询cassandra列家庭thriugh java。对我来说,它是为简单的查询工作
I am using datastax as a client for connecting to cassandra. I have successfully connected to cassandra cluster/keyspace/column families through Java. I am trying, firing some queries on cassandra column family thriugh java. For me it is working for simple queries like
ResultSet results = session.execute("select * from demodb.customer where id = 1");
现在我想从用户的id参数传递 session.execute ; 语句。
我应该怎么办呢?
Now I want to take id parameter from user and pass it to session.execute(); statement. How should I go about it?
推荐答案
这里是一个代码示例,语句。
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"));
地球cassandra上最近发布了两个博客文章,演示驱动程序可以做什么包含代码示例,请查看:
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:
这篇关于使用DataStax客户端将参数传递到Cassandra CQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!