Wednesday, November 6, 2013

Using Yahoo Query Language in Liferay(YQL)

Using Yahoo Query Language in Liferay(YQL)

Yahoo Query Language (YQL)

The Yahoo Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services. With YQL, apps run faster with fewer lines of code and a smaller network footprint.

Download Liferay YQL portlet from following location
You can find source and war file

Note:  Portlet developed in Liferay 6.1GA2 EE version

If you want deploy in CE version you just do changes in

Liferay 6.1 EE version

author=Liferay, Inc.

Liferay 6.1 CE version

name LiferayYQL
author=Liferay, Inc.

Understanding YQL:

Yql provide syntax’s like sql so that we can fetch data from yahoo web services.
We already know about web service like SOAP and REST but YQL is very simple to use in web sites.
This is very special language to yahoo to fetch yahoo data .
But we can also provide our data available to YQL using data tables.

The following scenarios we can use YQL

Fetch Weather Information
Fetch Stock Market Information Up to date
Currency conversion
Fetch yahoo news

How to use YQL:

Open Yahoo console from fowling URL

Select any tables which are left side
Select any tables it will populate sample query in console text box
You can click on Test button and see the result.
Here YQL provide to kinds of data JSON and XML before click on test button you can select any of formats then data will be showed in result part.
Once the result is desirable then copy the URL which showed in bottom this URL can be used to get content.
Once get content you can display into your website


Lets us assume we want display weather information in our web site:
Go to Yahoo console

Execute following YQL query
select * from weather.forecast where location=90210
Select JSON and Test
In the result you can see the data related to weather you can identify json or xml structure and retrieve in website.

We can use YQL in different ways:

I suggest you use two ways

YUI YQL java script library
Simple jquery AJAX

YUI YQL java script library:

To use YQL in your application first we need following YQL and YUI library
<script src=""></script>
And use YUI use method and add required modules we need to add yql module when we work with YQL

The following is complete code

<script src=""></script>
YUI().use('node','yql', function(Y) {
    var res ='#res');
        var woeid = '2502265';
    Y.YQL('select * from weather.forecast where woeid='+woeid, function(r) {
        var el = Y.Node.create('<div class="mod"></div>');
        el.set('innerHTML', '<h2>Weather for ' + zip + '</h2>' +;
<div id="res">Weather Info Here</div>

Simple jquery AJAX:

We can also used jquey Ajax to execute YQL queries
Generally All YQL queries requested by following URL
We need to pass following parameters to above URL

q :   the YQL query which you desire
format:   type of data xml or json
env:   env is type of parameter which specified where data table are available
For more info we need read about data tables
diagnostics: is Boolean value  it will give error information in response if anything fails
callback: is optional parameter

The following is example: * from weather.forecast where woeid=2502265&format=json&diagnostics=true&env=


When we execute any YQL statement in YQL console for each statement we will gate URL in the bottom
If see URL you can identify url and its query string but here all parameters are encoded format.
For know what parameters using you yql statement you can decode URL. The following is URL decoding in online.

<script src=""></script>
<script type="text/javascript">
    var weatherQuery='select * from weather.forecast where woeid=2502265’
        url: '',
        data: {
        type: "get",
        success: function(r){
        beforeSend: function(){
                                complete: function(){
        error: function(){

<div id="res">Weather Info Here</div>

YQL can fetch against yahoo web services
The following are some information about data tables

You can see all tables and execute query in YQL console and find the data structure and use

Get Stocks quotes information:

Query: select * from where symbol in ("YHOO","AAPL","GOOG","MSFT")

Get Stock Market Indexes

Query: select * from where symbol in ("^hsi","^bsesn","^nsei","^dji","^ixic")

Get Top Stories from news table:

Table: yahoo feed
Query: select * from feed where url=''

Get Weather Organization  ID(Weoid) from Longitude and Latitude

Table: geo.placefinder
Query: select * from geo.placefinder where text="22.2500,114.1667" and gflags="R"

Get Weather Organization  ID(Weoid) from Address

Table: geo.placefinder
Query: select * from geo.placefinder where text='Central,Honk Kong' and gflags='R'

WE can also fetch html from websites using YQL

If some time we did not find any web services simple from web page you can fetch data and you can filter the content.
Here in where condition we need to pass xpath notation
Fetch some use full data from Yahoo finance page like all Stock indexes
select * from html where url="" and xpath='//div[@class="desc"]'

select * from html where url="" and


We can open page and identify the required content from page using Chrome inspect element option.
Find the xpath of content and apply to YQL.

Important Points:

  • YQL is like sql statements to fetch content from yahoo web services.
  • These all information they provided as data tables. So that we can use YQL against data  tables to fetch the content.
  • YQL provide xml and JSON formats.
  • Each YQL statement can be provided as web URL so that we can directly execute in browser and see the result.
  • To fetch the content from data table yahoo provides one URL and we need to use that public URL and pass some query parameter to get result. By this we can send Ajax calls.
  • Yahoo provide special module YQL module from their java script library i.e. YUI.
  • We can use YUI  yql library to get content from data tables.
  • YQL very convenient way to use in web site because  we can use this with java script libraries like jquery,YUI or other java script  libraries.
  • YQL also provide way to fetch html content for web page as json or xml format so that we can use this data in website this scenarios happen when we don’t have web services.

Reference Links

YQL tutorial

YQL console

YQL complete tutorial

Data Tables

Xpath tutorial:

Online URL encodes and decodes:

Author: Meera Prince


Post a Comment

Recent Posts

Recent Posts Widget

Popular Posts