Read value from Cloudant NoSQLDB in Node-RED,IBM Bluemix

Q: How to read value from document in Cloudant NoSQLDB in Node-RED?

Document: document

Output: Riya Mary Roy ( Value of name)

1_node

Inject node configuration:

2_Node

Cloudant node configuration:

3_node

Function node configuration:

4_node

Debug node configuration:

5_node

Output on the console:

Output

Advertisements

Hashtag sentiment Analyzer using Node-RED,Cloudant, dashDB,R script

2

Twitter Node Configuration:7

Switch Node Configuration:

3

Not so positive function node configuration:

4

Positive sentiment Function Node configuration:

5

Cloudant Node Configuration:

6

Once after wiring the nodes, click on DEPLOY ( top right corner of Node-RED).

Launch Cloudant and then you will find your database in the list

8

Create a dashDB warehouse to graphically analyze the sentiment

9

Goto dashDB and then run R script

10

Rscript.PNG

Data has been graphically analyzed using R Scripts.

1

1 represents Positive sentiment and 0 represents not-so-Positive sentiment

 

Web app using Java & Cloudant NoSQLDatabase in IBM Bluemix

IBM® Bluemix™ is the Platform as a Service(PaaS) cloud offering from IBM®. It enables organizations and developers to quickly and easily create, deploy, and manage applications on the cloud. Bluemix delivers enterprise-level services that can easily integrate with your cloud applications without you needing to know how to install or configure them.

IBM® Cloudant® NoSQL Database for Bluemix® is a NoSQL database as a service (DBaaS). It’s built from the ground up to scale globally, run non-stop, and handle a wide variety of data types like JSON, full-text, and geospatial. Cloudant NoSQL DB is an operational data store optimized to handle concurrent reads and writes, and provide high availability and data durability.

This tutorial will help you to build and deploy a java application that inserts registration details to Cloudant NoSQLDB

Prerequisites

Let’s start building the java application,before that lets go to our Bluemix Catalog and select required services to build it!

Step 1: Goto Catalog and then click on Java Runtime

blog_2

Step 2: Enter your application name. Eg: samplecloudant as shown in the image below. Click on CREATE

Step 3: Application has been created and it can be viewed on the Bluemix DASHBOARD. Click on the application

Step 4: Click on ADD SERVICE in the application(samplecloudant) DASHBOARD

Step 5: In CATALOG goto Data and Analytics and click on Cloudant NoSQL DB

Step 6: Change the Service name of Cloudant NoSQL DB to myCloudant and click on CREATE.

The service is now up and running in Bluemix! We can now proceed to Eclipse IDE to build the registration application that inserts details into Cloudant NoSQL Database.

We will be creating this web application using a Maven project in Eclipse(it is the easiest way to retrieve the dependencies we need — the ektorp client jars).

Step 7: Open Eclipse and create a new Maven project

Set the following values

  • Group ID: com.register
  • Artifact ID: registerblue
  • Version: 1
  • Packaging: war
  • Leave everything else as-is and Click Finish

 

 

Copy dependencies into pom.xml inside <project>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
<version>1.4.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

Create a servlet

  • Click on registerblue>src>main>java
  • Right click on register folder and then New > (Other) > Web > Servlet
  • Keep class name as RegisterServlet & package name as com and then click on Finish

In web.xml

<servlet>
<display-name>RegisterServlet</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>

In RegisterServlet.java ( Resource Injection)

@Resource(name = “couchdb/myCloudant”)
protected CouchDbInstance mydb ;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

response.setContentType(“text/html”);
response.sendRedirect(request.getContextPath() + “/success.jsp”);

String name=request.getParameter(“name”);
String emailid = request.getParameter(“emailid”);
String mobile = request.getParameter(“mobile”);
String company=request.getParameter(“company”);
String loc=request.getParameter(“loc”);

