Prash's Blog

Nodejs application to insert data into couchdb April 23, 2012

Filed under: Nodejs — prazjain @ 11:56 am
Tags: , , , ,

In this example, we will make a node application to add some data into couchdb.

  • First lets start couchdb by running this bat file on your machine : “C:\Program Files (x86)\Apache Software Foundation\CouchDB\bin\couchdb.bat” (I have Win 7, your path maybe different), if you have configured couchdb to not run as a service on your machine.
  • Create a database “geoexample” in couchdb through Futon here : http://127.0.0.1:5984/_utils/index.html
  • Lets get some geo data to insert in couchdb. Flickr is a good source for geotagged photos, so lets use that. To use flickr photos, first you need to signin into flickr (signup if you are not registered already). Once you are logged into flickr go to -> You -> Your Apps, then click on Get Another Key : Apply for non-commercial key, then Flickr will ask you the name and details of your application, go on add anything you like. When you submit you will get your api key, which we will use to import data from flickr.
  • Copy paste the url below into browser after replacing [YOUR-KEY] with your flickr api key :

http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=[YOUR-KEY]&text=holi&has_geo=true&extras=geo&per_page=1000&format=json&nojsoncallback=1

This will give you a JSON response, save it as “data.json”.

  • Create a directory geoex for our node app
mkdir geoex
cd geoex
  • Copy data.json into our geoex directory.
  • Install cradle module using npm, cradle is used to interact with CouchDB
npm install cradle

(I tried installing cradle with -g switch but I got the error as below when running the application, so i will not recommend using -g switch)

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
    Error: Cannot find module 'cradle'
  • Create importflickr.js in geoex with code as below :
function importdata()
{
	var cradle = require("cradle"),
		sys = require("sys"),
		fs = require("fs");

	var connection = new (cradle.Connection)("localhost",5984);
	var db = connection.database('geoexample');

	data = fs.readFileSync("data.json","utf-8");

	flickr = JSON.parse(data);

	for(p in flickr.photos.photo)
	{
		photo = flickr.photos.photo[p];

		photo.geometry = {"type":"Point", "coordinates" : [photo.longitude, photo.latitude] };

		// save url to flickr image
		photo.image_url_small = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+"_s.jpg";

		db.save(photo.id,photo,function(er,ok)
			{
				if (er) {sys.puts("error: " + er); return; }
			});
	}
}
exports.importdata = importdata;

  • Create index.js with following code
var http = require("http");
var url = require("url");
var importd = require("./importflickr");

function onRequest(request, response)
{
	var pathname = url.parse(request.url).pathname;
	console.log("Request for " + pathname + " received.");
	if(pathname=="/import")
	{
		importd.importdata();
		response.writeHead(200,{"Content-Type":"text/plain"});
		response.write("Imported the data from flickr into couchdb");
		response.end();
	}
}
http.createServer(onRequest).listen(9999);
console.log("Server has started.");

  • This shall be all that is required, run your app
node index.js
Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s