Time Required: 10 min.
This tutorial shows how to create a simple Web application that uses YQL to fetch an RSS feed. The call to YQL is made within
an HTML script
tag, and the returned JSON response is parsed with JavaScript.
yql_news_app.html
.
yql_news_app.html
. The src
attribute in the second script tag is given an empty value intentionally. We will assign a YQL statement to src
later.
You should see the returned JSON response on the Formatted View tab.
The asterisk will return all of the fields (rows) of the response.
results
object. Notice that within the results object, there is the array item
containing objects with the title, link, and description of each news article.
yql_news_app.html
, paste the URL into the src
attribute of the second script
tag as seen below:
Notice that the YQL statement has been URL-encoded and the format
parameter specifies that the response be in JSON.
The new callback function top_stories
will be called as soon as YQL returns the response and have access to the returned JSON.
yql_news_app.html
. Your YQL News Application should resemble the figure below.
Replace the YQL statement for the Yahoo! Top Stories in your YQL News Application with the example query find sushi restaurants in san francisco. Be sure that the format is JSON and that the name of the callback function is correct.
Time Required: 15 min.
This tutorial shows you how create a YQL Open Data Table for the Bay Area Region Transit (BART) Real Time ETA feed. You will then use the YQL console to run a YQL query using the Open Data Table that you created.
YQL already contains an extensive list of built-in tables for you to use that cover a wide range of Yahoo! Web services and access to off-network data. Developers can now add to this list of built-in tables by creating their own Open Data Tables. YQL allows you to create and use your own table definitions, enabling YQL to bind to any data source through the SQL-like syntax and fetch data. Once an Open Data Table is created, anyone can use these definitions in YQL.
By creating an Open Data Table for the feed, you can then use YQL to make queries on the data from the feed and mash up other data from existing YQL tables with the social data. For more information about Open Data Tables, see Using YQL Open Data Tables.
bart.xm
l defines the Open Data Table, which allows the YQL Web service to access data from the BART ETA feed. For a more detailed
explanation, see A Closer Look at the Code.
USE "http://your_domain_name/bart.xml" AS bart_table; SELECT * FROM bart_table;
USE "http://your_domain_name/bart.xml" AS bart_table; SELECT name, eta FROM bart_table WHERE eta.destination LIKE "%SF%"
USE "http://your_domain_name/bart.xml" AS bart_table; SELECT name, eta.destination, eta.estimate FROM bart_table WHERE name
LIKE "%24%"
This section will examine in greater detail at the Open Data Table and the YQL queries that are used in this tutorial.
The Open Data Table must conform to the XML schema table.xsd
, which is given as the XML namespace attribute for the element table
. The element meta
has child elements that provide general information about the Open Data Table such as the author, the location of the documentation,
and a sample query, as shown below.
The element bindings
allows YQL to map the data source so that information can be queried. The element select
defines what repeating element of the XML from the data source will act as a row of a table with the attribute itemPath
, which is <root><station>...</station>
(given as root.element
) in the code below. A good example of a repeating element in XML would be the element entry
in an Atom 1.0 feed.
You invoke an Open Data Table with the verbs USE
and AS
. YQL fetches the Open Data Table defintion pointed to by the verb USE
and then the verb AS
creates an alias to that definition.
USE "http://your_domain_name/bart.xml" AS bart_table;
The alias bart_table
can now be accessed with a YQL statement. The simple YQL query below returns all the rows from the BART feed.
SELECT * FROM bart_table;
Time Required: 15-20 min.
This tutorial shows how to include JavaScript in an Open Data Table definition that is executed by the YQL Web service. The JavaScript in the Open Data Table for this tutorial includes external libraries and uses OAuth to make an authorized call to the Netflix API. With the Netflix Open Data Table, you can then run YQL statements from the YQL console or create an Open Application that searches and displays movies from the Netflix catalog.
Including JavaScript in an Open Data Table definition allows you to do the following:
JavaScript is placed in the element execute
of an Open Data Table definition. When a SELECT statement calls an Open Data Table definition having the element execute
, YQL executes the JavaScript on the backend and returns the response generated by the JavaScript. Open Data Table definitions
having the element execute
must return a response formed by the JavaScript.
In contrast, Open Data Tables not having the element execute
make GET requests to the URI defined in the element url
and return the response from that URI resource. We'll now look at a couple of examples of Open Data Tables with and without
the element execute
to illustrate the difference.
Example of Open Data Table Definition without JavaScript
The Open Data Table definition below does not include the element execute
, so YQL makes a GET request to the URI defined in the element url
: http://www.bart.gov/dev/eta/bart_eta.xml
Example of Open Data Table Definition with JavaScript
Because of the presence of the element execute
, YQL executes the JavaScript and returns the response assigned to response.object
, which will be discussed in Creating the Response.
JavaScript in Open Data Tables definitions have access to the three global objects y
, request
, and response
. From these global objects, methods can be called to include external libraries, perform 2-legged authorization , make requests
to Web services, run YQL queries, convert data structures, such as XML to JSON, and more. We'll be looking at the methods
used in this tutorial that allow you to include external libraries, make requests to Web services, and extract the results
from a returned response.
To read more about the JavaScript global objects and the available methods, see the JavaScript Objects and Methods Reference.
The global object y
includes the method include
that allows you to reference external JavaScript libraries from an Open Data Table definition. Because calls to the Netflix
API must be authorized with OAuth, you include external libraries to handle the authorization and sign the request as shown
in the code below:
To make a request to a Web service, you use the method get
from either the global object request
or from an instance of request
; the instance is created by calling the method rest
from the global object y
. Before looking at the code used in the Open Data Table definition for this tutorial, let's look at a couple of simple examples
of using both the global object and an instance of request
.
This example creates an instance of
request
by passing the URI resource to the method rest
. From the instance, the method get
can make the request to the Web service.
You can also call the method get
directly from the global object request
. The element url
holds the URI to the Web service, which is stored in request.url
.
You can also pass in parameters and define the content returned when making a request to a Web service, which is needed to make authorized requests, such as the request to the Netflix API in this tutorial.
To get the response from the Netflix API, the content type must be defined as 'application/xml', and the OAuth authorization
header must be passed. The code snippet below also uses the objects response
and response.object
to extract results from the returned response and create the response that YQL returns. We'll examine the object response
in Getting and Creating a Response.
To get the data (results) from the returned response, you reference the object
response
. From this earlier code example, you can see the dot operator being used to reference the results of the returned response.
We also saw this example of using the response object to get the data from the Netflix Web service.
When including JavaScript in an Open Data Table, the response from the YQL Web service is determined by the data that is assigned
to the object response.object
. You can use the returned value from a Web service or create your own with JSON or E4X.
In this code snippet, an XML literal is created with E4X and then assigned to response.object
. To interpolate the variable into the XML literal, enclose the variable in braces.
The code from the tutorial assigns the returned XML response to response.object
as seen below. If there is an error, a JSON object holding error message is returned.
The element paging
lets you have more control over the results returned from the YQL query. We'll look in detail at the paging used in the Netflix
Open Data Table that is shown below. For more information about paging, see the Open Data Tables Reference and Paging Results.
The value 'offset' for the attribute model
states that the YQL query can state an offset from the start of the results. The ability to get an offset from a result set
depends on the source of the data, which in the tutorial is the Netflix API. Be sure to verify that your data source allows
retrieving data from an offset when you create future Open Data Tables.
<paging model="offset">
The default offset is set by the attribute default
in the element start
, which is 0 or no offset in this example.
<start id="start_index" default="0" />
The maximum number of results that can be returned by a YQL query on this table is 100, which is defined by the attribute
max
.
<pagesize id="max_results" max="100" />
The attribute defaut
in the element total
states the default number of results returned for each page.
<total default="20" />
To set up the Netflix Open Data Table and run YQL queries:
netflix_table.xm
l defines the Open Data Table, which allows the YQL Web service to access data from the NetFlix API. For a more detailed explanation,
see A Closer Look at the Code.
ck
and cs
.
USE "http://your_domain/netflix.xml" AS netflix_table; SELECT * FROM netflix_table WHERE term="rocky" AND ck="your_consumer_key"
AND cs="your_consumer_secret"
USE "http://yourdomain/netflix.xml" AS netflix_table; SELECT catalog_title.title, catalog_title.box_art.medium, catalog_title.average_rating
FROM netflix_table(10) WHERE term="rocky" AND ck="your_consumer_key" AND cs="your_consumer_secret"
USE "http://yourdomain/netflix.xml" AS netflix_table;
SELECT catalog_title.title.regular, catalog_title.average_rating FROM netflix_table(5,5) WHERE term="star trek" AND ck="your_consumer_key"
AND cs="your_consumer_secret" AND catalog_title.average_rating > "3.0" | sort(field="catalog_title.average_rating") | reverse()
Copy
yql_netflix_app.html
to your Web server.
yql_netflix_appl.html
and insert your Netflix keys and the location of your Netflix Open Data Table in the code as shown below:
netflix_app.html
in the "Application URL" field.