Graph Databases Exposed Video Released

New Release! The video of Chris Tite’s latest presentation on Graph Databases Exposed is available for streaming from the Instruct Video Channel.

Recorded on 10 July 2014 at the Benjamin Hotel in Durban, Chris introduced attendees to the concepts underlying Graph Database technologies and their application.

Relational databases have been the cornerstone of data centric systems for almost 30 years, serving as a trusty workhorse upon which many developers depend. Times, however, are changing and datasets are becoming larger and more complex. Business and users expect more flexibility and insight from their data.

With a worldwide movement to connectedness in applications (Facebook, Twitter, Linked In and many more), our trusty relational database model can’t be relied upon to manage complex connected datasets.

This has lead database technologists to find more effect technologies to manage their connected data. Graph Databases, a subset of NoSQL technology, provide a powerful mechanism to work with datasets where relationships between various entities are important.

In this session Chris covers:

• Introduction to graph databases.
• Compares graph databases to relational databases.
• Introduces Neo4j, a powerful open source graph database technology.
• Demonstrates a practical implementation of graph databases in .Net.

Advertisements

Graph Out

photo2

Thanks to all the Durbanites who made it to the lastest Instruct event at The Benjamin. Graph Databases Exposed was a tight ramp up for the attendees. Covering basic NoSQL elements all the way through to hands-on implementation samples, it was a detail packed session.

Talking to some of the guys from SA Home Loans, The Unlimited and Derivco it was a valuable primer for the uninitiated. If you didn’t get to make it to the event, then keep an eye out for the video recording that will get posted to the Instruct Channel in the next week or two.

Our next Instruct session will cover Domain Driven Design on 02 September 2014. If you are interested in attending feel free to drop an email to events@kaleidocode.co.za to get an invite.

NoSQL Explained Instruct Event Video

The complete video recording of the recent NoSQL Explained lecture presented by Chris Tite has been posted to the Instruct YouTube channel. You can find more instruction videos on the channel.

