2013-07-02 109 views
12

我使用datastax作爲連接到cassandra的客戶端。我已通過Java成功連接到cassandra集羣/ keyspace/column家族。我正在嘗試,對cassandra列家庭thriugh java發射一些疑問。對我來說,工作簡單的查詢,如使用DataStax客戶端將參數傳遞給Cassandra CQL查詢

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

現在我想從用戶採取ID參數,並將其傳遞給session.execute();聲明。 我該怎麼處理?

回答

18

下面是使用預處理語句插入圖像數據的代碼示例。

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

已經有關於地球卡桑德拉最近兩次的博客文章有什麼樣的驅動程序可以做一個演示,它們包含的代碼示例,以便檢查出來:

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

非常感謝你! –

0

您需要創建一個準備好的語句。然後,您需要將該語句與您從用戶那裏獲得的ID值綁定。然後你可以執行綁定語句。

+0

好的....謝謝! –