[SAP HANA Academy] Live3: node.js Application
[Update: April 5th, 2016 – The Live3 on HCP tutorial series was created using the SAP HANA Cloud Platform free developer trial landscape in January 2015. The HCP landscape has significantly evolved over the past year. Therefore one may encounter many issues while following along with the series using the most recent version of the free developer trail edition of HCP.]
In this segment of the SAP HANA Academy’s Live3 course Philip Mugglestone reviews the node.js application. The node.js application will create a local web server which will access the Twitter streaming API. It will then receive and parse the tweets before loading them into the Tweets table in the SAP HANA database. Check out Philip’s tutorial video below.
(0:20 – 2:20) Overview of package.json File
Open the nodejs folder from the Live3 Github code repository. Open both of the files in folder using Notepad++. Node.js is very flexible as it has a lot of libraries that enable us to reuse work others have already done.
The package.json file has a list of dependencies that allows us to specify for the Twitter library, hdb, oauth and express. This enables us to easily create a web server that makes htp calls that start and stop loading the data. The emoji strip will remove emojis from the tweets.
We are able to install all of these libraries with one simple npm node installation command as opposed to installing each individual one manually.
(2:20 – 8:35) Overview of app.js File.
The next file, called app.js, has about 100 lines of code. The first part of the code references the different libraries we will use and puts them into variables. Next a variable called twitter is created where we have to specify our consumer key, consumer secret, access token key and access token secret. Copy these four items from the OAuth Tool webpage from the Twitter application you registered and paste them into their corresponding lines of code. This allows you to authenticate to the Twitter API using your personal Twitter account credentials.
The next part of the code deals with connecting to SAP HANA. We currently don’t have the necessary information (host name, port number, etc.) for this part but we will get it in the next video.
Moving along in the code we will call a library named express that sets up a simple web server in node.js. Then we do some overall set up for how people can access our web server. Next we use app.get to create do/start and do/stop URLs. These two functions define what happens when you make a call.
At the end of the code is a variable called server which fires up the application to do the processing in our web browser. Philip is using port 8888 but you can use any port you desire.
When you do a start it checks to see if you already have a Twitter stream running and if so, it kills the running stream. Then it checks for the track parameter in your URL. We will substitute the item we are tracking using JSON format for the Twitter API.
Whenever a Tweet is streamed back to us containing the tracking specification a function is called that allows us to do any processing we want. It converts the Tweet to JSON format and then pulls out the desired information. Then a SQL statement is built out which substitutes in the values from Twitter and then inserts the tweet into the SAP HANA table.
(8:35 – 10:25) How to Install package.json Dependencies with Command Line
Open a command line window in the node.js folder. Type in npm install and hit enter to install all of the dependencies from the package.json.
Now it is possible in some environments that a bug occurs in the npm if a folder is missing. This folder is called C:\Users\Administrator\AppData\Roaming\npm.
If this folder doesn’t exist and the installation fails then navigate to the Roaming folder in the AppData folder and create a new folder called npm. Now rerun npm install and all of the dependencies will be installed. Also now in the node.js folder there is a new folder called node_modules that contains the code for all of the different modules.
Follow along with the Live3 course here.
SAP HANA Academy over 900 free tutorial videos on using SAP HANA and SAP HANA Cloud Platform.