Reading from Atom or RSS feeds
The Atom/RSS adapter reads an RSS or Atom feed. It tails this feed and parses results into a columned stream, using the xpath syntax. For more information on xpath, see http://www.w3schools.com/xpath/xpath_intro.asp.
As with other adapters, to implement the Atom/RSS adapter, you need to register the adapter, create a server object with connection details for the feed, and create a foreign stream that you can later query.
As with other adapters, the foreign data wrapper loads a Java class for the plugin, called class com.sqlstream.plugin.webfeed.WebFeedStreamControlPlugin. You will reference the wrapper when you create the server object below. For more information on foreign data wrappers, please see the topic CREATE FOREIGN DATA WRAPPER in the s-Server Streaming SQL Reference Guide. The following code creates a foreign data wrapper for the WebFeed adapter.
CREATE OR REPLACE FOREIGN DATA WRAPPER "WebFeed"
LIBRARY 'class com.sqlstream.plugin.webfeed.WebFeedStreamControlPlugin'
DESCRIPTION 'Plugin to read/tail web-based data feeds';
As with other adapters, the server object for a WebFeed adapter contains server connection information. For WebFeed, this information includes the base URL for the feed, as well as information on how frequently to fetch the feed. For more information on server objects, please see the topic CREATE FOREIGN DATA WRAPPER in the s-Server Streaming SQL Reference Guide.
CREATE OR REPLACE SERVER "GoogleNews"
TYPE 'Google NewsFeeds'
FOREIGN DATA WRAPPER "WebFeed"
DESCRIPTION 'follow data from Google news feeds';
The foreign stream is the SQL object where data from the Atom/RSS adapter will be accessed. Once you create a foreign stream for the WebFeed adapter, you can query it as you would any other SQL object.
Since the Atom/RSS adapter does not know the structure of the target feed, you must define the SQLstream FOREIGN STREAM rowtype to match the rowtype of the feed. The foreign stream columns need to be in the same order as those of the web feed.
The following code creates a foreign stream using the server defined above. The list of columns defined for the foreign stream maps to the list of xpath expressions defined under server options.
CREATE OR REPLACE SCHEMA "RSSTest";
DROP SCHEMA "RSSTest" CASCADE;
CREATE SCHEMA "RSSTest" DESCRIPTION 'read RSS data feeds';
SET SCHEMA '"RSSTest"';
CREATE OR REPLACE FOREIGN STREAM "GoogleNewsRSS" (
DATE_FORMAT 'EEE, dd MMM yyyy HH:mm:ss z',
-- xpath expressions are evaluated relative to the current "item" element
-- note that rooted xpath expressions also work, e.g. /rss/channel/... or --channel/...
DESCRIPTION 'GoogleNews feed in RSS format';
Defining a foreign stream and server for an Atom feed is similar to those for RSS. Atom feeds specify a default namespace which requires a prefix for use with XPath expressions. The default prefix, "defaultns", is always available. You may also specify a preferred prefix in the DEFAULT_NS_PREFIX option. Use one of these prefixes to qualify each XPath expression element in the default namespace.
CREATE SCHEMA "AtomTest" DESCRIPTION 'test web-based data feeds';
SET SCHEMA '"AtomTest"';
CREATE OR REPLACE FOREIGN STREAM "GoogleNewsAtom" (
-- xpath expressions are evaluated relative to the current "entry" element
-- note that rooted xpath expressions also work, e.g. /feed/...
DESCRIPTION 'GoogleNews feed in Atom format';