java 使用 DataStax 客户端将参数传递给 Cassandra CQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17419142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameter to Cassandra CQL query using DataStax client
提问by Saurabh Deshpande
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
我使用 datastax 作为连接到 cassandra 的客户端。我已经通过 Java 成功连接到 cassandra 集群/键空间/列族。我正在尝试,通过 java 对 cassandra 列族发出一些查询。对我来说,它适用于简单的查询,如
ResultSet results = session.execute("select * from demodb.customer where id = 1");
Now I want to take id parameter from user and pass it to session.execute();statement. How should I go about it?
现在我想从用户那里获取 id 参数并将其传递给session.execute(); 陈述。我该怎么办?
回答by Lyuben Todorov
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:
最近在 cassandra 星球上有两篇博客文章展示了驱动程序可以做什么,它们包含代码示例,请查看:
回答by Raedwald
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.
您需要创建一个准备好的语句。然后您需要将该语句与您从用户那里获得的 ID 值绑定。然后就可以执行绑定语句了。