Database Reference
In-Depth Information
static final
byte
[]
INFO_COLUMNFAMILY
=
Bytes
.
toBytes
(
"info"
);
static final
byte
[]
NAME_QUALIFIER
=
Bytes
.
toBytes
(
"name"
);
static final
byte
[]
LOCATION_QUALIFIER
=
Bytes
.
toBytes
(
"location"
);
static final
byte
[]
DESCRIPTION_QUALIFIER
=
Bytes
.
toBytes
(
"description"
);
public
Map
<
String
,
String
>
getStationInfo
(
HTable table
,
String
stationId
)
throws
IOException
{
Get get
=
new
Get
(
Bytes
.
toBytes
(
stationId
));
get
.
addFamily
(
INFO_COLUMNFAMILY
);
Result res
=
table
.
get
(
get
);
if
(
res
==
null
) {
return null
;
}
Map
<
String
,
String
>
resultMap
=
new
LinkedHashMap
<
String
,
String
>();
resultMap
.
put
(
"name"
,
getValue
(
res
,
INFO_COLUMNFAMILY
,
NAME_QUALIFIER
));
resultMap
.
put
(
"location"
,
getValue
(
res
,
INFO_COLUMNFAMILY
,
LOCATION_QUALIFIER
));
resultMap
.
put
(
"description"
,
getValue
(
res
,
INFO_COLUMNFAMILY
,
DESCRIPTION_QUALIFIER
));
return
resultMap
;
}
private static
String
getValue
(
Result res
,
byte
[]
cf
,
byte
[]
qualifier
) {
byte
[]
value
=
res
.
getValue
(
cf
,
qualifier
);
return
value
==
null
?
""
:
Bytes
.
toString
(
value
);
}
In this example,
getStationInfo()
takes an
HTable
instance and a station ID. To
get the station info, we use
get()
, passing a
Get
instance configured to retrieve all the
column values for the row identified by the station ID in the defined column family,
INFO_COLUMNFAMILY
.
The
get()
results are returned in a
Result
. It contains the row, and you can fetch cell
values by stipulating the column cell you want. The
getStationInfo()
method con-
verts the
Result
into a more friendly
Map
of
String
keys and values.
We can already see how there is a need for utility functions when using HBase. There are
an increasing number of abstractions being built atop HBase to deal with this low-level in-
teraction, but it's important to understand how this works and how storage choices make a
difference.