Databases Reference
In-Depth Information
Therefore the emited tuples will be:
# Category
8 Players
8 Players
8 TVs
8 Mounts
0
Phones
2
Phones
17
Phones
21
Phones
Note that the relation between the products on the left and the categories on the right
should be incremented in one unit.
Now, let's explore the persistence used by the Bolt.
public
class
UserHistoryBolt
extends
BaseRichBolt
{
...
private
Set
<
String
>
getUserNavigationHistory
(
String
user
)
{
Set
<
String
>
userHistory
=
usersNavigatedItems
.
get
(
user
);
if
(
userHistory
==
null
)
{
userHistory
=
jedis
.
smembers
(
buildKey
(
user
));
if
(
userHistory
==
null
)
userHistory
=
new
HashSet
<
String
>();
usersNavigatedItems
.
put
(
user
,
userHistory
);
}
return
userHistory
;
}
private
void
addProductToHistory
(
String
user
,
String
product
)
{
Set
<
String
>
userHistory
=
getUserNavigationHistory
(
user
);
userHistory
.
add
(
product
);
jedis
.
sadd
(
buildKey
(
user
),
product
);
}
...
}
The
getUserNavigationHistory
method returns the set of products that the user has
visited. First, attempt to get the user's history from local memory with
usersNavigate
dItems.get(
user
)
, but if it's not there, read from the Redis server using
jedis.smem
bers(buildKey(user))
and add the entry to the memory structure
usersNavigatedItems
.
When the user navigates to a new product, call
addProductToHistory
to update both
the memory structure with
userHistory.add(product)
and the Redis server structure
with
jedis.sadd(
buildKey
(
user
),
product
)
.