Databases Reference
In-Depth Information
...
}
As mentioned earlier, use the ProductsReader helper class to read the product specific
information.
package
storm
.
analytics
.
utilities
;
...
public
class
ProductsReader
{
...
public
Product
readItem
(
String
id
)
throws
Exception
{
String
content
=
jedis
.
get
(
id
);
if
(
content
==
null
||
(
"nil"
.
equals
(
content
)))
return
null
;
Object
obj
=
JSONValue
.
parse
(
content
);
JSONObject
product
=(
JSONObject
)
obj
;
Product
i
=
new
Product
((
Long
)
product
.
get
(
"id"
),
(
String
)
product
.
get
(
"title"
),
(
Long
)
product
.
get
(
"price"
),
(
String
)
product
.
get
(
"category"
));
return
i
;
}
...
}
UserHistoryBolt
The UserHistoryBolt is the core of the application. It's responsible for keeping track of
the products navigated by each user and determining the result pairs that should be
incremented.
You'll use the Redis server to store product history by user, and you'll also keep a local
copy for performance reasons. You hid the data access details in the methods
getUser
NavigationHistory(
user
)
and
addProductToHistory(
user
,
prodKey
)
for read and write
access, respectively.
package
storm
.
analytics
;
...
public
class
UserHistoryBolt
extends
BaseRichBolt
{
@Override
public
void
execute
(
Tuple
input
)
{
String
user
=
input
.
getString
(
0
);
String
prod1
=
input
.
getString
(
1
);
String
cat1
=
input
.
getString
(
2
);
// Product key will have category information embedded.
String
prodKey
=
prod1
+
":"
+
cat1
;
Set
<
String
>
productsNavigated
=
getUserNavigationHistory
(
user
);
// If the user previously navigated this item -> ignore it
if
(!
productsNavigated
.
contains
(
prodKey
))
{
// Otherwise update related items