You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Tamar Fraenkel <ta...@tok-media.com> on 2012/01/23 09:24:19 UTC
get all columns for a row
Hi!
I am getting started with Cassandra and Hector and have a question.
If I have CF with many columns, only a small portion of them having values for
any given key. I want to read all columns for a given key, but I don't know
which columns were populated.
I managed to do that with the code below (assumes that column names are
incremental numbers).
Is there any better way?
Thanks,
Tamar
private static void readAllColunsForRowKey(String key) {
MultigetSliceQuery<String, Long, String> multigetSlicesQuery =
HFactory.createMultigetSliceQuery(tutorialKeyspace, StringSerializer.get(),
LongSerializer.get(),
StringSerializer.get());
multigetSlicesQuery.setColumnFamily(CF);
multigetSlicesQuery.setKeys(key);
multigetSlicesQuery.setRange(null, null, false, COL_COUNT);
QueryResult<Rows<String, Long, String>> results =
multigetSlicesQuery.execute();
Long lastColName;
while ((lastColName = printColsAndGetLastColName(results, key)) != null) {
multigetSlicesQuery.setRange(lastColName + 1, null, false, COL_COUNT);
results = multigetSlicesQuery.execute();
}
}
private static Long printColsAndGetLastColName(QueryResult<Rows<String, Long,
String>> results, String key) {
Rows<String, Long, String> rows = results.get();
if (rows.getCount() != 1) {
System.out.println("No such key");
return null;
}
Row<String, Long, String> row = rows.getByKey(key);
ColumnSlice<Long, String> columnSlice = row.getColumnSlice();
List<HColumn<Long, String>> columns = columnSlice.getColumns();
for (HColumn<Long, String> column : columns) {
System.out.println("Column Name: " + column.getName()
+ ", Column Val: " + column.getValue());
}
if (columns.size() == COL_COUNT) {
return columns.get(COL_COUNT - 1).getName();
}
return null;
}
Re: get all columns for a row
Posted by Tamar Fraenkel <ta...@tok-media.com>.
Thanks.
Tamar
On January 23, 2012 at 11:24 AM aaron morton <aa...@thelastpickle.com> wrote:
> The columns are stored at the intersection of the row and the CF. So if you
> read all the columns for a row in a CF you are only getting those ones.
>
> Your hector code (using the range) looks correct to me.
>
> Have fun.
> Aaron
>
>
>
>
>
>
>
> -----------------
> Aaron Morton
> Freelance Developer
> @aaronmorton
> http://www.thelastpickle.com
>
>
> On 23/01/2012, at 9:24 PM, Tamar Fraenkel wrote:
>
> >
> >
> > Hi!
> >
> > I am getting started with Cassandra and Hector and have a question.
> > If I have CF with many columns, only a small portion of them having values
> > for any given key. I want to read all columns for a given key, but I don't
> > know which columns were populated.
> > I managed to do that with the code below (assumes that column names are
> > incremental numbers).
> > Is there any better way?
> > Thanks,
> > Tamar
> > private static void readAllColunsForRowKey(String key) {
> > MultigetSliceQuery<String, Long, String> multigetSlicesQuery =
> > HFactory.createMultigetSliceQuery(tutorialKeyspace,
> > StringSerializer.get(), LongSerializer.get(),
> > StringSerializer.get());
> > multigetSlicesQuery.setColumnFamily(CF);
> > multigetSlicesQuery.setKeys(key);
> > multigetSlicesQuery.setRange(null, null, false, COL_COUNT);
> > QueryResult<Rows<String, Long, String>> results =
> > multigetSlicesQuery.execute();
> > Long lastColName;
> > while ((lastColName = printColsAndGetLastColName(results, key)) != null) {
> > multigetSlicesQuery.setRange(lastColName + 1, null, false, COL_COUNT);
> > results = multigetSlicesQuery.execute();
> > }
> > }
> >
> > private static Long printColsAndGetLastColName(QueryResult<Rows<String,
> > Long, String>> results, String key) {
> > Rows<String, Long, String> rows = results.get();
> > if (rows.getCount() != 1) {
> > System.out.println("No such key");
> > return null;
> > }
> > Row<String, Long, String> row = rows.getByKey(key);
> > ColumnSlice<Long, String> columnSlice = row.getColumnSlice();
> > List<HColumn<Long, String>> columns = columnSlice.getColumns();
> > for (HColumn<Long, String> column : columns) {
> > System.out.println("Column Name: " + column.getName()
> > + ", Column Val: " + column.getValue());
> > }
> > if (columns.size() == COL_COUNT) {
> > return columns.get(COL_COUNT - 1).getName();
> > }
> > return null;
> > }
> >
Re: get all columns for a row
Posted by aaron morton <aa...@thelastpickle.com>.
The columns are stored at the intersection of the row and the CF. So if you read all the columns for a row in a CF you are only getting those ones.
Your hector code (using the range) looks correct to me.
Have fun.
Aaron
-----------------
Aaron Morton
Freelance Developer
@aaronmorton
http://www.thelastpickle.com
On 23/01/2012, at 9:24 PM, Tamar Fraenkel wrote:
> Hi!
> I am getting started with Cassandra and Hector and have a question.
> If I have CF with many columns, only a small portion of them having values for any given key. I want to read all columns for a given key, but I don't know which columns were populated.
> I managed to do that with the code below (assumes that column names are incremental numbers).
> Is there any better way?
> Thanks,
> Tamar
>
> private static void readAllColunsForRowKey(String key) {
> MultigetSliceQuery<String, Long, String> multigetSlicesQuery =
> HFactory.createMultigetSliceQuery(tutorialKeyspace, StringSerializer.get(), LongSerializer.get(),
>
> StringSerializer.get());
> multigetSlicesQuery.setColumnFamily(CF);
> multigetSlicesQuery.setKeys(key);
> multigetSlicesQuery.setRange(null, null, false, COL_COUNT);
> QueryResult<Rows<String, Long, String>> results = multigetSlicesQuery.execute();
> Long lastColName;
> while ((lastColName = printColsAndGetLastColName(results, key)) != null) {
> multigetSlicesQuery.setRange(lastColName + 1, null, false, COL_COUNT);
> results = multigetSlicesQuery.execute();
> }
> }
>
> private static Long printColsAndGetLastColName(QueryResult<Rows<String, Long, String>> results, String key) {
> Rows<String, Long, String> rows = results.get();
> if (rows.getCount() != 1) {
> System.out.println("No such key");
> return null;
> }
> Row<String, Long, String> row = rows.getByKey(key);
> ColumnSlice<Long, String> columnSlice = row.getColumnSlice();
> List<HColumn<Long, String>> columns = columnSlice.getColumns();
> for (HColumn<Long, String> column : columns) {
> System.out.println("Column Name: " + column.getName()
> + ", Column Val: " + column.getValue());
> }
> if (columns.size() == COL_COUNT) {
> return columns.get(COL_COUNT - 1).getName();
> }
> return null;
> }
>