on
Italy
- Get link
- X
- Other Apps
/**
* Set up your blocking queues: Be sure to size these properly based
* on expected TPS of your stream
*/
BlockingQueue msgQueue = new LinkedBlockingQueue(10000);
/**
* Declare the host you want to connect to, the endpoint, and
* authentication (basic auth or oauth)
*/
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// Track anything that is geotagged
endpoint.addQueryParameter("locations", "-180,-90,180,90");
// These secrets should be read from a config file
Authentication hosebirdAuth = new OAuth1(consumerKey,
consumerSecret, token, secret);
// create a new basic client - by default gzip is enabled
Client client = new ClientBuilder().hosts(Constants.STREAM_HOST)
.endpoint(endpoint).authentication(hosebirdAuth)
.processor(new StringDelimitedProcessor(msgQueue)).build();
client.connect();
As you can see from the code, we use ‘addQueryParameter’ on the
endpoint to filter the data so only geotagged Tweets are sent. The
examples on HBC’s github page show
how to filter for search terms. For example, you could restrict your
analysis to Tweets with a certain hashtag. One thing to note: if you use
more than one filter they will be ‘OR’ed together. If you add an extra
line to the above code to ‘trackTerms’ the results you get back will
either contain your search term or be geotagged, which isn’t what we
want. The way forward here is to provide HBC with the most restrictive
filter (usually the search term) to limit the amount of data sent back
from Twitter and build the second filter directly into your code.// create producer
ProducerClient producer = new ProducerBuilder()
.withName("Twitter")
.withStreamName(streamName)
.withRegion(regionName)
.withThreads(10)
.build();
producer.connect();
The ‘name’ of the producer is used for de-bugging purposes and the
stream and region names are set using variables extracted from the
configuration file. The above call sets up a pool of 10 threads to
process messages that you want to send to Amazon Kinesis. The code below
shows how the Tweets from HBC are wired to the producer:// get message from HBC queue String msg = msgQueue.take(); // use 'random' partition key String key = String.valueOf(System.currentTimeMillis()); // send to Kinesis producer.post(key, msg);
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1392290776000",
"Effect": "Allow",
"Action": [
"kinesis:*", "s3:Get*", "s3:List*"
],
"Resource": [
"*"
]
}
]
}
Start a new m3.medium Amazon EC2 instance using the Amazon Linux AMI.
Assign the IAM role and the Security Group that you created. Modify the
following script by adding the name of your Amazon Kinesis Stream, the
region you are using (eg us-west-1) and the 4 keys that you got when you
set up your Twitter Application. Paste this into the User Data for the
instance (found under the ‘Advanced’ section of the ‘Instance
Configuration’ tab). Finally review and start your instance, remembering
to supply a key pair so that you can get access later.#!/bin/bash # update the instance yum update -y # install jdk yum install java-1.8.0-openjdk -y yum install java-1.8.0-openjdk-devel -y yum install git -y update-alternatives --set java /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-0.b18.4.amzn1.x86_64/jre/bin/java cd /home/ec2-user # install Apache Maven wget http://www.dsgnwrld.com/am/maven/maven-3/3.2.3/binaries/apache-maven-3.2.3-bin.tar.gz tar -xzvf apache-maven-3.2.3-bin.tar.gz # get the code git clone https://github.com/awslabs/aws-big-data-blog.git cp ./aws-big-data-blog/aws-blog-kinesis-data-visualization/TwitterProducer/* /home/ec2-user -r # create the config file echo "aws.streamName = Name of Your Amazon Kinesis Stream" > AwsUserData.properties echo "aws.regionName = Name of your region" >> AwsUserData.properties echo "twitter.consumerKey = Twitter Consumer Key" >> AwsUserData.properties echo "twitter.consumerSecret = Twitter Consumer Secret Key" >> AwsUserData.properties echo "twitter.token = Twitter Access Token" >> AwsUserData.properties echo "twitter.secret = Twitter Access Token Secret" >> AwsUserData.properties echo "twitter.hashtags = " >> AwsUserData.properties # do the build /home/ec2-user/apache-maven-3.2.3/bin/mvn packageThis script will run after the instance has booted and installed Open JDK and Apache Maven. Next, it downloads the source code for the Producer from git and creates the configuration file. Finally, Apache Maven is used to build the source code. To start the producer, follow the instructions to SSH to the instance and type:
java -jar target/TwitterProducer-0.0.1-SNAPSHOT.jar AwsUserData.propertiesYou may have to wait a couple minutes after the server has booted for the Apache Maven build to complete and the jar file to appear on the server. At this point, you should see messages indicating that Tweets are being sent to Amazon Kinesis. Amazon Kinesis Application The Amazon Kinesis Application will retrieve the JSON Tweet from Amazon Kinesis, extract the geo information and publish this to the Amazon ElastiCache Redis Cluster.
Coordinate c = null;
try {
// For this app, we interpret the payload as UTF-8 chars.
data = decoder.decode(record.getData()).toString();
// use the ObjectMapper to read the json string and create a tree
JsonNode node = mapper.readTree(data);
JsonNode geo = node.findValue("geo");
JsonNode coords = geo.findValue("coordinates");
Iterator elements = coords.elements();
double lat = elements.next().asDouble();
double lng = elements.next().asDouble();
c = new Coordinate(lat, lng);
} catch(Exception e) {
// if we get here, its bad data, ignore and move on to next record
}
if(c != null) {
String jsonCoords = mapper.writeValueAsString(c);
jedis.publish("loc", jsonCoords);
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1392290776000",
"Effect": "Allow",
"Action": [
"kinesis:*", "cloudwatch:*", "dynamodb:*", "elasticache:*", "s3:Get*", "s3:List*"
],
"Resource": [
"*"
]
}
]
}
#!/bin/bash # update instance yum update -y # install jdk yum install java-1.8.0-openjdk -y yum install java-1.8.0-openjdk-devel -y yum install git -y update-alternatives --set java /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.25-0.b18.4.amzn1.x86_64/jre/bin/java cd /home/ec2-user # install Apache Maven wget http://www.dsgnwrld.com/am/maven/maven-3/3.2.3/binaries/apache-maven-3.2.3-bin.tar.gz tar -xzvf apache-maven-3.2.3-bin.tar.gz # get the code git clone https://github.com/awslabs/aws-big-data-blog.git cp ./aws-big-data-blog/aws-blog-kinesis-data-visualization/KinesisApplication/* /home/ec2-user -r # create the config file echo "appName = DataVizAnalyzer" > KinesisClient.properties echo "kinesisEndpoint = Name of your region" >> KinesisClient.properties echo "redisEndpoint = Name of the Redis node" >> KinesisClient.properties echo "redisPort = The port number used by Redis" >> KinesisClient.properties echo "kinesisStreamName = The name of your Amazon Kinesis Stream" >> KinesisClient.properties # build /home/ec2-user/apache-maven-3.2.3/bin/mvn package
java -jar target/KinesisClient-0.0.1-SNAPSHOT.jar KinesisClient.propertiesYou should soon see some messages to indicate that data is being extracted from Amazon Kinesis and coordinate data is being sent to Redis.
// subscribe to listen to events from redis redisClient.on("ready", function () { redisClient.subscribe("loc");
});
If Redis publishes a change, it is a very simple process to push that change to any connected users:// When we get a message from redis, we send the message down the socket to the client
redisClient.on('message', function(channel, message) {
var coord = JSON.parse(message);
io.emit('tweet', coord);
});
The node.js code that you downloaded also contains the HTML and javascript code to visualize the geotagged data. var socket = io();
socket.on('tweet', function(coord) {
startLight(coord.lat, coord.lng, 2000, 0x6DAEE1);
});
The spinning globe and flashing lights combine to make a striking
display, but this visualization is limited by the amount of data it can
display. Creating the graphic and flashing lights is very resource
intensive and you may find that if you try to display more than 25
events per second your computer will start to slow.{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1392290776000",
"Effect": "Allow",
"Action": [
"elasticache:*"
],
"Resource": [
"*"
]
}
]
}
Now you can go to the AWS Console for Elastic Beanstalk and create a new application.{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"dnsaz1clusterexamplecom": {
"Type": "AWS::Route53::RecordSetGroup",
"Properties": {
"HostedZoneName": "example.com.",
"RecordSets": [
{
"Name": "az1cluster.example.com.",
"Type": "CNAME",
"TTL": "300",
"ResourceRecords": [
"az1cluster.cbjhdm0le2vh.us-east-1.redshift.amazonaws.com"
]
}
]
}
},
"dnsaz2clusterexamplecom": {
"Type": "AWS::Route53::RecordSetGroup",
"Properties": {
"HostedZoneName": "example.com.",
"RecordSets": [
{
"Name": "az2cluster.example.com.",
"Type": "CNAME",
"TTL": "300",
"ResourceRecords": [
"az2cluster.cbjhdm0le2vh.us-east-1.redshift.amazonaws.com"
]
}
]
}
},
"dnsmyparallelclusterexamplecomt": {
"Type": "AWS::Route53::RecordSetGroup",
"Properties": {
"HostedZoneName": "example.com.",
"RecordSets": [
{
"Name": "myparallelcluster.example.com.",
"Type": "CNAME",
"SetIdentifier": "t",
"Weight": "50",
"ResourceRecords": [],
"AliasTarget": {
"HostedZoneId": "Z1HN6T29675QII",
"DNSName": "az2cluster.example.com"
}
}
]
}
},
"dnsmyparallelclusterexamplecomy": {
"Type": "AWS::Route53::RecordSetGroup",
"Properties": {
"HostedZoneName": "example.com.",
"RecordSets": [
{
"Name": "myparallelcluster.example.com.",
"Type": "CNAME",
"SetIdentifier": "y",
"Weight": "50",
"ResourceRecords": [ ],
"AliasTarget": {
"HostedZoneId": "Z1HN6T29675QII",
"DNSName": "az1cluster.example.com"
}
}
]
}
}
},
"Description": "Evenly Weighted DNS entry myparallelcluster.example.com to two identical Redshift Clusters"
}
In this example, the DNS record myparallelcluster.example.com is evenly weighted to az1.cluster.example.com and az2cluster.example.com, which resolve to our Amazon Redshift cluster endpoints.
Comments
Post a Comment