String dbname = “registerdb”;
try {
//creates a database with the specified name
CouchDbConnector dbconnector = mydb.createConnector(dbname, true);
//create a document to place into your new database
Map<String, Object> doc = new HashMap<String, Object>();
doc.put(“_id”, UUID.randomUUID().toString());
doc.put(“name”, name);
doc.put(“emailid”, emailid);
doc.put(“mobile”, mobile);
doc.put(“company”, company);
doc.put(“location”, loc);

dbconnector.create(doc);
} catch (Exception e) {
response.getWriter().println(e.getMessage());
}
}

Export the WAR file and push it into bluemix using Cloudfoundry command line interface.

Once the application has started,insert the details through registration page.

To view the inserted details,click on the Cloudant service that is bound to your application.

Click on LAUNCH, it will take you to database list,you can now find your database registerdb in the list.

Click on registerdb and then you can view your documents on the right side.

You can create a view in Design Documents section and then copy paste API URL(top right) to view the documents in the browserdetails

Courtesy:IBM Bluemix and Cloudant NoSQL DB!

Simple application using Node-RED Starter(SDK for Node js, Cloudant NoSQL database) in IBM Bluemix

bluemix1

IBM® Bluemix™ is the Platform as a Service(PaaS) cloud offering from IBM®. It enables organizations and developers to quickly and easily create, deploy, and manage applications on the cloud. Bluemix delivers enterprise-level services that can easily integrate with your cloud applications without you needing to know how to install or configure them. Bluemix provides a dashboard for you to create, view, and manage your applications and services as well as monitor your application’s resource usage. Bluemix dashboard also provides the ability to manage organizations, spaces, and user access.

Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.Node-RED provides a browser-based flow editor that makes it easy to wire together flows using the wide range nodes in the palette. Flows can be then deployed to the runtime in a single-click.JavaScript functions can be created within the editor using the a rich text editor.A built-in library allows you to save useful functions, templates or flows for re-use.

In IBM® Bluemix™, a boilerplate is a container for an application and its associated runtime environment and predefined services for a particular domain. You can use a boilerplate to quickly get up and running. The Node-RED Starter boilerplate has Node-RED,Cloudant databse and Monitoring and Analytics Services as a combo pack.You don’t have to spend time in adding or binding services.

The steps below will help you to build a simple twitter feed application and insert those tweets into Cloudant database with the Node-RED Starter boilerplate.

Prerequisites

Step 1: Choose the Boilerplate Node-RED Starter from the CATALOG tab1.2

Step 2: Input the host name and click on CREATE

1.1

Step 3: Hurray! The application is created with Cloudant NoSQL DB and SDK for Node js

blog2

Step 4: One instance of the application is running perfectly! Click on the URL

blog3Step 5: Click on the button Go to your Node-RED flow editor


3

Step 6: The Node-RED page opens up with the nodes for each functionality on the left side


4.1

Step 7: Drag and drop the twitter node to the sheet1 (ensure you drag and drop the correct node)

twitter

Step 8: Double click on the twitter node and enter the required fields(ensure that you authenticate it with your twitter id) and click Oktwitter3Step 9: Drag and drop debug node in sheet 1


debug

Step 10: Enter the required field as shown below

debug2

Step 11: Drag and drop cloudant node to sheet1(ensure that you have choosen the correct node)

cloudant

Step 12: Double click the node and enter the required details

cloudant2

Step 13: Wire the nodes from its node points and click on DEPLOY

out

Step 14:Yes,now you can see live tweets for the hastag #bluemix on your debug console!!

output

Step 15: Goto the cloudant database

cloudb

Step 16: Click on Launch

cloud1

Step 17: You can see the database (tweets) ! Click on it. 

cloud2

Step 18: You can see the documents with unique id and _rev

we

Step 19: Click on one of the document and you can see the tweet,date,time,created by whom and all other details in JSON format.

out2

You can use the documents inserted in the Cloudant NoSQL database for further use and Yes,the application was ready within few minutes! That’s the power of Node-RED in IBM Bluemix. You can drag and drop other several nodes which are available in Node-RED to make this a better application(for analysis,for sending notifications etc)

Thanks to Node-Red,Cloudant NoSQL database and IBM Bluemix! You just saved our time from writing 1000 lines of code!

Courtesy: IBM Bluemix, Node-RED, Cloudant NoSQL Database