Quantcast
Channel: Wick's Workbench in the Cloud
Viewing all articles
Browse latest Browse all 15

Learning about CouchDB

$
0
0

To continue my learning about NoSQL databases, I decided to try CouchDB on a Ubuntu 12.10 system using MonoDevelop for an IDE.

Using the Synaptic Package Manger, I installed MonoDevelop. This installed version 3.03.2 of MonoDevelop and supporting libraries. I also installed nunit (2.6.0.1205) at the same time.

My next step was to install CouchDB. I did this from a terminal by issuing the following command.

apt-get install couchdb

To check that CouchDB is operational, I performed two simple quick tests to see if CouchDB was setup. The first was to open a web browser and go to the url http://localhost:5984. This returned a result of

{"couchdb":"Welcome","version":"1.2.0"}

. My next test was to open a terminal and try the command curl http://localhost:5984/ This returned

{"couchdb":"Welcome","version":"1.2.0"}.

It looks like CouchDB is ready to go.

There needs to a be a way to administer and configure this database. The administrative interface is accessed by a web browser. The URL is http://localhost:5984/_utils/. CouchDB calls this interface “Futon”.

More information about Futon can be found at the CouchDB Wiki.

From within Funton, I created my first database named ‘wickfirstcouchdb’. I had to read read the screen carefully, there is a restriction for the database name. It must lowercase alphabetical characters only.

In a web browser, I navigated to http://localhost:5984/wickfirstcouchdb. This returned a value of

{"db_name":"wickfirstcouchdb","doc_count":0,"doc_del_count":0,"update_seq":0,"purge_seq":0,"compact_running":false,"disk_size":79,"data_size":0,"instance_start_time":"1362266551217552","disk_format_version":6,"committed_update_seq":0}

So far everything is looking good and I am ready for the next step. I spent sometime reading the CouchDB Wiki. I found the HTTP Document API to be helpful in gettng my mind around how to work with CouchDB.

In MonoDevelop, I started a new C# console application and named it ‘LearningCouchDB’. In order to work with the data from CouchDB, a third party library to handle the serialization and deserialization of JSON data. For this project, I am using JSON.NET.

The whole point of this project is to learn about CouchDB. So I will not be using good programming practices.

Here is my code

using System;
using System.Net;
using System.IO;
using System.Text;
using Newtonsoft.Json;

namespace LearningCouchDB
{
	class MainClass
	{
		const string COUCHDBURL = "http://127.0.0.1:5984/";

		public static void Main (string[] args)
		{
			GetMyDatabase("_all_dbs");
			GetMyDatabase("wickfirstcouchdb");
			WriteDocument();
			GetDocument("2752d78cc3de4b87136af3850c031547");
		}

		static void GetMyDatabase(string dbName)
		{
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(COUCHDBURL + dbName);
			request.Method = "GET";

			using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
			{

				Console.WriteLine("Results Get {0}", dbName);
				using (StreamReader reader = new StreamReader( response.GetResponseStream()))
				{
					Console.WriteLine("\t{0}",reader.ReadToEnd());
				}
			}
		}

		static void WriteDocument()
		{
			SimpleDoc docData = new SimpleDoc();
			docData.SiteName = "CNN";
			docData.URL = "http://www.cnn.com";
			docData.Notes = string.Empty;

			string jsonData = JsonConvert.SerializeObject(docData);

			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(COUCHDBURL + "wickfirstcouchdb");
			request.Method = "POST";

			byte[] bytes = UTF8Encoding.UTF8.GetBytes( jsonData );
			request.ContentLength = bytes.Length;
			request.ContentType = "application/json";
			using (Stream dataStream = request.GetRequestStream())
			{
				dataStream.Write(bytes, 0, bytes.Length);
			}

			using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
			{

				Console.WriteLine("Results From Write Document ");
				using (StreamReader reader = new StreamReader( response.GetResponseStream()))
				{
					Console.WriteLine("\t{0}",reader.ReadToEnd());
				}
			}
		}

		static void GetDocument(string id)
		{
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(COUCHDBURL + "wickfirstcouchdb/" + id);
			request.Method = "GET";

			using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
			{

				Console.WriteLine("Results Get Document {0}", id);
				using (StreamReader reader = new StreamReader( response.GetResponseStream()))
				{
					Console.WriteLine("\t{0}",reader.ReadToEnd());
				}
			}
		}
	}

	public class SimpleDoc
	{

		public string SiteName {
			get;
			set;
		}

		public string URL {
			get;
			set;
		}

		public string Notes {
			get;
			set;
		}

	}
}

 

 

Resources

 


Viewing all articles
Browse latest Browse all 15

Trending Articles