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)
- DNSMasq, the Pint-Sized Super Dæmon!
- High-Availability Storage with HA-LVM
- Localhost DNS Cache
- Days Between Dates: the Counting
- Real-Time Rogue Wireless Access Point Detection with the Raspberry Pi
- You're the Boss with UBOS
- The Usability of GNOME
- Linux for Astronomers
- Multitenant Sites
- Many Drives, One Folder