2013-07-02 3 views
12

私は、cassandraに接続するためのクライアントとしてdatastaxを使用しています。私は、Javaを介してcassandra cluster/keyspace/columnファミリに正常に接続しました。私は努力しています。私にとっては、今、私は、ユーザーからのidパラメータを取ると)(session.executeにそれを渡したいDataStaxクライアントを使用してCassandra CQLクエリにパラメータを渡す

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

のような単純なクエリのために働いています。声明。 どうすればいいですか?

答えて

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

ドライバーが何ができるかのデモと惑星のカサンドラ上の2件の最近のブログ記事がありました、彼らはコード例が含まれているので、それらをチェックアウト:

  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

ありがとう!ありがとう! –