Skip to content

Commit c813f7d

Browse files
Merge pull request #218 from JocaPC/master
Updated Azure funciton examples
2 parents 8d42e22 + 323c42b commit c813f7d

File tree

8 files changed

+132
-7
lines changed

8 files changed

+132
-7
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"frameworks": {
3-
"net46":{
3+
"net46": {
44
"dependencies": {
55
"Antlr4.Runtime": "4.5.3",
6-
"Sql-Server-Rest-Api": "0.2.6"
6+
"Sql-Server-Rest-Api": "0.3.3"
77
}
88
}
9-
}
9+
}
1010
}

samples/features/json/azure-function-odata/azure-function/run.csx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceW
1010
try{
1111
string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString;
1212
var sqlQuery = new QueryPipe(ConnectionString);
13-
var tableSpec = new SqlServerRestApi.SQL.TableSpec("sys.objects", "object_id,name,type,schema_id,create_date");
13+
var tableSpec = new SqlServerRestApi.TableSpec("sys","objects", "object_id,name,type,schema_id,create_date");
1414
return await req.CreateODataResponse(tableSpec, sqlQuery);
1515

1616
} catch (Exception ex) {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"dependencies": {
3-
"Belgrade.Sql.Client": "0.6.5"
2+
"frameworks": {
3+
"net46": {
4+
"dependencies": {
5+
"Belgrade.Sql.Client": "0.7"
6+
}
7+
}
48
}
5-
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"frameworks": {
3+
"net46": {
4+
"dependencies": {
5+
"Belgrade.Sql.Client": "0.7"
6+
}
7+
}
8+
}
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Configuration;
2+
using Belgrade.SqlClient;
3+
using Belgrade.SqlClient.SqlDb;
4+
5+
public static async Task Run(Stream myBlob, string name, TraceWriter log)
6+
{
7+
log.Info($"C# Blob trigger function Processed blob\n Name:{name}");
8+
if(name.EndsWith(".dat")){
9+
string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString;
10+
log.Info($"Importing blob\n Name:{name}");
11+
string sql =
12+
@"BULK INSERT Product
13+
FROM '" + name + @"'
14+
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
15+
FORMATFILE='product.fmt',
16+
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
17+
TABLOCK); ";
18+
log.Info($"SQL query:{sql}");
19+
await (new Command(ConnectionString)).ExecuteNonQuery(sql);
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
12.0
2+
7
3+
1 SQLNCHAR 2 100 "" 1 Name SQL_Latin1_General_CP1_CI_AS
4+
2 SQLNCHAR 2 30 "" 2 Color SQL_Latin1_General_CP1_CI_AS
5+
3 SQLMONEY 0 8 "" 3 Price ""
6+
4 SQLNCHAR 2 10 "" 4 Size SQL_Latin1_General_CP1_CI_AS
7+
5 SQLINT 1 4 "" 5 Quantity ""
8+
6 SQLNCHAR 2 8000 "" 6 Data SQL_Latin1_General_CP1_CI_AS
9+
7 SQLNCHAR 2 8000 "" 7 Tags SQL_Latin1_General_CP1_CI_AS
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- 1. INSERT CSV file into Product table
2+
BULK INSERT Product
3+
FROM 'product.csv'
4+
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
5+
FORMAT='CSV', CODEPAGE = 65001, --UTF-8 encoding
6+
FIRSTROW=2,
7+
TABLOCK);
8+
9+
-- 2. INSERT file exported using bcp.exe into Product table
10+
BULK INSERT Product
11+
FROM 'product.dat'
12+
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
13+
FORMATFILE='product.fmt',
14+
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
15+
TABLOCK);
16+
17+
-- 3. Read rows from product.dat file using format file and insert it into Product table
18+
INSERT INTO Product WITH (TABLOCK) (Name, Color, Price, Size, Quantity, Data, Tags)
19+
SELECT Name, Color, Price, Size, Quantity, Data, Tags
20+
FROM OPENROWSET(BULK 'product.dat',
21+
DATA_SOURCE = 'MyAzureBlobStorage',
22+
FORMATFILE='product.fmt',
23+
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as products;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/********************************************************************************
2+
* Note: You can export file and create format file using bcp out command:
3+
*
4+
*>bcp "SELECT Name, Color, Price, Size, Quantity, Data, Tags FROM Product" queryout product.dat -d ProductCatalog -T
5+
*
6+
********************************************************************************/
7+
8+
/********************************************************************************
9+
* 1. SETUP *
10+
********************************************************************************/
11+
12+
13+
/********************************************************************************
14+
* 1.1. OPTIONAL CREDENTIAL SETUP *
15+
* (if data source is not public) *
16+
********************************************************************************/
17+
-- 1.1.1. (optional) Create master key that will encrypt credentials
18+
--
19+
-- Required only if you need to setup CREDENTIAL in 1.1.2.
20+
-- CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'some strong password';
21+
22+
-- 1.1.2. (optional) Create credential with Azure Blob SAS
23+
--
24+
-- CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
25+
-- WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
26+
-- 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';
27+
-- NOTE: DO NOT PUT FIRST CHARACTER '?'' IN SECRET!!!
28+
29+
30+
31+
/********************************************************************************
32+
* 1.2. REQUIRE DATA SOURCE SETUP *
33+
* (optionally add credential) *
34+
********************************************************************************/
35+
36+
-- Create external data source with with the roow URL of the Blob storage Account and associated credential (if it is not public).
37+
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
38+
WITH ( TYPE = BLOB_STORAGE,
39+
LOCATION = 'https://sqlchoice.blob.core.windows.net/sqlchoice/samples/load-from-azure-blob-storage',
40+
-- CREDENTIAL= MyAzureBlobStorageCredential --> CREDENTIAL is not required if a blob storage is public!
41+
);
42+
43+
/********************************************************************************
44+
* 1.3. CREATE DESTINATION TABLE (if not exists) *
45+
*********************************************************************************/
46+
47+
DROP TABLE IF EXISTS Product;
48+
GO
49+
50+
CREATE TABLE dbo.Product(
51+
Name nvarchar(50) NOT NULL,
52+
Color nvarchar(15) NULL,
53+
Price money NOT NULL,
54+
Size nvarchar(5) NULL,
55+
Quantity int NULL,
56+
Data nvarchar(4000) NULL,
57+
Tags nvarchar(4000) NULL
58+
)
59+
GO

0 commit comments

Comments
 (0)