---
title: "Apache Cassandra Learning Step by Step (3): Samples ABC"
author: "itstarting"
date: "2012-02-16T16:48:01Z"
tags: []
categories: ["互联网"]
slug: "apache-cassandra-learning-step-by-step-3-samples-abc"
draft: false
lastmod: "2012-02-16T16:48:01Z"
id: 1408195
url: "https://itstarting.iteye.com/blog/1408195"
---

====16 Feb 2012, by Bright Zheng (IT进行时)====

# 4. Samples ABC

We try to learn it step by step to understand the concepts and Java API usages by means of:

1\. Concept Introduction

2\. CLI

3\. Java Sample Code

## 4.1. Get a Single Column by a Key

## 4.1.1. Sample Code

**public** QueryResult&lt;HColumn&lt;String,String&gt;&gt; execute() {       

        ColumnQuery&lt;String, String, String&gt; columnQuery = HFactory.*createStringColumnQuery*(keyspace);

        columnQuery.setColumnFamily("Npanxx");

        columnQuery.setKey("512204");

        columnQuery.setName("city");

        QueryResult&lt;HColumn&lt;String, String&gt;&gt; result = columnQuery.execute();

       

        **return** result;

    }

## 4.1.2.  Sample Code run by Maven

C:\\projects\_learning\\learning-cassandra-tutorial&gt;mvn -e exec:java -Dexec.args="get" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner"

The output is:

\[INFO] --- exec-maven-plugin:1.1.2-Beta1:java (default-cli) @ cassandra-tutorial ---

HColumn(city=Austin)

\[INFO] ------------------------------------------------------------------------

\[INFO] BUILD SUCCESS

## 4.1.3.  CLI

\[default@Tutorial] **get Npanxx\['512204']\['city'];**

=&gt; (column=city, value=Austin, timestamp=1329234388328000)

Elapsed time: 16 msec(s).

## 4.2. Get multiple columns by a Key

## 4.2.1. Sample Code

**public** QueryResult&lt;ColumnSlice&lt;Long,String&gt;&gt; execute() {

        SliceQuery&lt;String, Long, String&gt; sliceQuery =

            HFactory.*createSliceQuery*(keyspace, *stringSerializer*, *longSerializer*, *stringSerializer*);

        sliceQuery.setColumnFamily("StateCity");

        sliceQuery.setKey("TX Austin");

       

        //way 1: set multiple columnNames

        sliceQuery.setColumnNames(202L, 203L, 204L);

       

        //way 2: use setRange

        // change 'reversed' to true to get the columns in reverse order

        //sliceQuery.setRange(202L, 204L, false, 5);

        

        QueryResult&lt;ColumnSlice&lt;Long, String&gt;&gt; result = sliceQuery.execute();

        **return** result;

    }

## 4.2.2. Sample Code run by Maven

C:\\projects\_learning\\learning-cassandra-tutorial&gt;mvn -e exec:java -Dexec.args="get\_slice\_sc" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner"

The output is:

\[INFO] --- exec-maven-plugin:1.1.2-Beta1:java (default-cli) @ cassandra-tutorial ---

ColumnSlice(\[HColumn(202=30.27x097.74), HColumn(203=30.27x097.74), HColumn(204=30.32x097.73)]

\[INFO] ------------------------------------------------------------------------

\[INFO] BUILD SUCCESS

## 4.2.3. CLI(TODO)

TODO: Refering to CLI Syntax, Cassandra can’t get multiple columns at one ‘get’ command?

## 4.3. Get multiple rows by a set of Key

## 4.3.1. Sample Code

**public** QueryResult&lt;Rows&lt;String,String,String&gt;&gt; execute() {

        MultigetSliceQuery&lt;String, String, String&gt; multigetSlicesQuery =

            HFactory.*createMultigetSliceQuery*(keyspace, *stringSerializer*, *stringSerializer*, *stringSerializer*);

        multigetSlicesQuery.setColumnFamily("Npanxx");

        multigetSlicesQuery.setColumnNames("city","state","lat","lng");       

        multigetSlicesQuery.setKeys("512202","512203","512205","512206");

        QueryResult&lt;Rows&lt;String, String, String&gt;&gt; results = multigetSlicesQuery.execute();

        **return** results;

    }

## 4.3.2. Sample Code run by Maven

C:\\projects\_learning\\learning-cassandra-tutorial&gt;mvn -e exec:java -Dexec.args="multiget\_slice" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner"

The output is:

\[INFO] --- exec-maven-plugin:1.2:java (default-cli) @ cassandra-tutorial ---

Rows({

512205=Row(512205,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73), HColumn(state=TX)])),

