Showing posts with label visual studio 2013. Show all posts
Showing posts with label visual studio 2013. Show all posts

Thursday, October 10, 2013

Setting up Neo4j for Geotopia's graph DB

Since my datamodel probably is going to be very chaotic (lots of relationships between entities) I decided to pick a graph database. Neo4j is my choice and there is an excellent article out there that helps you setup an environment and host it on Azure! Social networks are graphs after all. Querying a social graph can lead to massive and complex joins when you use a SQL RDBMS.

http://blog.jongallant.com/2013/03/neo4j-azure-vs2012.html

After following the instructions on the blog of Jon, I have a cloud service running neo4j and I am ready to store some nodes over there. I have neo4j server running locally and in my Azure space. Running it locally caused some error due to "too long filenames". You can tackle this by changing the output directory of the dev fabric.

To execute CRUD actions on my Neo4j graph db I use Neo4jClient from Readify.

Example: here is a simple representation of a Geotopic and a GeotopicUser.

    public class Geotopic
    {
        public string id { get; private set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string message { get; set; }

        public Geotopic(string latitude, string longitude, string message)
        {
            this.latitude = latitude;
            this.longitude = longitude;
            this.message = message;

            this.id = Guid.NewGuid().ToString();
        }
    }

    public class GeotopiaUser
    {
        public string id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public GeotopiaUser(string firstName, string lastName, string id)
        {
            this.FirstName = firstName;
            this.LastName = lastName;

            this.id = id;
        }
    }

These entities do have relationships. A user can post a geotopic. A user can follow another user. A user can like a topic. A user can recommend a topic.

First, after a user is logged in by using the Windows Azure Active Directory credentials, I make sure that the GeotopicUser exists in the neo4j graph db. The id of the user is the username that is registered in the actived directory. See the code snippet below on how users are created and how creating relationships between them is accomplished.

Using the Neo4j Client makes it quite easy to create a graph. In this example I create 2 users, one follows the other and a geotopic that is posted by the first user.

            GraphClient client = new GraphClient(new Uri("http://db/data"));
            client.Connect();

             // Create Indexes
            client.CreateIndex("GeotopiaUser", new IndexConfiguration() { Provider = IndexProvider.lucene, Type = IndexType.exact }, IndexFor.Node); // exact node index
            client.CreateIndex("Geotopic", new IndexConfiguration() { Provider = IndexProvider.lucene, Type = IndexType.exact }, IndexFor.Node); // exact node index

            // Create Entities
            // Movies
            GeotopiaUser user1 = new GeotopiaUser("Riccardo", "Becker", "riccardo@geotopia.onmicrosoft.com");

            var geoUser1 = client.Create(user1,
                new IRelationshipAllowingParticipantNode[0],
                new[]
                {
                    new IndexEntry("GeotopiaUser")
                    {
                        { "Id", user1.id.ToString() }
                    }
                });

            //new topic
            Geotopic topic1 = new Geotopic("0.0", "0.0", "First topic!");

            var firsttopic = client.Create(topic1,
                new IRelationshipAllowingParticipantNode[0],
                new[]
                {
                    new IndexEntry("Geotopic")
                    {
                         { "Id", topic1.id.ToString() }
                    }
                });

            client.CreateRelationship(firsttopic, new PostedBy(geoUser1));

            GeotopiaUser user2 = new GeotopiaUser("John", "Doe", "johndoe@geotopia.onmicrosoft.com");

            var geoUser2 = client.Create(user2,
                new IRelationshipAllowingParticipantNode[0],
                new[]
                {
                    new IndexEntry("GeotopiaUser")
                    {
                        { "Id", user2.id.ToString() }
                    }
                });

            //john follows me
            client.CreateRelationship(geoUser2, new FollowRelationship(geoUser1));

Executing this code created the following graph:


Node with id 49 is my first user (riccardo) and node 51 is the second user (John Doe). John follows me and I created one topic. This is setup by creating a "posted_by" relationship between node 50 and 49.

The following code uses the index "GeotopiaUser" and looks up every user in the graph.

            List> list = client.QueryIndex("GeotopiaUser", IndexFor.Node, "Id: *").ToList();

            foreach (Node user in list)
            {
                Console.WriteLine(user.Data.FirstName + " " + user.Data.LastName);

            }

The next post will describe how to post a Geotopic on the canvas of Geotopia and how this is added to the graph DB of Neo4j.

Happy coding!


Thursday, October 3, 2013

The Wireframe of Geotopia

Using the Visual Studio 2013 RC, I created a cloud project with an ASP.NET webrole. When you add a webrole to your cloud project, the screen below appears.



I want to create an MVC app and want to use Windows Azure Active Directory authentication. To enable this you need to create a new Directory in your Active Directory in the Windows Azure portal. When you create a new directory, this screen appears.


Add some users to your directory. In this case, I have "riccardo@geotopia.onmicrosoft.com" for now.
Go back to Visual Studio and select the Change Authencation button. Next, select the Organization Acounts and fill in your directory.


When you click Ok now you need to login with the credentials of one of your users you created in your directory. I log in with riccardo@geotopia.onmicrosoft.com. Next, select Create Project and your MVC5 app is ready to go. Before this application is able to run locally, in your development fabric, you need to change the return URL in the Windows Azure portal. Go to applications in the designated Directory on the Windows Azure portal. The newly created MVC project appears there, in my case it's Geotopia.WebRole. In this screen you see the app url which points to https://localhost:44305. This URL is NOT correct when you run the MVC5 app as a cloud project in the development fabric. Click the application in the portal and select Configure. Change the app URL and the return URL to the correct url when your app runs locally in development fabric. In my case: https://localhost:8080. When you run your app, you get a warning about a problem with the security certificate, but you can ignore this for now. After logging in succesfully, you will be redirected to the correct return URL (configured in the portal) and showing you that you are logged in. I also created a bing map which centers on the beautiful Isola di Dino in Italy with a bird's eye view.



In the next blog I will show how to create a Geotopic on the Bing Map and how to store it in Table Storage and how add your own fotographs to it to show your friends how beautiful the places are you visited. This creates an enriched view, additional to the bing map.