From BaseX Documentation
Revision as of 12:46, 25 May 2012 by AW (talk | contribs)
Jump to navigation Jump to search

As Twitter attracts more and more users (over 140 million active users in 2012) and is generating large amounts of data (over 340 millions of short messages ('tweets') daily), it became a really exciting data source for all kind of analytics. Twitter provides the developer community with a set of APIs for retrieving the data about its users and their communication, including the Streaming API for data-intensive applications, the Search API for querying and filtering the messaging content, and the REST API for accessing the core primitives of the Twitter platform.

This article is about the use of BaseX for processing and storing the live data stream of Twitter. We illustrate some statistics about the Twitter data and the performance of BaseX.

BaseX as Twitter Storage

For retrieving the Twitter stream we connect with the Streaming API to the endpoint of Twitter and receive a never ending tweet stream. As Twitter delivers the tweets as JSON objects the objects has to be converted into XML fragments. For this purpose the parse function of the XQuery JSON Module is used. The examples tweet as JSON and tweet as XML show that each tweet is streamed as an object containing the tweet message itself and over 60 data fields (for further information see the fields description). For storing the tweets including the meta-data, we use the standard insert function of XQuery Update.

Twitters' Streaming Data

The following figure shows the amount of data, that is delivered by the Twitter Streaming API to the connected endpoints with the 10% gardenhose access per hour on the 6th of the months February, March, April and May. It is the pure public live stream without any filtering applied.



Type Description Example (native → hex integers)
Num Compressed integer (1-5 bytes), specified in 150F; 51141 FF
Token Length (Num) and bytes of UTF8 byte representation Hello05 48 65 6c 6c 6f
Double Number, stored as token 12303 31 32 33
Boolean Boolean (1 byte, 00 or 01) true01
Nums, Tokens, Doubles Arrays of values, introduced with the number of entries 1,202 01 31 01 32
TokenSet Key array (Tokens), next/bucket/size arrays (3x Nums)

Example Tweet (JSON)

  "contributors": null,
  "text": "Using BaseX for storing the Twitter Stream",
  "geo": null,
  "retweeted": false,
  "in_reply_to_screen_name": null,
  "possibly_sensitive": false,
  "truncated": false,
  "entities": {
    "urls": [
    "hashtags": [
    "user_mentions": [
  "in_reply_to_status_id_str": null,
  "id": 1984009055807*****,
  "in_reply_to_user_id_str": null,
  "source": "<a href=\"http:\/\/\" rel=\"nofollow\">twitterfeed<\/a>",
  "favorited": false,
  "in_reply_to_status_id": null,
  "retweet_count": 0,
  "created_at": "Fri May 04 13:17:16 +0000 2012",
  "in_reply_to_user_id": null,
  "possibly_sensitive_editable": true,
  "id_str": "1984009055807*****",
  "place": null,
  "user": {
    "location": "",
    "default_profile": true,
    "statuses_count": 9096,
    "profile_background_tile": false,
    "lang": "en",
    "profile_link_color": "0084B4",
    "id": 5024566**,
    "following": null,
    "protected": false,
    "favourites_count": 0,
    "profile_text_color": "333333",
    "contributors_enabled": false,
    "verified": false,
    "description": "http:\/\/\/5ktAf",
    "profile_sidebar_border_color": "C0DEED",
    "name": "BaseX",
    "profile_background_color": "C0DEED",
    "created_at": "Sat Feb 25 04:05:30 +0000 2012",
    "default_profile_image": true,
    "followers_count": 860,
    "geo_enabled": false,
    "profile_image_url_https": "https:\/\/\/sticky\/default_profile_images\/default_profile_0_normal.png",
    "profile_background_image_url": "http:\/\/\/images\/themes\/theme1\/bg.png",
    "profile_background_image_url_https": "https:\/\/\/images\/themes\/theme1\/bg.png",
    "follow_request_sent": null,
    "url": "http:\/\/\/5ktAf",
    "utc_offset": null,
    "time_zone": null,
    "notifications": null,
    "friends_count": 2004,
    "profile_use_background_image": true,
    "profile_sidebar_fill_color": "DDEEF6",
    "screen_name": "BaseX",
    "id_str": "5024566**",
    "show_all_inline_media": false,
    "profile_image_url": "http:\/\/\/sticky\/default_profile_images\/default_profile_0_normal.png",
    "is_translator": false,
    "listed_count": 0
  "coordinates": null

Example Tweet (XML)

<json booleans="retweeted possibly__sensitive truncated favorited possibly__sensitive__editable default__profile profile__background__tile protected contributors__enabled verified default__profile__image geo__enabled profile__use__background__image show__all__inline__media is__translator" 
  numbers="id retweet__count statuses__count favourites__count followers__count friends__count listed__count"
  nulls="contributors geo in__reply__to__screen__name in__reply__to__status__id__str in__reply__to__user__id__str in__reply__to__status__id in__reply__to__user__id place following follow__request__sent utc__offset time__zone notifications coordinates" 
  arrays="urls indices hashtags user__mentions"
  objects="json entities user">
  <text>Using BaseX for storing the Twitter Stream</text>
  <source><a href="" rel="nofollow">twitterfeed</a></source>
  <created__at>Fri May 04 13:17:16 +0000 2012</created__at>
    <created__at>Sat Feb 25 04:05:30 +0000 2012</created__at>

BaseX Performance