From 323c42bfd88daa897d37d34d1fc1824c406d9ae3 Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Fri, 21 Apr 2017 16:41:55 +0200 Subject: [PATCH] Updated Azure Function examples --- .../azure-function/project.json | 6 +- .../azure-function/run.csx | 2 +- .../azure-function/project.json | 10 +++- .../azure-function/project.json | 9 +++ .../azure-function/run.csx | 21 +++++++ .../blob-storage/data/product.fmt | 9 +++ .../sql-scripts/demo.sql | 23 ++++++++ .../sql-scripts/setup.sql | 59 +++++++++++++++++++ 8 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/project.json create mode 100644 samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/run.csx create mode 100644 samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/blob-storage/data/product.fmt create mode 100644 samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/demo.sql create mode 100644 samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/setup.sql diff --git a/samples/features/json/azure-function-odata/azure-function/project.json b/samples/features/json/azure-function-odata/azure-function/project.json index ec274ba09d..8b0a4811f2 100644 --- a/samples/features/json/azure-function-odata/azure-function/project.json +++ b/samples/features/json/azure-function-odata/azure-function/project.json @@ -1,10 +1,10 @@ { "frameworks": { - "net46":{ + "net46": { "dependencies": { "Antlr4.Runtime": "4.5.3", - "Sql-Server-Rest-Api": "0.2.6" + "Sql-Server-Rest-Api": "0.3.3" } } - } + } } \ No newline at end of file diff --git a/samples/features/json/azure-function-odata/azure-function/run.csx b/samples/features/json/azure-function-odata/azure-function/run.csx index 4e46a9b029..73ecddc3b7 100644 --- a/samples/features/json/azure-function-odata/azure-function/run.csx +++ b/samples/features/json/azure-function-odata/azure-function/run.csx @@ -10,7 +10,7 @@ public static async Task Run(HttpRequestMessage req, TraceW try{ string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString; var sqlQuery = new QueryPipe(ConnectionString); - var tableSpec = new SqlServerRestApi.SQL.TableSpec("sys.objects", "object_id,name,type,schema_id,create_date"); + var tableSpec = new SqlServerRestApi.TableSpec("sys","objects", "object_id,name,type,schema_id,create_date"); return await req.CreateODataResponse(tableSpec, sqlQuery); } catch (Exception ex) { diff --git a/samples/features/json/azure-function-rest-api/azure-function/project.json b/samples/features/json/azure-function-rest-api/azure-function/project.json index b350e5c54e..34a3276381 100644 --- a/samples/features/json/azure-function-rest-api/azure-function/project.json +++ b/samples/features/json/azure-function-rest-api/azure-function/project.json @@ -1,5 +1,9 @@ { - "dependencies": { - "Belgrade.Sql.Client": "0.6.5" + "frameworks": { + "net46": { + "dependencies": { + "Belgrade.Sql.Client": "0.7" + } + } } -} +} \ No newline at end of file diff --git a/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/project.json b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/project.json new file mode 100644 index 0000000000..34a3276381 --- /dev/null +++ b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/project.json @@ -0,0 +1,9 @@ +{ + "frameworks": { + "net46": { + "dependencies": { + "Belgrade.Sql.Client": "0.7" + } + } + } +} \ No newline at end of file diff --git a/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/run.csx b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/run.csx new file mode 100644 index 0000000000..482dbc886d --- /dev/null +++ b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/azure-function/run.csx @@ -0,0 +1,21 @@ +using System.Configuration; +using Belgrade.SqlClient; +using Belgrade.SqlClient.SqlDb; + +public static async Task Run(Stream myBlob, string name, TraceWriter log) +{ + log.Info($"C# Blob trigger function Processed blob\n Name:{name}"); + if(name.EndsWith(".dat")){ + string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString; + log.Info($"Importing blob\n Name:{name}"); + string sql = +@"BULK INSERT Product +FROM '" + name + @"' +WITH ( DATA_SOURCE = 'MyAzureBlobStorage', + FORMATFILE='product.fmt', + FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage', + TABLOCK); "; + log.Info($"SQL query:{sql}"); + await (new Command(ConnectionString)).ExecuteNonQuery(sql); + } +} \ No newline at end of file diff --git a/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/blob-storage/data/product.fmt b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/blob-storage/data/product.fmt new file mode 100644 index 0000000000..440b2aba7a --- /dev/null +++ b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/blob-storage/data/product.fmt @@ -0,0 +1,9 @@ +12.0 +7 +1 SQLNCHAR 2 100 "" 1 Name SQL_Latin1_General_CP1_CI_AS +2 SQLNCHAR 2 30 "" 2 Color SQL_Latin1_General_CP1_CI_AS +3 SQLMONEY 0 8 "" 3 Price "" +4 SQLNCHAR 2 10 "" 4 Size SQL_Latin1_General_CP1_CI_AS +5 SQLINT 1 4 "" 5 Quantity "" +6 SQLNCHAR 2 8000 "" 6 Data SQL_Latin1_General_CP1_CI_AS +7 SQLNCHAR 2 8000 "" 7 Tags SQL_Latin1_General_CP1_CI_AS diff --git a/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/demo.sql b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/demo.sql new file mode 100644 index 0000000000..45ef4de0dd --- /dev/null +++ b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/demo.sql @@ -0,0 +1,23 @@ +-- 1. INSERT CSV file into Product table +BULK INSERT Product +FROM 'product.csv' +WITH ( DATA_SOURCE = 'MyAzureBlobStorage', + FORMAT='CSV', CODEPAGE = 65001, --UTF-8 encoding + FIRSTROW=2, + TABLOCK); + +-- 2. INSERT file exported using bcp.exe into Product table +BULK INSERT Product +FROM 'product.dat' +WITH ( DATA_SOURCE = 'MyAzureBlobStorage', + FORMATFILE='product.fmt', + FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage', + TABLOCK); + +-- 3. Read rows from product.dat file using format file and insert it into Product table +INSERT INTO Product WITH (TABLOCK) (Name, Color, Price, Size, Quantity, Data, Tags) +SELECT Name, Color, Price, Size, Quantity, Data, Tags +FROM OPENROWSET(BULK 'product.dat', + DATA_SOURCE = 'MyAzureBlobStorage', + FORMATFILE='product.fmt', + FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as products; \ No newline at end of file diff --git a/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/setup.sql b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/setup.sql new file mode 100644 index 0000000000..e03d672a00 --- /dev/null +++ b/samples/features/sql-bulk-load/load-from-azure-blob-storage-with-azure-function-blob-trigger/sql-scripts/setup.sql @@ -0,0 +1,59 @@ +/******************************************************************************** +* Note: You can export file and create format file using bcp out command: +* +*>bcp "SELECT Name, Color, Price, Size, Quantity, Data, Tags FROM Product" queryout product.dat -d ProductCatalog -T +* +********************************************************************************/ + +/******************************************************************************** +* 1. SETUP * +********************************************************************************/ + + +/******************************************************************************** +* 1.1. OPTIONAL CREDENTIAL SETUP * +* (if data source is not public) * +********************************************************************************/ +-- 1.1.1. (optional) Create master key that will encrypt credentials +-- +-- Required only if you need to setup CREDENTIAL in 1.1.2. +-- CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'some strong password'; + +-- 1.1.2. (optional) Create credential with Azure Blob SAS +-- +-- CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential +-- WITH IDENTITY = 'SHARED ACCESS SIGNATURE', +-- SECRET = 'sv=2015-12-11&ss=b&srt=sco&sp=rwac&se=2017-02-01T00:55:34Z&st=2016-12-29T16:55:34Z&spr=https&sig=copyFromAzurePortal'; +-- NOTE: DO NOT PUT FIRST CHARACTER '?'' IN SECRET!!! + + + +/******************************************************************************** +* 1.2. REQUIRE DATA SOURCE SETUP * +* (optionally add credential) * +********************************************************************************/ + +-- Create external data source with with the roow URL of the Blob storage Account and associated credential (if it is not public). +CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage +WITH ( TYPE = BLOB_STORAGE, + LOCATION = 'https://sqlchoice.blob.core.windows.net/sqlchoice/samples/load-from-azure-blob-storage', +-- CREDENTIAL= MyAzureBlobStorageCredential --> CREDENTIAL is not required if a blob storage is public! +); + +/******************************************************************************** +* 1.3. CREATE DESTINATION TABLE (if not exists) * +*********************************************************************************/ + +DROP TABLE IF EXISTS Product; +GO + +CREATE TABLE dbo.Product( + Name nvarchar(50) NOT NULL, + Color nvarchar(15) NULL, + Price money NOT NULL, + Size nvarchar(5) NULL, + Quantity int NULL, + Data nvarchar(4000) NULL, + Tags nvarchar(4000) NULL +) +GO