You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Techy Teck <co...@gmail.com> on 2013/04/21 06:02:47 UTC
Retrieve data from Cassandra database using Datastax java driver
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.
Re: Retrieve data from Cassandra database using Datastax java driver
Posted by Abhijit Chanda <ab...@gmail.com>.
You have the collection attributeNames, just iterate it
Iterator<Row> it = result.iterator();
while(it.hasNext()){
Row row = it.next();
for(String column : attributeNames) {
//not sure how to put the columnName and columnValue that
came back from the database
attributes.put(column,row.getString(column));
}
}
Cheers
On Sun, Apr 21, 2013 at 10:24 AM, Techy Teck <co...@gmail.com>wrote:
> Thanks Dave for the suggestion. I have all my columns name in this
> collection-
>
> *final Collection<String> attributeNames*
> *
> *
> And all my results back in this resultset-
>
> *ResultSet result =
> CassandraDatastaxConnection.getSession().execute(query);*
> *
> *
> Now I need to store the column name and its corresponding value in the
> Below Map-
>
> *Map<String, String> attributes = new
> ConcurrentHashMap<String, String>();*
> *
> *
> What's the best way to do that in this case?
>
> Thanks for the help.
>
>
>
>
>
>
> On Sat, Apr 20, 2013 at 9:36 PM, Dave Brosius <db...@mebigfatguy.com>wrote:
>
>> 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.
>>
>>
>>
>
--
-Abhijit
Re: Retrieve data from Cassandra database using Datastax java driver
Posted by Techy Teck <co...@gmail.com>.
Thanks Dave for the suggestion. I have all my columns name in this
collection-
*final Collection<String> attributeNames*
*
*
And all my results back in this resultset-
*ResultSet result =
CassandraDatastaxConnection.getSession().execute(query);*
*
*
Now I need to store the column name and its corresponding value in the
Below Map-
*Map<String, String> attributes = new
ConcurrentHashMap<String, String>();*
*
*
What's the best way to do that in this case?
Thanks for the help.
On Sat, Apr 20, 2013 at 9:36 PM, Dave Brosius <db...@mebigfatguy.com>wrote:
> 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.
>
>
>
Re: Retrieve data from Cassandra database using Datastax java driver
Posted by Dave Brosius <db...@mebigfatguy.com>.
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.