Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
Copyright Central Park Data Systems, Inc. 1
Looking Inside the Developer’s Toolkit:REST Web Services for Everyday RPG and SQL Consumption
Charles Guarino
Charles GuarinoTwitter @charlieguarino
Central Park Data Systems, Inc.
REST SERVICES
About The Speaker
With an IT career spanning over 30 years, Charles Guarino
has been a consultant for most of them. Since 1995 he has
been founder and President of Central Park Data Systems,
Inc., a New York area based IBM midrange consulting
company. In addition to being a professional speaker, he is a
frequent contributor of technical and strategic articles and
webcasts for the IT community. He is a proud member of
COMMON’s Speaker Excellence Hall of Fame and also Long
Island Software and Technology Network’s Twenty Top
Techies of 2009. Charles currently serves as a member of
COMMON’s Strategic Education Team (SET) and is also
Immediate Past President and monthly Q&A host of LISUG,
a Long Island IBM i User’s Group www.lisug.org.
Charles can be reached at [email protected].
LinkedIn - http://www.linkedin.com/in/guarinocharles
Copyright Central Park Data Systems, Inc. 2
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Local System
Program “A” Program “B”
How Do They Work?
Consumer Provider
PARAMETERS
Copyright Central Park Data Systems, Inc. 3
The Cloud
Power System
running IBM I
Program “A”
Web Services Server
on some other
system
Program “B”
How Do They Work?
Consumer Provider
More Web Services Fun Facts
• A STATELESS connection
• Can be sent in the clear or securely
• Can be public or private between companies or individuals
• Hardware, programming language and platform agnostic
• Can save you lots of $ - pre-written and tested and documented services
• Can be FREE, Restricted Access, Pay-For-Each-Use or Subscription Based
• Extends your applications in ways you may have never known possible, providing real time information.
• Can be many and simultaneous points of failure
• See the end of this presentation for useful links!!!!
Copyright Central Park Data Systems, Inc. 4
Methods of Transport
REST(Representational State Transfer)
URL http://.....
Web Services for the masses
Copyright Central Park Data Systems, Inc. 5
How do they track your usage?
But how do they make money?
Copyright Central Park Data Systems, Inc. 6
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Data Format XML
XML – IBM i has native parsing tools
Extensible Markup Language
Copyright Central Park Data Systems, Inc. 7
Data Format JSON
JSON
Java Script Object Notation
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Copyright Central Park Data Systems, Inc. 8
http://wiki.cdyne.com/wiki/index.php?title=CDYNE_Weather
http://wiki.cdyne.com/wiki/index.php?title=CDYNE_Weather
Copyright Central Park Data Systems, Inc. 9
http://wiki.cdyne.com/wiki/index.php?title=CDYNE_Weather
http://wsf.cdyne.com/WeatherWS/Weather.asmx?op=GetCityForecastByZIP
Copyright Central Park Data Systems, Inc. 10
http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityForecastByZIP?Zip=10038
Header Data
First Day’s Forecast
Second Day’s Forecast
First screen prompts for zip code:
Copyright Central Park Data Systems, Inc. 11
Subfile displayed with fully parsed data for zipcode 10038:
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Copyright Central Park Data Systems, Inc. 12
Definitions For Using SQL and XML
SYSTOOLS
CCSID 37
CCSID 65535
CLOB versus BLOB
XPATH
A Review Once Again of the XML Data
Header Data
First Day’s Forecast
Second Day’s Forecast
Starting point for SQL statement
“ForecastReturn/ForecastResult/Forecast”
Copyright Central Park Data Systems, Inc. 13
http://www.ibm.com/developerworks/data/library/techarticle/dm-1105httprestdb2/
HTTPGETCLOB !!!
IBM i Navigator Using 7.1 Technology Refresh 6
Copyright Central Park Data Systems, Inc. 14
The NEW Power of the SQL Select Statement
Select *
from XMLTABLE(xmlnamespaces(default
'http://ws.cdyne.com/WeatherWS/'),
'ForecastReturn/ForecastResult/Forecast'
passing XMLPARSE(document(
systools.httpGetClob('http://wsf.cdyne.com/WeatherWS/Weather.asmx/Ge
tCityForecastByZIP?Zip=10038','')))
Columns
State VarChar(3) path '../../State',
City VarChar(30) path '../../City',
Station VarChar(30) path '../../WeatherStationCity',
DateTime VarChar(50) path 'Date',
Description VarChar(30) path 'Desciption',
MorningLow VarChar(3) path 'Temperatures/MorningLow',
DayTimeHigh VarChar(3) path 'Temperatures/DaytimeHigh',
Nighttime VarChar(3)
path 'ProbabilityOfPrecipiation/Nighttime',
Daytime VarChar(3)
path 'ProbabilityOfPrecipiation/Daytime')x;
Prompt for zip code
Concatenate zip code and execute HTTPGETCLOB,
place XML into variable “BigVar”
Declare cursor, read 7 rows into data structure
Load parsed data into subfile and display
WEATHRRSQL Basic Program Flow
Copyright Central Park Data Systems, Inc. 15
Program WEATHRRSQL
Stepping through the code, zip code 10038 was keyed.
Copyright Central Park Data Systems, Inc. 16
Stepping through subprocedure ConsumeWebService
Data structure XMLINDATA after SQL Fetch
Copyright Central Park Data Systems, Inc. 17
Loading the subfile:
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Copyright Central Park Data Systems, Inc. 18
Prompt for zip code
Concatenate zip code and execute HTTPGETCLOB,
place XML into variable “BigVar”
Parse XML into data structures using RPG’s XML-INTO
Load parsed data into subfile and display
WEATHRINTO Basic Program Flow
Program WEATHRINTO
Copyright Central Park Data Systems, Inc. 19
1
2
3
4
Header data – A Closer Look
1
2
3
Detail Data – A Closer Look
If you want to capture the data, the names must match – misspellings and all!
Copyright Central Park Data Systems, Inc. 20
Immediately after HTTPGETCLOB, loaded BigVar
xml-into HeaderData:
Copyright Central Park Data Systems, Inc. 21
Loaded data structure HeaderData:
Loaded data structure WeatherData:
Copyright Central Park Data Systems, Inc. 22
Loading the subfile:
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Copyright Central Park Data Systems, Inc. 23
www.openweathermap.org
Sample API call using London, UK
http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295
cca9337e1e96b00e0
Copyright Central Park Data Systems, Inc. 24
http://www.scottklement.com/yajl
http://www.mcpressonline.com/programming/techtip-json-and-xml-conversion-in-db2-for-i.html
Copyright Central Park Data Systems, Inc. 25
API key stored in data area WEATHERMAP
This is invalid XML data, needs to be fixed
<1aaa>
</1aaa>
Copyright Central Park Data Systems, Inc. 26
Prepare URL from JSON web service,
include app key from data area
Execute HTTPGETCLOB, place JSON into “Jsonstring”
place XML into variable “BigVar”
Call program to convert JSON to XML
Fix any bad XML tags
WEATHRJSON Basic Program Flow
Invoke XML SAX parser and
write selected data elements to table WEBSVCDATA
Program WEATHRJSON
Copyright Central Park Data Systems, Inc. 27
Program call with parameter “Costa Mesa, CA”
URL is loaded, appended with API key
Copyright Central Park Data Systems, Inc. 28
Variable JSONSTRING
Variable JSONSTRING converted to XMLSTRING
Copyright Central Park Data Systems, Inc. 29
Parsed data in table WEBSVCDATA
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Copyright Central Park Data Systems, Inc. 30
Prepare URL from JSON web service,
include app key from data area
Execute HTTPGETCLOB, place JSON into “Jsonstring”
place XML into variable “BigVar”
Call program to convert JSON to XML
Fix any bad XML tags
WTHRJSONXT Basic Program Flow
Invoke XML SAX parser and INSERT rows
from selected data elements to table WEATHERDATA
Program WTHRJSONXT
Copyright Central Park Data Systems, Inc. 31
JSONSTRING is loaded from web service
XMLSTRING is loaded from web service
Copyright Central Park Data Systems, Inc. 32
At the beginning of subprocedure processXML
Insert row into table WeatherData
Copyright Central Park Data Systems, Inc. 33
Table WeatherData (Weath00001) after insert
What We’ll Cover …
• Web services 101
• XML versus JSON
• Real life web services
• XML using native SQL
• XML using native RPG
• JSON to XML using native RPG
• JSON to XML using native SQL
• Wrap-up
Copyright Central Park Data Systems, Inc. 34
Looking Inside the Developer’s Toolkit:REST Web Services for Everyday RPG and SQL Consumption
Charles Guarino
Charles GuarinoTwitter @charlieguarino
REST SERVICES
THANK YOU !!!
Definitions
• XML (eXtensible Markup Language)• Unlike HTML which has pre-defined tags, the tags can have any
value you wish. This is what makes it extensible.
• Your document must still be well-formed to be parsed, meaning each start tag must have a corresponding end tag.
• The actual data is stored between the tags.
• Parsing• The “shredding” or extraction of the XML data from its tags.
• SOAP • Simple Object Access Protocol
• Message-based, you create a message similar to an email using a pre-defined language, Web Services Definition Language, or WSDL (“wiz-dul”).
Copyright Central Park Data Systems, Inc. 35
Definitions (cont.)
• REST (Representation State Transfer)• HTTP based, no need to create a message
• Web service can be consumed using a browser
• U.R.I. (Universal Resource Identifier)• More broadly based than a URL, which refers to a website, is
used to identify any object in the cloud
Helpful resources• The basics of RESTful web services
http://www.ibm.com/developerworks/webservices/librar
y/ws-restful
• XML Name Spaces
http://www.w3schools.com/xml/xml_namespaces.asp
• SQL Messages and Codes When Using SQL With DB2 for i
http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.j
sp?topic=%2Frzala%2Frzalakickoff.htm
Copyright Central Park Data Systems, Inc. 36
Helpful resources
• A good primer on understanding CCSID
http://www.itjungle.com/mpo/mpo091103-
story03.html
• A beginner’s definition of BLOB
http://www.techterms.com/definition/blob
• A beginner’s definition of CLOB
http://www.techterms.com/definition/clob
Helpful resources
• What is SYSTOOLS?http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frzajq%2Frzajqsystools.htm
• IBM developerWorks website discussing new SQL HTTP functions, including HTTPGETCLOB https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/New%20HTTP%20functions%20added%20to%20SYSTOOLS
• A beginner’s tutorial on XPATH
http://www.w3schools.com/xsl/xpath_intro.asp
Copyright Central Park Data Systems, Inc. 37
Additional Session Information
• To Run SQL Scripts in IBM i Navigator
Open your connection in Navigator
Open “Databases”
When your database name appears, right click on it.
In the menu, click on “Run SQL Scripts”
• When you are in the SQL Scripts window, click on “Connection”
and select “Temporary JDBC Settings”
In the “Translation” tab, make sure “Translate CCSID 65535” is
CHECKED.
Software Disclaimer
DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY
The software is supplied “as is” and all use is at your own
risk.
Central Park Data Systems Inc. and/or Charles Guarino
disclaims all warranties of any kind, either express or
implied, as to the software, including, but not limited to,
implied warranties of fitness for a particular purpose,
merchantability or non‐infringement of proprietary rights.
No documentation furnished is intended to express or imply
any warranty that the operation of the software will be
uninterrupted, timely, or error‐free.