Read JSON file (streaming, no download)
Gets JSON file content without downloading to disk (streaming). Supply DriveId and FileId (path must end with colon). For compressed files set FileCompressionType to GZip or Zip.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
SELECT * from get_json_file
WITH(
DriveId='b!0zqXLXXJh0uUMzl-JXAd9Ztngc-5utVDqRyD2lKpD2535-11HLQTR5z4hOzmA7Q0'
, FileId='root:/myfolder/dump.json:' --Path must end with colon
--, FileId='01N3NI7YRUO2UHV2TUMBGJ4H4QQMWCG6DA'
--to read compressed file use below way
--, FileId='root:/myfolder/dump.json.gz:' --Path must end with colon
--, FileCompressionType='GZip' --None, GZip, Zip
, Filter='$.store.books[*]' --or just blank (see help file for more filter examples)
)
--SELECT * FROM get_json_file WITH(FileId='01N3NI7YQJMKXUWUAQGJEJJJNSGVT7QSJ3')
--SELECT * FROM get_json_file WITH(FileId='root:/dump.json:') --Path must end with colon
--SELECT * FROM get_json_file WITH(FileId='root:/Documents/dump.json:') --Path must end with colon
--SELECT * FROM get_json_file WITH(FileId='root:/Documents/SubFolder/dump.json:') --Path must end with colon
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_ONEDRIVE_IN_GATEWAY], 'SELECT * from get_json_file
WITH(
DriveId=''b!0zqXLXXJh0uUMzl-JXAd9Ztngc-5utVDqRyD2lKpD2535-11HLQTR5z4hOzmA7Q0''
, FileId=''root:/myfolder/dump.json:'' --Path must end with colon
--, FileId=''01N3NI7YRUO2UHV2TUMBGJ4H4QQMWCG6DA''
--to read compressed file use below way
--, FileId=''root:/myfolder/dump.json.gz:'' --Path must end with colon
--, FileCompressionType=''GZip'' --None, GZip, Zip
, Filter=''$.store.books[*]'' --or just blank (see help file for more filter examples)
)
--SELECT * FROM get_json_file WITH(FileId=''01N3NI7YQJMKXUWUAQGJEJJJNSGVT7QSJ3'')
--SELECT * FROM get_json_file WITH(FileId=''root:/dump.json:'') --Path must end with colon
--SELECT * FROM get_json_file WITH(FileId=''root:/Documents/dump.json:'') --Path must end with colon
--SELECT * FROM get_json_file WITH(FileId=''root:/Documents/SubFolder/dump.json:'') --Path must end with colon')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_ONEDRIVE_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * from get_json_file
WITH(
DriveId=''b!0zqXLXXJh0uUMzl-JXAd9Ztngc-5utVDqRyD2lKpD2535-11HLQTR5z4hOzmA7Q0''
, FileId=''root:/myfolder/dump.json:'' --Path must end with colon
--, FileId=''01N3NI7YRUO2UHV2TUMBGJ4H4QQMWCG6DA''
--to read compressed file use below way
--, FileId=''root:/myfolder/dump.json.gz:'' --Path must end with colon
--, FileCompressionType=''GZip'' --None, GZip, Zip
, Filter=''$.store.books[*]'' --or just blank (see help file for more filter examples)
)
--SELECT * FROM get_json_file WITH(FileId=''01N3NI7YQJMKXUWUAQGJEJJJNSGVT7QSJ3'')
--SELECT * FROM get_json_file WITH(FileId=''root:/dump.json:'') --Path must end with colon
--SELECT * FROM get_json_file WITH(FileId=''root:/Documents/dump.json:'') --Path must end with colon
--SELECT * FROM get_json_file WITH(FileId=''root:/Documents/SubFolder/dump.json:'') --Path must end with colon'
EXEC (@MyQuery) AT [LS_TO_ONEDRIVE_IN_GATEWAY]