512206=Row(512206,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73), HColumn(state=TX)])),

512203=Row(512203,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.27), HColumn(lng=097.74), HColumn(state=TX)])),

512202=Row(512202,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.27), HColumn(lng=097.74), HColumn(state=TX)]))})

\[INFO] ------------------------------------------------------------------------

\[INFO] BUILD SUCCESS

## 4.3.3. CLI(TODO)

TODO: N/A?

## 4.4. Get Slices from a Range of Rows by Key

## 4.4.1. Sample Code

GetRangeSlicesForStateCity.java

**public** QueryResult&lt;OrderedRows&lt;String,String,String&gt;&gt; execute() {

        RangeSlicesQuery&lt;String, String, String&gt; rangeSlicesQuery =

            HFactory.*createRangeSlicesQuery*(keyspace, *stringSerializer*, *stringSerializer*, *stringSerializer*);

        rangeSlicesQuery.setColumnFamily("Npanxx");

        rangeSlicesQuery.setColumnNames("city","state","lat","lng");       

        rangeSlicesQuery.setKeys("512202", "512205");

        rangeSlicesQuery.setRowCount(5);

        QueryResult&lt;OrderedRows&lt;String, String, String&gt;&gt; results = rangeSlicesQuery.execute();

        **return** results;

    }

**Important Note**: The result actually is NOT meaningful (expected return might be 512202-512205, 4 rows, but actually not) since the Key is sorted by ***RandomPartitioner*** (which can be configured in /conf/cassandra.yaml, but not recommend to do so).  The result can be referred at “Sample Code run by Maven”.

## 4.4.2. Sample Code run by Maven

C:\\projects\_learning\\learning-cassandra-tutorial&gt;mvn -e exec:java -Dexec.args="get\_range\_slices" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner"

The output is:

\[INFO] --- exec-maven-plugin:1.2:java (default-cli) @ cassandra-tutorial ---

Rows({

512202=Row(512202,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.27), HColumn(lng=097.74), HColumn(state=TX)])),

512206=Row(512206,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73), HColumn(state=TX)])),

512205=Row(512205,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73), HColumn(state=TX)]))

})

\[INFO] ------------------------------------------------------------------------

\[INFO] BUILD SUCCESS

## 4.4.3. CLI(TODO)

TODO: N/A

## 4.5. Get Slices from a Range of Rows by Columns

## 4.5.1. Sample Code

GetSliceForAreaCodeCity.java

**public** QueryResult&lt;ColumnSlice&lt;String,String&gt;&gt; execute() {

        SliceQuery&lt;String, String, String&gt; sliceQuery =

            HFactory.*createSliceQuery*(keyspace, *stringSerializer*, *stringSerializer*, *stringSerializer*);

        sliceQuery.setColumnFamily("AreaCode");

        sliceQuery.setKey("512");

        // change the order argument to 'true' to get the last 2 columns in descending order

        // gets the first 4 columns "between" Austin and Austin\_\_204 according to comparator

        sliceQuery.setRange("Austin", "Austin\_\_204", **false**, 5);

 

        QueryResult&lt;ColumnSlice&lt;String, String&gt;&gt; result = sliceQuery.execute();

 

        **return** result;

    }

## 4.5.2. Sample Code run by Maven

C:\\projects\_learning\\learning-cassandra-tutorial&gt;mvn -e exec:java -Dexec.args="get\_slice\_acc" -Dexec.mainClass="com.datastax.tutorial.TutorialRunner"

The output is:

\[INFO] --- exec-maven-plugin:1.2:java (default-cli) @ cassandra-tutorial ---

ColumnSlice([

HColumn(Austin\_\_202=30.27x097.74),

HColumn(Austin\_\_203=30.27x097.74),

HColumn(Austin\_\_204=30.32x097.73)

])

\[INFO] ------------------------------------------------------------------------

\[INFO] BUILD SUCCESS

## 4.5.3. CLI

N/A

## 4.6. Get Slices from Indexed Columns

## 4.6.1. Sample Code

GetIndexedSlicesForCityState.java

**public** QueryResult&lt;OrderedRows&lt;String, String, String&gt;&gt; execute() {

        IndexedSlicesQuery&lt;String, String, String&gt; indexedSlicesQuery =

            HFactory.*createIndexedSlicesQuery*(keyspace, *stringSerializer*, *stringSerializer*, *stringSerializer*);

        indexedSlicesQuery.setColumnFamily("Npanxx");

        indexedSlicesQuery.setColumnNames("city","lat","lng");

        indexedSlicesQuery.addEqualsExpression("state", "TX");

        indexedSlicesQuery.addEqualsExpression("city", "Austin");

        indexedSlicesQuery.addGteExpression("lat", "30.30");

        QueryResult&lt;OrderedRows&lt;String, String, String&gt;&gt; result = indexedSlicesQuery.execute();

        

        **return** result;

    }

