Facebook Query Language ( FQL )

1 min read

Recently I worked for a project, which fetches data from Facebook and save data locally, Initially we were using Graph API and later migrated to FQL. I would like to share my knowledge about FQL. FQL is Facebook’s SQL alternative, It allows us to query information from Facebook database.
There are 2 options to retrieve data of Facebook pages
1) Graph API
2) FQL

Why FQL ?

Using Graph API it is not possible to make a bulk fetch, eg : If we need to retrieve all the albums and photos of an album then we need to make multiple calls to achieve this. It will increase response time, which affects the performance of the system.
This is where FQL comes in and resolve this problem , which helps us to make a single call to fetch all the data, In our examples, We can retrieve all the albums and photos of an album in a single call, fetch the data, parse and save it to the DB.

FQL Features

FQL enables SQL-style interface to query the data exposed by the Graph API and It provides advanced features that are not available in the Graph API, which includes batching multiple queries in a single call which improves the application performance a lot.

Refer the documents click here

FQL Tables

      album – An album of photos or videos as represented in FQL.
      event – An FQL table that returns information about an event.
      stream – An FQL table that can be used to return a list of a stream posts.
      video – An FQL table containing information about videos.
      profile_pic – An FQL table that returns profile pictures closest to a requested size.

Syntax

Give a HTTP GET request to
https://graph.facebook.com/fql?q=query

Where query can be a single FQL query or a JSON-encoded dictionary of queries.
Example : Single FQL query
[source]
query = SELECT name FROM user WHERE uid = me()
[/source]
Example : JSON encoded dictionary of queries
[source]
“query1″:”SELECT uid, rsvp_status FROM event_member WHERE eid=12345678”
“query2″:”SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #query1)”
[/source]
You can fetch data from query1 and use it in another query2 within the same call.

Note
FQL is still available in version 2.0, but will not be available in the next version of the API.

Facebook Explorer
explorer

Example ruby code to fetch events details using FQL

[source]
class Fql
def fql_update_events()
begin
query = get_fql_events_query()
events_data = RestClient.get(query)
if events_data
events_data = JSON.parse (events_data)
f_events=events_data[“data”][0][“fql_result_set”]
puts “f_events=#{f_events.to_s}”
end
rescue Exception => ex1
puts ex1.to_s
end
end
def get_fql_events_query()
@@app_token = “APPID AND SECRET KEY”
@@page_id = “FACEBOOK PAGE ID”
query1 = “{\”f_events\”:\”select eid, name, description, pic_cover, creator,start_time, end_time, venue, location, pic_big, all_members_count, attending_count, unsure_count from event where creator = #{@@page_id} \”}”
query2 = “https://graph.facebook.com/fql?q=”
escquery=Rack::Utils.escape(query1)
query=query2 + escquery + “&access_token=#{@@app_token}”
return query
end
end
end
fql = Fql.new
fql.fql_update_events()
[/source]
Hope this will help you to understand FQL and use it in your project.
Happy Coding 🙂

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *