Versioning and why

As we already discussed version for a record, there is a tuple consisting of {row, column, version} that defines a cell and its value. We can have as many versions as we want, but the number of versions should be decided optimally as it is storage dependent. It means the more versions, the more disk space it requires (it is possible to have an unbounded number of cell versions).

The version is denoted using a long value. The versioned values are stored in descending order so as to keep the most recent value on top. So, when we fetch records, the most recent version is returned.

Let's consider a scenario to version HBase.

Suppose we have an employee database with the employee_history table where we need to keep all the details of a person's previous company. We can enable versioning (increase to whatever number we like to have after a default of 3) and keep all previous and current details of the employee, such as their employment history and scenarios similar to this.

We can visualize the version and its value in the following diagram, where we have Employee_Employment_History_Table (which can be associated with the employee table) containing a column family as Employment_History and columns as Companies, Salaries, and Posts Held, with version of records as 3; this means we can have three values for each column. As we store the value, the current value will always be fetched. So suppose there is a person named A, then Companies column can contain values such as Acompany1, Acompany2, and Acompany3 specifying the companies the person worked with.

The following table shows how we can get the salary or other column value of an employee (current and previous):

Versioning and why

Now, let's see how to fetch the versions of the record.

As we discussed, Get is used to fetch records from a table, so we can use Get.setMaxVersion(number of version we need to return). Using this function, we can read a specified number of records. We can see the code example as follows:

Get get = new Get(Bytes.toBytes("rowkey"));
get.setMaxVersions(5); 
Result resultSet = HTableObj.get(get);

So, this will return five versions of a record. Likewise, we can specify 1, 2, 3, or the other versions we want to fetch. This will not return not only Version 5, but all five versions of the record.

A detailed explanation of versions in various operations such as Get, Put, and Delete will be discussed in the upcoming chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.12.156