Graph API
At Facebook's core is the social graph; people and the connections they have to everything they care about. The Graph API presents a simple, consistent view of the Facebook social graph, uniformly representing objects in the graph (e.g., people,photos, events, and pages) and the connections between them (e.g., friend relationships, shared content, and photo tags).
GraphAPI就是FaceBook的核心API, 用于替代传统的restful API.
对于FaceBook这样一个庞大的系统而言, 需要一个相对高阶抽象的API, 不然随着业务的扩展, API的规模将迅速扩大, 变得很难维护.
所以他推出Graph API, 但是抽象的东西往往会比较难以理解, 尤其是对于用惯了twitter API这种简单API的用户.
但是理解后, 你就明白这样的API是最适合facebook的, 可以说很多的借鉴了semantic web的思维
对于Facebook, 全球最大的social network, 实际就是维护了一张巨大的graph, graph中的点包含了万事万物, 而真正有价值的是点点之间的线, 万事万物都是有联系的. “不是风动,不是旗动,是心动”很好的反映了万事万物之间千丝万缕的联系呵呵
而facebook的目的应该就是收集尽可能多的线. 你看这个概念和本体建模的概念是不是很像. 从此可以看出Facebook这个公司的牛比和野心.
这样的话, 复杂问题就简单化了, 你所有的查询不外乎两种,
查看某一个点的属性
查看某一点和其他点的联系
理解了吗?
这两年对Twitter的研究如火如荼, 可是很少听到对Facebook社会性数据的研究, 我以前也一直纳闷, 什么原因?
原因很简单, 你拿不到重要的社会化数据
Facebook是严格权限控制的, 通过OAuth2.0, 你如果想得到任何点和其他点的联系, 必须通过这个的点的授权
好了, 说白了, 你通过API只能得到各个孤立点的信息, 而前面说了真正有价值的是点与点之间的联系, 而这些数据你是拿不到的(除非获得授权)
当然这个是出于个人隐私考虑, 但是其实也就断绝了绝大多数Facebook社会性数据的研究的可能性.
还是简单介绍一下Graph API, 象我上面介绍的, 其实很简单了
All objects in Facebook can be accessed in the same way:
- Users: https://graph.facebook.com/btaylor (Bret Taylor)
- Pages: https://graph.facebook.com/cocacola (Coca-Cola page)
- Events: https://graph.facebook.com/251906384206 (Facebook Developer Garage Austin)
- Groups: https://graph.facebook.com/195466193802264 (Facebook Developers group)
- Applications: https://graph.facebook.com/2439131959 (the Graffiti app)
- Status messages: https://graph.facebook.com/367501354973 (A status message from Bret)
- Photos: https://graph.facebook.com/98423808305 (A photo from the Coca-Cola page)
- Photo albums: https://graph.facebook.com/99394368305 (Coca-Cola's wall photos)
- Profile pictures: http://graph.facebook.com/100000992767935/picture (your profile picture)
- Videos: https://graph.facebook.com/817129783203 (A Facebook tech talk on Graph API)
- Notes: https://graph.facebook.com/122788341354 (Note announcing Facebook for iPhone 3.0)
- Checkins: https://graph.facebook.com/414866888308 (Check-in at a pizzeria)
All of the objects in the Facebook social graph are connected to each other via relationships
- Friends: https://graph.facebook.com/me/friends?access_token=...
- News feed: https://graph.facebook.com/me/home?access_token=...
- Profile feed (Wall): https://graph.facebook.com/me/feed?access_token=...
- Likes: https://graph.facebook.com/me/likes?access_token=...
- Movies: https://graph.facebook.com/me/movies?access_token=...
- Music: https://graph.facebook.com/me/music?access_token=...
- Books: https://graph.facebook.com/me/books?access_token=...
- Notes: https://graph.facebook.com/me/notes?access_token=...
- Permissions: https://graph.facebook.com/me/permissions?access_token=...
- Photo Tags: https://graph.facebook.com/me/photos?access_token=...
- Photo Albums: https://graph.facebook.com/me/albums?access_token=...
- Video Tags: https://graph.facebook.com/me/videos?access_token=...
- Video Uploads: https://graph.facebook.com/me/videos/uploaded?access_token=...
- Events: https://graph.facebook.com/me/events?access_token=...
- Groups: https://graph.facebook.com/me/groups?access_token=...
- Checkins: https://graph.facebook.com/me/checkins?access_token=...
可以看出, 所有关系的查询都是需要权限的...真可惜, 不然可以产生多少有意思的场景和应用
除此之外, 还有两个API很有用
对象元数据
https://graph.facebook.com/331218348435?metadata=1
{ "name": "Facebook Developer Garage Austin - SXSW Edition", "metadata": { "connections": { "feed": "http://graph.facebook.com/331218348435/feed", "picture": "https://graph.facebook.com/331218348435/picture", "invited": "https://graph.facebook.com/331218348435/invited", "attending": "https://graph.facebook.com/331218348435/attending", "maybe": "https://graph.facebook.com/331218348435/maybe", "noreply": "https://graph.facebook.com/331218348435/noreply", "declined": "https://graph.facebook.com/331218348435/declined" } } }
Searching
We support search for the following types of objects:
- All public posts: https://graph.facebook.com/search?q=watermelon&type=post
- People: https://graph.facebook.com/search?q=mark&type=user
- Pages: https://graph.facebook.com/search?q=platform&type=page
- Events: https://graph.facebook.com/search?q=conference&type=event
- Groups: https://graph.facebook.com/search?q=programming&type=group
- Places: https://graph.facebook.com/search?q=coffee&type=place¢er=37.76,122.427&distance=1000
- Checkins: https://graph.facebook.com/search?type=checkin
Facebook Query Language (FQL)
The FQL object enables running FQL queries using the Graph API. Facebook Query Language, or FQL, enables you to use a SQL-style interface to query the data exposed by the Graph API. It provides for some advanced features not available in the Graph API, including batching multiple queries into a single call.
可见FQL是基于Graph API的, 这不过更方便和好用一些, 对于有SQL经验的人
FQL也是严格权限控制的, 还句话说Graph API查不到的, FQL也查不到
Unlike SQL, the FQL FROM
clause can contain only a single table. 不能连表查询
但可以使用IN
SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
而且比GraphAPI方便的是, 可以一次执行多条FQL语句,并返回最终结果
"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)"
下面列出的是Facebook可以查询的所有表, 相对于facebook那么庞大的系统而言, 表结构还是相当简单的, 大家如果要使用FQL必须先熟悉这些表
Facebook公开这些表结构, 还有个好处是可以给所有想建social network数据库提供一个参考模型, 一个标杆, 就这么建就可以了.
Tables
Query this table to return information about a photo album.
Query this table to return read-only properties about an application.
An FQL table containing the requests sent via an app to a user.
Query this table to return information about a checkin. By default, this query returns the last 20 checkins and returns a maximum of 500 checkins.
Query this table to obtain comments associated with one or more [fb:comments]
The comments_info FQL table. Query this table to obtain XIDs for fb:comments objects associated with an application ID.
Query this table to return a user's friends and the Facebook Pages to which the user is connected.
Query this table to return information about a cookie.
Query this table to return the application IDs for which the specified user is listed as a developer in the Developer application.
The domain table provides a read-only mapping between domain names and ids.
Query this table to return information about the admin of a domain.
Query this table to return information about an event.
Query this table to return information about a user's status for an event or see a list of events for a user.
Query this table to return detailed information about a user's family.
Query this table to determine whether two users are linked together as friends.
Query this table either to determine which users have sent friend requests to the logged-in user or to query whether a friend request has been sent from the logged-in user to a specific user.
Query this table to return any friend lists owned by the specified user.
Query this table to determine which users are members of a friend list.
Query this table to return information about a group.
Query this table to return information about the members of a group, or retrieve a list of groups of which a user is a member
The insights table contains statistics about Applications, Pages and Domains
Query this table to return the user IDs of users who like a given Facebook object (video, note, link, photo, or album).
Query this table to return the links a user has posted.
The link_stat table contains counts that show how users on Facebook are interacting with a given link.
The mailbox_folder table contains information about a user's mailbox folders.
Query this table to return information about messages in a thread.
Query this table to return the notes the current user has written or to return details for a particular note.
Query this table to get the notifications for the current session user, that is, any notification that appears on http://www.facebook.com/notifications.php.
Query this table to return information about a URL in the Open Graph
Query this table to return information about a Facebook Page.
Query this table to return information about which Facebook Pages the user Admins.
An FQL table that can be used to return a list of a users that are blocked from a Facebook Page.
Query this table to return information about the user who likes a Facebook Page.
Query this table to return the permissions the current user has granted to the app.
Query this table to return more descriptive information about extended permissions.
Query this table to return information about a photo.
Query this table to return information about a photo tag.
Query this table to return information about a place.
Query this table to return a user's privacy setting for a given object_id.
Query default privacy settings for a user for a particular app
Query this table to return certain (typically publicly) viewable information for a profile.
A Question as represented in FQL.
An option for a question, as represented in FQL.
The votes on a particular option for a question, as represented in FQL.
Query this table to obtain reviews associated with an application, a user or both.
Query this table to determine whether two users are linked together as friends.
Query this table to return standard information about a user, for use when you need analytic information only.
Query this table to return one or more of a user's statuses.
Query this table to return posts from a user's stream or the user's profile.
Query this table to return a filter_key that can be used to query the stream FQL table, as seen through any content filters the user has available on Facebook.
Query this table to return associations between users or Facebook Pages and the items they tag in status posts.
Query this table to return information about message threads in a user's Inbox.
Query this table to return the native strings (original, untranslated text in your application interface) and the translated strings for your application.
This table can be used to access information about messages in the new Facebook messaging system.
This table can be used to access information about threads in the new Facebook messaging system.
This table should be used to access information about subscribe and unsubscribe actions performed on a thread in the new Facebook messaging system.
This table should be used to access information about the number of threads in a folder in the new Facebook messaging system.
An FQL table containing the Open Graph URLs that the current session user has Liked.
Query this table to return detailed information from a user's profile.
The video table contains information about videos.
The video_tag table contains information about users tagged in videos.
本文章摘自博客园,原文发布日期:2012-01-31