Recorded on 4 February 2014 at the Benjamin Hotel in Durban. Hosted by Chris Tite, entrepreneur, software architect and speaker (http://www.christite.net).

With the onset of web and mobile applications that require high availability and scalability, web developers have started turning away from using relational databases because of scaling limitations. NoSQL technology is gaining momentum and is supported as either commercial or open source solutions.

In this session Chris introduces NoSQL and contrasts it with SQL technology, giving insights into both technologies to help you select the correct tool for the job at hand. Topics covered are:

  • NoSQL compared with SQL – Pros and Cons
  • BASE vs ACID
  • Brewers CAP theorem and how it affects your choice of DB
  • SQL Architectural patterns
  • NoSQL Architectural patterns
  • Hands-on with Mongo DB:
    • an introduction in to Mongo DB,
    • setting up the environment,
    • writing Mongo queries
    • writing a Mongo DB application in .NET

Question and Answer session from the NoSQL Explained Instruct event:

About the author and presenter, Chris Tite:

Chris is a software architect with 20 years experience. He is passionate about quality software and great architecture, while forging the growth of this generation of software developers. Chris holds a B.Sc. Electronic Engineering degree with over 18 years of development experience spanning several platforms and industries. He sports extensive experience with Microsoft tools like ASP.Net, C#.Net, VB.Net and SQL, as well as with ETL and Data Warehousing tools.

Chris is a true software professional, well versed with open source tools and languages, including such staples as PHP, Perl, Postgres, and MySQL hosted on Linux. He has a passion for software architecture and processes. Since 2005, Chris has been enthused by Agile development methods, seeing first hand the delight of clients experiencing progressive and effective delivery of a shared vision.

Session Resources – NoSQL Explained – Demo 3: Connecting to MongoDB using .Net

Using .Net to insert a document into MongoDB

To run this demo please ensure that your MongoDB instance is running before starting. If you are unsure how to get an instance of MongoDB running please view the Session Resources – NoSQL Explained – Running an MongoDB instance page for details on how to get an instance of MongoDB running on your machine.

This demo was prepared using the 10gen MongoDB driver, ensure that your project references MongoDB.Bson and MongoDB.Driver.

Use NuGet to install the correct MongoDB supporting dlls.

I am going to start off the Demo code by defining an Employee class that I will use as my document structure in MongoDB. Recall that MongoDB is a document database so all data entities are referred to as documents which are found in collections. A document in MongoDB often has nested documents thus creating a fairly complex document structure. In this demo we are not going to create any complex structure we are going to work with a very simple flat document which will define an Employee in our system.


    public class Employee
    {
        public ObjectId Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public DateTime DateAdded { get; set; }
    }

Adding your first document using .Net

Now that we have a class that defines our document we can look at the code to insert the document into the MongoDB. Below I have added a few lines of code which will connect to an instance of MongoDB and insert the Employee document into the Employees collection.


	private string AddEmployee(string name, string surname, DateTime dateAdded)
	{
		//using the client connect to the server
		string connectionString = "mongodb://localhost";
		MongoClient mongoClient = new MongoClient(connectionString);

		//using a server connection get a hook to the database
		MongoServer server = mongoClient.GetServer();
		MongoDatabase database = server.GetDatabase("Demo3");

		//Create an employee object
		Employee employee = new Employee {Name = name, Surname = surname,
                   DateAdded = dateAdded};

		//Get a reference to the Employees collection
		MongoCollection employees = database.GetCollection("Employees");

		//Add the document to the collection
		employees.Insert(employee);

		//return the MongoDB unique identifier
		return employee.Id.ToString();
	}

So what is actually happening in the code above?

The code breaks down as follows.

The first four lines of code establish a connection to an instance of MongoDB using the database Demo3. Next we create an new Employee document setting the attributes to the correct values (name, surname and date added). We then get a reference to the Employees collection and finally insert the Employee document into the Employees collection.
We now have an employee document in the Employees collection of the Demo3 database.

Finding the Inserted document

Now that we have an Employee document in our database we would need to find it to be able to provide any form of useful business application. Finding a document is equally as simple as inserting one, the code below shows just how simple it is.

</pre>
     private string FindEmployeeByName(string employeeName)
     {
         //using the client connect to the server
         string connectionString = "mongodb://localhost";
         MongoClient mongoClient = new MongoClient(connectionString);

         //using a server connection get a hook to the database
         MongoServer server = mongoClient.GetServer();
         MongoDatabase database = server.GetDatabase("Demo3");

         //Create a query, find an employee whose name is equal
         //to that supplied in the variable employeeName
         var query = Query.EQ(emp => emp.Name, employeeName);

         //Get a reference to the Employees collection
         MongoCollection employees = database.GetCollection("Employees");

         //Apply query, using the findOne command
         Employee employee = employees.FindOne(query);

         //Return the result
         if (employee != null)
         {
             //Return a concatenated version of the record
             return employee.Name + " " + employee.Surname + " - Added on "
                  + employee.DateAdded;
         }

         return "Not Found!";

     }

The code breaks down as follows.

The first four lines of code establish a connection to an instance of MongoDB using the database Demo3, much like we did when we were inserting a document. For purposes of demonstration I have included these four lines again, when writing actual production code this connection logic would be managed in a separate connection manager class to encourage code reuse.

Next we create a query, telling MongoDB that we wish to find an Employee document where the name is equal (EQ) to the name supplied. We then get a reference to the Employees collection and run the query against the Employees collection, using a findOne command.

Note that we could have used the find command to return a collection of documents which met our search criteria.

We should now have an Employee document, we check if we have a returned document, if so return the required information, in this case a “flattened” version of the record.

Session Resources – NoSQL Explained – Demo 1: MongoDB Database create, data insert and data find

To conduct the first demo in the Instruct session on NoSQL you can follow along with the code below.
This MongoDB demo creates a database, inserts data and finds it.
All these commands are to be typed in sequentially into the command prompt. Please ensure that your MongoDB instance is running before starting.
If you are unsure how to get an instance of MongoDB running please view the Session Resources – NoSQL Explained – Running an MongoDB instance page for details on how to get an instance of MongoDB running on your machine.

We are going to run this demo from the MongoDB command prompt, run the following command.

c:\data_mongo\bin\mongo

This code will start an instance of the MongoDB command prompt which will connect to an instance of MongoDB server running on localhost and listening on the default port 27017.

To connect to an instance of MongoDB running on another server include the machine name and port as command parameters like this.

c:\data_mongo\bin\mongo mumbai:40001

Now for a few quick demo items, type the following commands in sequence to create a database, insert a record and find it.


//Set context to a new database called Demo1.
//Note the database hasn't been created yet we have just told MongoDB
//that we wish to use the database called Demo1

use Demo1

//Create a users collection and insert a document entity into the
//database.
//Note MongoDB will now create a database called Demo1 and include the
//users document collection.
//A new document will be created in the users collection with the
//following parameters:
//username, password, salt, isLockedOut, failedPasswordAttempt

db.users.insert({ username : "Chris.Tite",
password : "FslPhcKizgtFNt42HM+CpLSI3wk=",
salt : "V7BcIDv6T0M=", isLockedOut : false, failedPasswordAttempt : 0 })

//To check that the document was created run a find command on the
//users collection.
//Note that the find command will list all documents created in the
//users collection

db.users.find()

//inserts a second entity into the database
//Notice that the structure for this insert has an extra parameter
//called "pin".
//MongoDB has no problem inserting this slightly different document
//structure.

db.users.insert({ username : "Sally.Burnett",
 password : "R29evUC0dGwkLzCd5rT91b8n+xU=",
 pin : "a+ZRoz4kD+4jRx8MG8QcMQv2hIA=", salt : "VlMIEUalxZU==",
 isLockedOut : false, failedPasswordAttempt : 0 })

//View both new documents by doing another find

db.users.find()

//To find an entry in the users collection simply add the parameter
//you wish to search by.
//Note the json structure

db.users.find({ username : "Chris.Tite" })

//To find a single entry use the findOne command

db.users.findOne({ isLockedOut : false })

//To update a document use the update command.
//In the case below we are updating all entities in the users
//collection setting the isActive attribute to true
//Notice the empty braces {} as the first parameter in the update
//statement)
//The second parameter is the value of the new or existing parameter
//The third parameter (optional) tells Mongo if it needs to upsert
//i.e. create a new document when no documenst meet the criteria
//The forth parameter (optional) tells Mongo that it should update
//multiple documents

db.users.update({}, {$set : {isActive : true}}, false, true)

//Here is another example of an update statement this time using a
//save command.
//This command updates all entities in the users collection setting
//the isApproved attribute to true

db.users.save({}, {$set : {isApproved : true}}, false, true)

//If you are interested to know what the save command does simply run
//the command with out the brackets
//You will notice the command prompt shows the javascript for the save
//command, it uses an update
// or insert depending on whether or not it finds a record.
//It is always handy to remember that running a command without the
//brackets shows the actual javascript for the command.

db.users.save

//To remove a document use the remove command.
//The code below removes the entity in the users collection with the
//username attribute matching "Chris.Tite"

db.users.remove({username : "Chris.Tite"})

Congratulations you have successfully created a new database which contains a users collection.
You have also successfully inserted two users into the users collection, done a few finds, updated the user documents and removed a user document from the users collection.

Proceed to demo two to walk through working with Mongo indexes.

Session Resources – NoSQL Explained – Running an MongoDB instance

Before working on any of the demos which were prepared for the NoSQL Explained session it is important to have in instance of MongoDB running.

If you are familiar with MongoDB and/or already have an instance running on your machine then skip this post and head straight to the demo posts if not stay close and we’ll cover setting up an instance of MongoDB in a few easy steps.

Getting MongoDB running on your machine

Step 1: Download a version of MongoDB form the MongoDB website http://www.mongodb.org/downloads.

Note: Version 2.0.9 was used for the demos, but since the demos cover very rudimentary functionality a newer version of MongoDB can be downloaded.

Step 2: Once the MongoDB has been downloaded, unzip the zip file to a working folder and find the bin folder, this folder contains all the exe files required to run MongoDB.

Step 3: On your C drive create a folder called “data_mongo” and copy the bin folder from step 2 into this folder.

Step 4: Run the following command to start MongoDB.

c:\data_mongo\bin\mongod --dbpath c:/data_mongo/db

Looking at the command above the following can be noted:

  • The executable mongod.exe is the server executable
  • The parameter –dbpath defines the path where the databases files reside. Note if the dbpath parameter is not supplied MongoBD will default to the path “c:\data\db”
  • The parameter –port can also be included if you with to have MongoDB run off a different port. By default MongoDB will run off port 27017 if no port parameter has been inlcuded

You now have an instance of MongoDB running on your machine. The instance is listening on port 27017 and will write data to the path “c:\data_mongo\db”.

Proceed to the first demo to start creating a database and inserting data.