At the Forge - Cassandra
Restart Cassandra, and reconnect via the CLI. Then, type:
cassandra> show keyspaces
Your new keyspace, “People”, now should appear in the list:
cassandra> show keyspaces Keyspace1 system People
You can ask for a description of your keyspace:
cassandra> describe keyspace People People.Users Column Family Type: Standard Columns Sorted By: org.apache.cassandra.db.marshal.BytesType@1b22920 Column Family Type: Standard Column Sorted By: org.apache.cassandra.db.marshal.BytesType flush period: null minutes ------
You now can see that your People keyspace contains a single “Users” column family. With this in place, you can start to set and retrieve data:
cassandra> get People.Users['1'] Returned 0 results. cassandra> set People.Users['1']['email'] = 'email@example.com' cassandra> set People.Users['1']['first_name'] = 'Reuven' cassandra> set People.Users['1']['last_name'] = 'Lerner'
In Cassandra-ese, you would say that you now have set three column values ('email', 'first_name' and 'last_name'), for one key ('1') under a single column family (“Users”), within a single Keyspace (“People”). If you're used to working with a language like Ruby or Python, you might feel a bit underwhelmed—after all, it looks like you just set a multilevel hash. But that makes sense, given that Cassandra is a super-version of a key-value store, right?
Now, let's try to retrieve the data. You can do that with the key:
cassandra> get People.Users['1'] => (column=6c6173745f6e616d65, value=Lerner, ↪timestamp=1279024194314000) => (column=66697273745f6e616d65, value=Reuven, ↪timestamp=1279024183326000) => (column=656d61696c, firstname.lastname@example.org, timestamp=1279024170585000) Returned 3 results.
Notice how each column has its own unique ID and that the data was stored with a timestamp. Such timestamps are crucial when you are running multiple Cassandra nodes, and they update one another without your knowledge to achieve complete consistency.
You can add additional information too:
cassandra> set People.Users['2']['first_name'] = 'Atara' cassandra> set People.Users['2']['last_name'] = 'Lerner-Friedman' cassandra> set People.Users['2']['school'] = 'Yachad' cassandra> set People.Users['3']['first_name'] = 'Shikma' cassandra> set People.Users['3']['last_name'] = 'Lerner-Friedman' cassandra> set People.Users['3']['school'] = 'Yachad'
Now you have information about three users, and as you can see, the columns that you used within the “Users” column family were not determined by the configuration file and can be added on the spot. Moreover, there is no rule saying that you must set a value for the “email” column; such enforcement doesn't exist in Cassandra. But what is perhaps most amazing to relational database veterans is that there isn't any way to retrieve all the values that have a last_name of 'Lerner-Friedman' or a school named 'Yachad'. Everything is based on the key (which I have set to an integer in this case); you can drill down, but not across, as it were.
You can ask Cassandra how many columns were set for a given key, but you won't know what columns those were:
cassandra> count People.Users['1'] 3 columns cassandra> count People.Users['2'] 3 columns
However, if you're trying to store information about many users, and those users are going to be updating their information on a regular basis, Cassandra can be quite helpful.
Now that you've got the hang of columns, I'll mention a particularly interesting part of the Cassandra data model. Instead of defining columns, you instead can define “super columns”. Each super column is just like a regular column, except it can contain multiple columns within it (rather than a name-value pair). In order to define a super column, set the ColumnType attribute in the storage-conf.xml file to “Super”. For example:
<ColumnFamily Name="Users" CompareWith="BytesType" ↪ColumnType="Super" />
Note that if you restart Cassandra with this changed definition, and then try to retrieve People.Users['1'], you'll probably get an error. That's because you effectively have changed the schema without changing the data, which always is a bad idea. Now you can store and retrieve finer-grained information:
cassandra> set People.Users['1']['address']['city'] = 'Modiin' cassandra> get People.Users['1']['address']['city'] => (column=63697479, value=Modiin, timestamp=1279026442675000)
Webinar: 8 Signs You’re Beyond Cron
On Demand NOW
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.View Now!
|Dr Hjkl on the Command Line||May 21, 2015|
|Initializing and Managing Services in Linux: Past, Present and Future||May 20, 2015|
|Goodbye, Pi. Hello, C.H.I.P.||May 18, 2015|
|Enter to Win Archive DVD + Free Backup Solution||May 18, 2015|
|Using Hiera with Puppet||May 14, 2015|
|Urgent Kernel Patch for Ubuntu||May 12, 2015|
- Dr Hjkl on the Command Line
- Initializing and Managing Services in Linux: Past, Present and Future
- Goodbye, Pi. Hello, C.H.I.P.
- Using Hiera with Puppet
- Enter to Win Archive DVD + Free Backup Solution
- Gartner Dubs DivvyCloud Cool Cloud Management Vendor
- Infinite BusyBox with systemd
- Mumblehard--Let's End Its Five-Year Reign
- It's Easier to Ask Forgiveness...
- Urgent Kernel Patch for Ubuntu