Getting Started - .NET

This Quickstart Guide

What you'll learn: How to connect and stream live data from the DataSift platform to your machine.
Duration: 10 minutes

Table Of Contents

Before You Start

Before you get started make sure you've completed the Developer Quick Start guide. This will take you through:

  • Activating the Tumblr data source
  • Activating the Links augmentation

You'll need to have done these steps to continue.

Once complete, sign-in to DataSift and make a note of your username and API key on your dashboard. You'll need them in a minute.

dashboard-apidetails_2

Step 1: Install The Client Library

The .NET library is available as a NuGet package on here.

Create a new Command Line Application in Visual Studio 2012 or above.

Install the client library using the Package Manager Console:

Install-Package Datasift.net

Step 2: Create A DataSift Client

With the package installed, now you can write a script to access the API. Firstly we need to create a client object that will access the API for us.

Open Program.cs in your new application. Firstly include the following namespaces at the top of the file:

using DataSift;
using DataSift.Enum;
using DataSift.Streaming;

Next replace the code inside the Program class with the following:

// References we'll need to keep
private static DataSiftStream _stream;
private static string _hash;

static void Main(string[] args)
{
    // Create a new DataSift client
    var client = new DataSiftClient("DATASIFT_USERNAME", "DATASIFT_APIKEY");
}

Step 3: Compile A Filter

In order to stream data from the platform, you need to create a filter in CSDL. You compile this filter using the API and receive a hash that represents the filter.

Add the following code in your Main method:

// Compile filter looking for brand mentions
var csdl = @"interaction.content contains_any ""Calvin Klein, GQ, Adidas"" ";

var compiled = client.Compile(csdl);
_hash = compiled.Data.hash;

Here we're using the interaction.content property (or target) of the piece of content. This is the text of the Tumblr post in this case.

Step 4: Streaming Data

With your filter compiled you can now start streaming data. This sample code shows how to declare your event handlers and kick off the data stream.

Warning: When you are running a stream you are consuming your platform credit. Your free credit gives you plenty to play with, but always remember to stop your stream if you're not using to make the most of your credit.

Add the following to your Main method:

_stream = client.Connect();
_stream.OnConnect += stream_OnConnect;
_stream.OnMessage += stream_OnMessage;
_stream.OnDelete += stream_OnDelete;
_stream.OnDataSiftMessage += stream_OnDataSiftMessage;
_stream.OnClosed += stream_OnClosed;

// Wait for key press before ending example
Console.WriteLine("-- Press any key to exit --");
Console.ReadKey(true);

Then, add the following event handlers to your class:

static void stream_OnConnect()
{
    Console.WriteLine("Connected to DataSift.");

    // Subscribe to stream
    _stream.Subscribe(_hash);
}

static void stream_OnMessage(string hash, dynamic message)
{
    Console.WriteLine("INTERACTION: {0}", message.interaction.content);
}

static void stream_OnDelete(string hash, dynamic message)
{
    // You must delete the interaction to stay compliant
    Console.WriteLine("Deleted: {0}", message.interaction.id);
}

static void stream_OnDataSiftMessage(DataSift.Enum.DataSiftMessageStatus status, string message)
{
    switch (status)
    {
        case DataSiftMessageStatus.Warning:
            Console.WriteLine("WARNING: " + message);
            break;
        case DataSiftMessageStatus.Failure:
            Console.WriteLine("FAILURE: " + message);
            break;
        case DataSiftMessageStatus.Success:
            Console.WriteLine("SUCCESS: " + message);
            break;
    }
}

static void stream_OnClosed()
{
    Console.WriteLine("Connection has been closed.");
}

Step 5: Give It A Whirl

With your script now complete, you can run the example and see data pouring into your console.

Run your application in debug mode by pressing F5.

There's a complete version of the Program.cs file here.

Step 6: Classifying Data

To help you understand, integrate and act upon the data, DataSift allows you to add custom metadata through tags and scores. Read more about DataSift VEDO to find out more. As a quick example, you could use tags to identify where music is being shared from.

Try replacing the csdl variable in your Main method with the following:

var csdl = @"tag.brand ""Calvin Klein"" { interaction.content contains ""Calvin Klein"" }
            tag.brand ""GQ"" { interaction.content contains ""GQ"" }
            tag.brand ""Adidas"" { interaction.content contains ""Adidas"" }
            return
            {
                interaction.content contains_any ""Calvin Klein, GQ, Adidas""
            }";

Then within the stream_OnMessage handler, change the code to:

Console.WriteLine("{0}: {1}", message.interaction.tag_tree.brand[0], message.interaction.content);

Run the application again, and you can see the tags assigned to the data (delivered under the interaction.tag_tree property of each item).

Learn More

Congratulations, you can now use the DataSift API to stream live data. To learn more about the platform please take a look at the following resources: