使用 DataStax 客户端将参数传递给 Cassandra CQL 查询 [英] Passing parameter to Cassandra CQL query using DataStax client
问题描述
我使用 datastax 作为连接到 cassandra 的客户端.我已经通过 Java 成功连接到 cassandra 集群/键空间/列族.我正在尝试,通过 java 对 cassandra 列族发出一些查询.对我来说,它适用于像
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屋!