## 4.6.2. Sample Code run by Maven

 

The output is:

\[INFO] --- exec-maven-plugin:1.2:java (default-cli) @ cassandra-tutorial ---

Rows({512204=Row(

512204,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73)])),

512206=Row(512206,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73)])),

512205=Row(512205,ColumnSlice(\[HColumn(city=Austin), HColumn(lat=30.32), HColumn(lng=097.73)]))})

\[INFO] ------------------------------------------------------------------------

\[INFO] BUILD SUCCESS

## 4.6.3. CLI

\[default@Tutorial] get npanxx where state='TX' and city='Austin' and lat&gt;'30.30';

\-------------------

RowKey: 512204

=&gt; (column=city, value=Austin, timestamp=1329299521508000)

=&gt; (column=lat, value=30.32, timestamp=1329299521540000)

=&gt; (column=lng, value=097.73, timestamp=1329299521555000)

=&gt; (column=state, value=TX, timestamp=1329299521524000)

\-------------------

RowKey: 512206

=&gt; (column=city, value=Austin, timestamp=1329299521618000)

=&gt; (column=lat, value=30.32, timestamp=1329299521633000)

=&gt; (column=lng, value=097.73, timestamp=1329299522491000)

=&gt; (column=state, value=TX, timestamp=1329299521618000)

\-------------------

RowKey: 512205

=&gt; (column=city, value=Austin, timestamp=1329299521555000)

=&gt; (column=lat, value=30.32, timestamp=1329299521586000)

=&gt; (column=lng, value=097.73, timestamp=1329299521602000)

=&gt; (column=state, value=TX, timestamp=1329299521571000)

 

3 Rows Returned.

Elapsed time: 16 msec(s).

 

## 4.7. Insertion

## 4.7.1. Sample Code

InsertRowsForColumnFamilies.java

**public** QueryResult&lt;?&gt; execute() {

        Mutator&lt;String&gt; mutator = HFactory.*createMutator*(keyspace, *stringSerializer*);

       

        mutator.addInsertion("CA Burlingame", "StateCity", HFactory.*createColumn*(650L, "37.57x122.34",*longSerializer*,*stringSerializer*));

        mutator.addInsertion("650", "AreaCode", HFactory.*createStringColumn*("Burlingame\_\_650", "37.57x122.34"));

        mutator.addInsertion("650222", "Npanxx", HFactory.*createStringColumn*("lat", "37.57"));

        mutator.addInsertion("650222", "Npanxx", HFactory.*createStringColumn*("lng", "122.34"));

        mutator.addInsertion("650222", "Npanxx", HFactory.*createStringColumn*("city", "Burlingame"));

        mutator.addInsertion("650222", "Npanxx", HFactory.*createStringColumn*("state", "CA"));                

       

        MutationResult mr = mutator.execute();

        **return** **null**;

    }

## 4.7.2. Sample Code run by Maven

Omitted

## 4.7.3. CLI

\[default@Tutorial] set StateCity\['CA Burlingame']\['650']='37.57x122.34';

