Databases Reference
In-Depth Information
The download thread locks pendingToSave, and creates a new empty buffer for the
other threads to use while it sends the old one to Redis. This code block runs each
downloadTime milliseconds and is configurable through the download-time topology
configuration parameter. The longer the download-time is, the fewer writes to Redis
are performed because consecutive adds to a pair are written just once.
Keep in mind that again, as in the previous bolt, it is extremely important to apply the
correct fields grouping when assigning sources to this bolt, in this case grouping by
product. That's because it stores in-memory copies of the information by product, and
if several copies of the cache and the buffer exist there will be inconsistencies.
NewsNotifierBolt
The NewsNotifierBolt is in charge of notifying the web application of changes in the
statistics, in order for users to be able to view changes in real time. The notification is
made by HTTP POST using
Apache HttpClient
, to the URL configured in the web
server parameter of the topology configuration. The POST body is encoded in JSON.
This bolt is removed from the topology when testing.
package
storm
.
analytics
;
...
public
class
NewsNotifierBolt
extends
BaseRichBolt
{
...
@Override
public
void
execute
(
Tuple
input
)
{
String
product
=
input
.
getString
(
0
);
String
categ
=
input
.
getString
(
1
);
int
visits
=
input
.
getInteger
(
2
);
String
content
=
"{ \"product\": \""
+
product
+
"\", \"categ\":\""
+
categ
+
"\",
\"visits\":"
+
visits
+
" }"
;
HttpPost
post
=
new
HttpPost
(
webserver
);
try
{
post
.
setEntity
(
new
StringEntity
(
content
));
HttpResponse
response
=
client
.
execute
(
post
);
org
.
apache
.
http
.
util
.
EntityUtils
.
consume
(
response
.
getEntity
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
reconnect
();
}
}
...
}
The Redis Server
Redis is an advanced in-memory Key Value Store with support for persistence (see
http://redis.io
). Use it to store the following information: