Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

java - Passing parameter to Cassandra CQL query using DataStax client

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

Now I want to take id parameter from user and pass it to session.execute(); statement. How should I go about it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...