\[default@Tutorial] set AreaCode\[‘650']\[‘Burlingame\_\_650’]=’37.57x122.34';

\[default@Tutorial] set Npanxx\['650222']\['lat']='37.57';

…

## 4.8. Deletion

## 4.8.1. Sample Code

InsertRowsForColumnFamilies.java

**public** QueryResult&lt;?&gt; execute() {

        Mutator&lt;String&gt; mutator = HFactory.*createMutator*(keyspace, *stringSerializer*);

       

        //Mutator.addDeletion(String key, String cf, String columnName, Serializer&lt;String&gt; nameSerializer)

        //columnName as null means to delete the whole row.

        mutator.addDeletion("CA Burlingame", "StateCity", **null**, *stringSerializer*);

        mutator.addDeletion("650", "AreaCode", **null**, *stringSerializer*);

        mutator.addDeletion("650222", "Npanxx", **null**, *stringSerializer*);

        // adding a non-existent key like the following will cause the insertion of a tombstone

        // mutator.addDeletion("652", "AreaCode", null, stringSerializer);

        MutationResult mr = mutator.execute();

        **return** **null**;

 

    }

## 4.8.2. Sample Code run by Maven

Omitted…

## 4.8.3. CLI

\[default@Tutorial] del StateCity\['CA Burlingame'];

\[default@Tutorial] del AreaCode\['650'];

\[default@Tutorial] del Npanxx\['650222'];

**Important Note**: Whatever you use, either java code or CLI, the deletion event will still leave the DeletedColumn row key there marked as Tombstone (hehe, 墓碑, a really good naming) which can be retrieved back by command of ‘list’ like this.

\[default@Tutorial] list StateCity;

Using default limit of 100

\-------------------

**RowKey: CA Burlingame**

\-------------------

RowKey: TX Austin

=&gt; (column=202, value=30.27x097.74, timestamp=1329297768323000)

=&gt; (column=203, value=30.27x097.74, timestamp=1329297768338000)

=&gt; (column=204, value=30.32x097.73, timestamp=1329297768354000)

=&gt; (column=205, value=30.32x097.73, timestamp=1329297768370000)

=&gt; (column=206, value=30.32x097.73, timestamp=1329297768385000)

2 Rows Returned.

Elapsed time: 16 msec(s).

As you see, two rows returned! Even the row of ‘CA Burlingame’ has been deleted.

Even worse, if the deletion of non-existing key will cause an issue called ‘insertion of a tombstone’ which means it will add one more row in the Column Family!!!

Fortrunately, the command of ‘get’ won’t retrieve it back any more.

\[default@Tutorial] get StateCity\['CA Burlingame'];

Returned 0 results.

Elapsed time: 0 msec(s).

 

Go deeper? Please read on.

When will Cassandra remove these tombstones? As I know, two ways:

**1. Wait until gc\_grace\_seconds is timeout (Not verified yet)**

The gc\_grace\_seconds is set per CF and can be updated without a restart.

How to get gc\_grace\_seconds? Simply use CLI:

\[default@Tutorial] show schema;

…

create column family StateCity

  with column\_type = 'Standard'

  and comparator = 'LongType'

  and default\_validation\_class = 'UTF8Type'

  and key\_validation\_class = 'UTF8Type'

  and rows\_cached = 0.0

  and row\_cache\_save\_period = 0

  and row\_cache\_keys\_to\_save = 2147483647

  and keys\_cached = 200000.0

  and key\_cache\_save\_period = 14400

  and read\_repair\_chance = 1.0

  **and gc\_grace = 864000   // 10 days, OMG**

  and min\_compaction\_threshold = 4

  and max\_compaction\_threshold = 32

  and replicate\_on\_write = true

  and row\_cache\_provider = 'ConcurrentLinkedHashCacheProvider'

and compaction\_strategy = 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy';

…

 

**2. The Compaction event (under investigation but no luck yet)**

The Compaction will be triggered automatically.

But how to trigger compaction manually? Use nodetool as well.

C:\\java\\apache-cassandra-1.0.7\\bin&gt;nodetool -h localhost **flush** Tutorial

Starting NodeTool

C:\\java\\apache-cassandra-1.0.7\\bin&gt;nodetool -h localhost **compact** Tutorial

Starting NodeTool

Then we can see some logging messages in the Cassandra console.

But as I found, the tombstones are still here. (WHY???)

C:\\java\\apache-cassandra-1.0.7\\bin&gt;sstable2json ..\\runtime\\data\\Tutorial\\StateCity-hc-9-Data.db

{

"4341204275726c696e67616d65": \[\["650","37.57x122.34",1329316454906000]],

"54582041757374696e": \[\["202","30.27x097.74",1329297768323000], \["203","30.27x097.74",1329297768338000], \["204","30.32x097.73",1329297768354000], \["205","30.32x097.73",1329297768370000], \["206","30.32x097.73",1329297768385000]],

**"616263": \[]**

}

And still appears in the list command. (KAO, 阴魂不散? Big why???)

\[default@Tutorial] list statecity;

Using default limit of 100

\-------------------

RowKey: CA Burlingame

\-------------------

RowKey: TX Austin

=&gt; (column=202, value=30.27x097.74, timestamp=1329297768323000)

=&gt; (column=203, value=30.27x097.74, timestamp=1329297768338000)

=&gt; (column=204, value=30.32x097.73, timestamp=1329297768354000)

=&gt; (column=205, value=30.32x097.73, timestamp=1329297768370000)

=&gt; (column=206, value=30.32x097.73, timestamp=1329297768385000)

\-------------------

**RowKey: abc**

 

3 Rows Returned.

Elapsed time: 31 msec(s).

 

 

在这儿咱发几句牢骚：

1\. 可能是学习深度还不足的原因，感觉CLI比较弱，适合初始化建模DDL和简单的数据分析；

2\. Tombstone的清理问题还没有最终得到验证，暂时挂起，权当悬案先，以后有答案了再补充、更正