Azure Functions : Extending the “quick demo” from earlier this week

Introduction

Last week we did a quick demo on Azure Functions in combination with the event hub. Today I wanted to explore what would happen if I wanted to use “custom” libraries. Could I install libraries on functions? Let’s try it out shall we!

 

Install NPM Module in 

First thing we’ll do is install the extra npm module we need. We can do this via the “kudu“-console ;

2016-12-06-17_13_06-diagnostic-console

Just like we would anywhere else…

 

Adding an extra output

As I want to output the csv files to another container, I’ll be adding an additional output ;

2016-12-06-17_11_04-function-app-microsoft-azure

Same like before…

 

The Function!

Now let’s extend the function…

2016-12-06-17_10_52-function-app-microsoft-azure

module.exports = function (context, myEventHubTrigger) {
    context.log('JavaScript eventhub trigger function processed work item', myEventHubTrigger);
    context.bindings.outputBlob = myEventHubTrigger;

    var json2csv = require('json2csv');
    var fields = ['Temperature', 'Pressure'];
    context.bindings.outputBlobby = json2csv({ data: myEventHubTrigger, fields: fields });

    context.done();
};

We’ve added the necessary code to convert the json 2 csv and send it to our new output.

 

Test driving it

So we have our little test script…

2016-12-06-17_09_38-kvaessw10kavs_-_xca_functions

Which I’ll be running every 10 seconds ; 2016-12-06-17_10_05-program-manager

And great, that’s doing its thing…2016-12-06-17_10_21-kvaessw10kavs_-_xca_functions

The function receives & processes the messages ;
2016-12-06-17_10_45-function-app-microsoft-azure
Where the new output receives files.
2016-12-06-17_11_40-outcontainernmp-microsoft-azure

Which contains csv files!
2016-12-06-17_12_18-c__users_kavs_downloads_55c081e6-7f8c-4880-885c-4c78b0cdede3-notepad

Based upon the json from the other container.

2016-12-06-17_12_12-c__users_kavs_downloads_3ec34566-877f-4b18-9066-8f9f25992984-notepad

 

So it works! 😀

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