Do you have a Custom REST API that you want to query from SQL Server? Do you want to join the data from REST API with Native SQL Server tables? With the help of Progress DataDirect OpenAccess SDK, you can create a custom ODBC driver for your REST API, and use it with Linked Server in SQL Server. This tutorial will help you get started with creating your own Custom ODBC Driver for your REST API.
jar xvf oa_rest_ip_generator_8_X_X.jar
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
tables
>
<
table
name
=
"TimeSeries"
>
<
column
name
=
"Information"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"Function"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"Symbol"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"LastRefreshed"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"Interval"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"OutputSize"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"TimeZone"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"Timestamp_Recorded"
dataType
=
"12"
userData
=
""
/>
<
column
name
=
"open"
dataType
=
"8"
userData
=
""
/>
<
column
name
=
"high"
dataType
=
"8"
userData
=
""
/>
<
column
name
=
"low"
dataType
=
"8"
userData
=
""
/>
<
column
name
=
"close"
dataType
=
"8"
userData
=
""
/>
<
column
name
=
"volume"
dataType
=
"2"
userData
=
""
/>
<
stat
nonUnique
=
"0"
indexName
=
"IDXTMESTMPREC"
indexType
=
"1"
seqInIndex
=
"1"
columnName
=
"Timestamp_Recorded"
cardinality
=
"-1"
pages
=
"-1"
filterConditions
=
""
/>
<
pkfk
pkColumnName
=
"Timestamp_Recorded"
keySeq
=
"1"
updateRule
=
"-1"
deleteRule
=
"-1"
pkName
=
"PKTMESTMPREC"
/>
</
table
>
</
tables
>
DATABASE_NAME=Stocks
CATALOG_NAME=AlphaVantage
SCHEMA_NAME=Stocks
DATABASE_VERSION=1.0
SCHEMA_LOCATION=schema
SCHEMA_TYPE=XML
PATHTOWRITE=src
BASEPACKAGE=com.ddtek
DATASOURCE=Stocks
BASE_URL=https://www.alphavantage.co/query
java -jar oarestipgen.jar input.props
C:\Program Files\Progress\DataDirect\oaserver8X\ip\oajava\oasql.jar;
\path\to\Project\out\production\<project>\;
\path\to\Project\lib\cxf-api-2.7.8.jar;
\path\to\Project\lib\cxf-rt-bindings-xml-2.7.8.jar;
\path\to\Project\lib\cxf-rt-core-2.7.8.jar;
\path\to\Project\lib\cxf-rt-frontend-jaxrs-2.7.8.jar;
\path\to\Project\lib\cxf-rt-transports-http-2.7.8.jar;
\path\to\Project\lib\jackson-core-asl-1.8.10.jar;
\path\to\Project\lib\jackson-mapper-asl-1.8.10.jar;
\path\to\Project\lib\javax.ws.rs-api-2.0-m10.jar;
\path\to\Project\lib\wsdl4j-1.6.3.jar;
Note: If you use eclipse, replace the 2nd line in the path with \path\to\Project\bin
RESOURCE_PROPERTIES=\path\to\Project\Schema
This is the method where you observe the conditions for the SQL query that was issued to the driver, build a corresponding request URL for the Alpha Vantage API and send it to OpenAccess. The template code will handle making the request to server and providing the response to the parseJSONResponse method
In this method, you will be provided the response to the request that you built in the buildRequest method. All you must do is parse the response, send it back to OpenAccess in a ArrayList Object filled with HashMap object.
IMPORTANT: Once you have built the code, you would have to restart the OpenAccess service that you have created above to see the changes.
-Xrunjdwp:transport=dt_socket,address=9015,server=y,suspend=n -Xdebug -Xrs
SELECT
*
FROM
OPENQUERY(OPENACESSSTOCKS,
'select INFORMATION,"FUNCTION",SYMBOL,LASTREFRESHED,"INTERVAL",OUTPUTSIZE,TIMEZONE,TIMESTAMP_RECORDED,OPEN,HIGH,LOW,CLOSE,CAST(VOLUME as decimal(38,6)) as volume FROM TIMESERIES where "function"='
'TIME_SERIES_MONTHLY'
' AND "SYMBOl"='
'PRGS'
''
)
SELECT
*
FROM
OPENQUERY(OPENACESSSTOCKS,
'select INFORMATION,"FUNCTION",SYMBOL,LASTREFRESHED,"INTERVAL",OUTPUTSIZE,TIMEZONE,TIMESTAMP_RECORDED,OPEN,HIGH,LOW,CLOSE,CAST(VOLUME as decimal(38,6)) as volume FROM TIMESERIES where "function"='
'TIME_SERIES_DAILY'
' AND "SYMBOl"='
'PRGS'
' AND "interval"='
'5min'
''
)
SELECT
*
FROM
OPENQUERY(OPENACESSSTOCKS,
'select INFORMATION,"FUNCTION",SYMBOL,LASTREFRESHED,"INTERVAL",OUTPUTSIZE,TIMEZONE,TIMESTAMP_RECORDED,OPEN,HIGH,LOW,CLOSE,CAST(VOLUME as decimal(38,6)) as volume FROM TIMESERIES where "function"='
'TIME_SERIES_INTRADAY'
' AND "SYMBOl"='
'PRGS'
' AND "interval"='
'5min'
''
)
SELECT
*
FROM
OPENQUERY(OPENACESSSTOCKS,
'select INFORMATION,"FUNCTION",SYMBOL,LASTREFRESHED,"INTERVAL",OUTPUTSIZE,TIMEZONE,TIMESTAMP_RECORDED,OPEN,HIGH,LOW,CLOSE,CAST(VOLUME as decimal(38,6)) as volume FROM TIMESERIES where "function"='
'TIME_SERIES_MONTHLY'
' AND "SYMBOl"='
'PRGS'
''
) API
INNER
JOIN
Chinook2.dbo.Company C
ON
API.SYMBOL = C.TICKER
USE [Chinook2]
GO
/****** Object: StoredProcedure [dbo].[sp_stocksapi] Script
Date
: 3/29/2018 10:37:48 AM ******/
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
CREATE
PROCEDURE
[dbo].[sp_stocksapi]
(
@pfunction
varchar
(100),
@psymbol
varchar
(5),
@pinterval
varchar
(10))
AS
BEGIN
DECLARE
@TSQL
varchar
(8000)
IF @pinterval
is
null
BEGIN
SELECT
@TSQL=
'SELECT * FROM OPENQUERY(OPENACESSSTOCKS,'
'select INFORMATION,"FUNCTION",SYMBOL,LASTREFRESHED,"INTERVAL",OUTPUTSIZE,TIMEZONE,TIMESTAMP_RECORDED,OPEN,HIGH,LOW,CLOSE,CAST(VOLUME as decimal(38,6)) as volume FROM TIMESERIES WHERE "function" = '
''
''
+ @pfunction +
''
''
' and symbol = '
''
''
+ @psymbol +
''
''
''
')'
END
IF @pinterval
is
not
null
BEGIN
SELECT
@TSQL=
'SELECT * FROM OPENQUERY(OPENACESSSTOCKS,'
'select INFORMATION,"FUNCTION",SYMBOL,LASTREFRESHED,"INTERVAL",OUTPUTSIZE,TIMEZONE,TIMESTAMP_RECORDED,OPEN,HIGH,LOW,CLOSE,CAST(VOLUME as decimal(38,6)) as volume FROM TIMESERIES WHERE "function" = '
''
''
+ @pfunction +
''
''
' and symbol = '
''
''
+ @psymbol +
''
''
' and "interval"='
''
''
+@pinterval+
''
''
''
')'
END
EXEC
(@TSQL);
END
GO
EXEC sp_stocksapi 'TIME_SERIES_DAILY', 'PRGS', '5min'
EXEC sp_stocksapi 'TIME_SERIES_INTRADAY', 'PRGS', '1min'
EXEC sp_stocksapi 'TIME_SERIES_MONTHLY', 'PRGS', null
EXEC sp_stocksapi 'TIME_SERIES_WEEKLY', 'PRGS', null
We hope this tutorial helped you to real-time join your Native SQL Server table with the data from REST API through a Linked Server using the custom ODBC driver built using Progress DataDirect OpenAccess SDK for your REST API. If you have any questions/issues, feel free to reach out to us, we will be happy to help you during your evaluation.