Database Reference
In-Depth Information
One of the strengths of HBase over a relational database is that you don't have to specify
all the columns up front. So, if each station now has at least these three attributes but there
are hundreds of optional ones, in the future we can just insert them without modifying the
schema. (Our application's reading and writing code would, of course, need to be
changed. The example code might change in this case to looping through
Result
rather
than grabbing each value explicitly.)
Here's an example of a station query:
%
hbase HBaseStationQuery 011990-99999
name SIHCCAJAVRI
location (unknown)
description (unknown)
Observation queries
Queries of the
observations
table take the form of a station ID, a start time, and a
maximum number of rows to return. Since the rows are stored in reverse chronological or-
der by station, queries will return observations that preceded the start time. The
getSta-
tionObservations()
method in
Example 20-4
uses an HBase scanner to iterate over
the table rows. It returns a
NavigableMap<Long, Integer>
, where the key is the
timestamp and the value is the temperature. Since the map sorts by key in ascending or-
der, its entries are in chronological order.
Example 20-4. An application for retrieving a range of rows of weather station observa-
tions from an HBase table
public class
HBaseTemperatureQuery
extends
Configured
implements
Tool
{
static final
byte
[]
DATA_COLUMNFAMILY
=
Bytes
.
toBytes
(
"data"
);
static final
byte
[]
AIRTEMP_QUALIFIER
=
Bytes
.
toBytes
(
"airtemp"
);
public
NavigableMap
<
Long
,
Integer
>
getStationObservations
(
HTable
table
,
String stationId
,
long
maxStamp
,
int
maxCount
)
throws
IOException
{
byte
[]
startRow
=
RowKeyConverter
.
makeObservationRowKey
(
stationId
,
maxStamp
);
NavigableMap
<
Long
,
Integer
>
resultMap
=
new
TreeMap
<
Long
,
Integer
>();
Scan scan
=
new
Scan
(
startRow
);
scan
.
addColumn
(
DATA_COLUMNFAMILY
,
AIRTEMP_QUALIFIER
);
ResultScanner scanner
=
table
.
getScanner
(
scan
);
try
{
Result res
;
int
count
=
0
;