From user-return-33610-apmail-cassandra-user-archive=cassandra.apache.org@cassandra.apache.org Sun Apr 21 04:38:02 2013 Return-Path: X-Original-To: apmail-cassandra-user-archive@www.apache.org Delivered-To: apmail-cassandra-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A15F31015E for ; Sun, 21 Apr 2013 04:38:02 +0000 (UTC) Received: (qmail 90712 invoked by uid 500); 21 Apr 2013 04:38:00 -0000 Delivered-To: apmail-cassandra-user-archive@cassandra.apache.org Received: (qmail 90469 invoked by uid 500); 21 Apr 2013 04:37:59 -0000 Mailing-List: contact user-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cassandra.apache.org Delivered-To: mailing list user@cassandra.apache.org Received: (qmail 90441 invoked by uid 99); 21 Apr 2013 04:37:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Apr 2013 04:37:58 +0000 X-ASF-Spam-Status: No, hits=2.2 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_NONE X-Spam-Check-By: apache.org Received-SPF: error (athena.apache.org: local policy) Received: from [72.35.23.36] (HELO smtp-out2.electric.net) (72.35.23.36) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Apr 2013 04:37:53 +0000 Received: from [10.86.10.83] (helo=fuseout2c) by bean.electric.net with esmtps (TLSv1:AES256-SHA:256) (Exim 4.77) (envelope-from ) id 1UTm1X-0006jY-Tv for user@cassandra.apache.org; Sat, 20 Apr 2013 21:37:11 -0700 Received: from mailanyone.net by fuseout2c with esmtpsa (TLSv1:CAMELLIA256-SHA:256) (MailAnyone extSMTP dbrosius@baybroadband.net) id 1UTm1W-0002X2-23 for user@cassandra.apache.org; Sat, 20 Apr 2013 21:37:11 -0700 Message-ID: <51736CDC.8030809@mebigfatguy.com> Date: Sun, 21 Apr 2013 00:36:44 -0400 From: Dave Brosius User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: user@cassandra.apache.org Subject: Re: Retrieve data from Cassandra database using Datastax java driver References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------010703010500000109050700" X-Virus-Checked: Checked by ClamAV on apache.org This is a multi-part message in MIME format. --------------010703010500000109050700 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit getColumnDefinitions only returns meta data, to get the data, use the iterator to navigate the rows Iterator it = result.iterator(); while (it.hasNext()) { Row r = it.next(); //do stuff with row } On 04/21/2013 12:02 AM, Techy Teck wrote: > I am working with Datastax java-driver. And I am trying to retrieve > few columns from the database basis on the input that is being passed > to the below method- > > > public Map getAttributes(final String userId, final > Collection attributeNames) { > > String query="SELECT " +attributeNames.toString().substring(1, > attributeNames.toString().length()-1)+ " from profile where id = > '"+userId+ "';"; > CassandraDatastaxConnection.getInstance(); > > ResultSet result = > CassandraDatastaxConnection.getSession().execute(query); > > Map attributes = new ConcurrentHashMap(); > for(Definition def : result.getColumnDefinitions()) { > //not sure how to put the columnName and columnValue that came back > from the database > attributes.put(column name, column value); > } > return attributes; > } > > Now I got the result back from the database in *result* > * > * > Now how to put the colum name and column value that came back from the > database in a map? > > I am not able to understand how to retrieve colum value for a > particular column in datastax java driver? > > Any thoughts will be of great help. --------------010703010500000109050700 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
getColumnDefinitions only returns meta data, to get the data, use the iterator to navigate the rows


Iterator<Row> it = result.iterator();

while (it.hasNext()) {
    Row r = it.next();
    //do stuff with row
}

On 04/21/2013 12:02 AM, Techy Teck wrote:
I am working with Datastax java-driver. And I am trying to retrieve few columns from the database basis on the input that is being passed to the below method-


public Map<String, String> getAttributes(final String userId, final Collection<String> attributeNames) {

String query="SELECT " +attributeNames.toString().substring(1, attributeNames.toString().length()-1)+ " from profile where id = '"+userId+ "';";
CassandraDatastaxConnection.getInstance();

ResultSet result = CassandraDatastaxConnection.getSession().execute(query);

Map<String, String> attributes = new ConcurrentHashMap<String, String>();
for(Definition def : result.getColumnDefinitions()) {
//not sure how to put the columnName and columnValue that came back from the database
attributes.put(column name, column value);
}
return attributes;
}

Now I got the result back from the database in result

Now how to put the colum name and column value that came back from the database in a map?

I am not able to understand how to retrieve colum value for a particular column in datastax java driver?

Any thoughts will be of great help.

--------------010703010500000109050700--