top of page
Writer's pictureNakkeeran Natarajan

Working With Azure Tables - Part Two


Introduction

Let us look at a basic sample which retrieves the Azure table data into the applications using Node JS.


In my previous article, you have seen the basic introduction to Azure Tables.

Start creating a node application by running npm init. Create the main file (JavaScript file) for writing the core functionality. In my case, index.js will be used.



The dependency files need to be imported. To access the Azure Table Storage data, Azure Storage functions and objects will be used. The npm package called azure-storage package is available for working with Azure Storage. This needs to be imported and installed using npm command.



The following code snippet shows the package.json file created for this project.

  1. {   

  2.  "name": "getazuretabledata",    

  3. "version": "1.0.0",    

  4. "description": "",    

  5. "main": "index.js",    

  6. "scripts": {      

  7. "test": "echo \"Error: no test specified\" && exit 1"    },   

  8.  "author": "nakkeeran",    

  9. "license": "ISC",    

  10. "dependencies": {      

  11. "azure-storage": "^2.5.0"    

  12. }  

  13. }  

From the Azure Table Storage, account name and access keys need to be noted and used in the node application for accessing the Azure Storage data. 

  • Navigate to https://portal.azure.com.

  • Go to the Azure Storage account created.

  • Navigate to Access keys under Settings option. Copy the storage account name and any one of the keys available.



Main Index.js File

Create a port and import necessary packages.

  1. var port = process.env.PORT || 8000;  

  2. var http = require('http');  

  3. var azure = require('azure-storage');  

Create the Server. In the Server function, the following steps are followed. 

  • Access the table service using azure.createTableService(accountName ,accessKey) method.

  1. var port = process.env.PORT || 8000;  

  2. var http = require('http');  

  3. var azure = require('azure-storage');  

  • Table query is built. In the query, a necessary condition should be built using the filters available. In this sample, we are going to see how we are filtering the Azure Table data using partition keys.

  1. var accountName = "accountname copied from azure portal";  

  2. var accessKey = "access key copied from azure portal";  

  • Then, when the table service initialized, the necessary query is made using queryEntities method. The necessary parameters are - Azure table name, query, token, and the call back method.

  1. var server = http.createServer(function(request, response) 

  2. {    

  3. / /response.end("Listening on port %s...", server.address().port);

  4. var tableService = azure.createTableService(accountName, accessKey); // explicit

  5. var TableQuery = azure.TableQuery;  

  6. var TableUtilities = azure.TableUtilities;  

  7. var queryCondition = TableQuery.stringFilter('PartitionKey', TableUtilities.QueryComparisons.EQUAL, "Grade 1");  

  8. var query = new azure.TableQuery().where(queryCondition);      

  9. tableService.queryEntities('azuretesttable', query, nullfunction(error, result, res) 

  10. {  

  11. if (!error) {              

  12. response.end(JSON.stringify(result));          }  

  13. else{              

  14. console.log(error);          

  15. }      

  16. });  

  17. });    

The following code snippet shows the entire main function logic.

  1. var port = process.env.PORT || 8000;  

  2. var http = require('http');  

  3. var azure = require('azure-storage');  

  4. var accountName = "accountname copied from azure portal";  

  5. var accessKey = "access key copied from azure portal";  

  6. var server = http.createServer(function(request, response) {    

  7. var tableService = azure.createTableService(accountName, accessKey);

  8. var TableQuery = azure.TableQuery;  

  9. var TableUtilities = azure.TableUtilities;  

  10. var queryCondition = TableQuery.stringFilter('PartitionKey', TableUtilities.QueryComparisons.EQUAL, "Grade 1");  

  11. var query = new azure.TableQuery().where(queryCondition);      

  12. tableService.queryEntities('azuretesttable', query, nullfunction(error, result, res) 

  13. {  

  14. if (!error) {              

  15. response.end(JSON.stringify(result));          

  16. }  

  17. else{              

  18. console.log(error);          

  19. }      

  20. });  

  21. });    

  22. server.listen(port);   

The following snapshot shows the results retrieved from the Azure Table.



Summary

In this article, you have seen the basic example of retrieving the Azure Storage Table data into Node.js application.

0 comments

Comments

Couldn’t Load Comments
It looks like there was a technical problem. Try reconnecting or refreshing the page.
bottom of page