Modeling social media data¶
The Databases page covers the relational model in the abstract. This page applies it to the three platforms whose data the collection notebooks produce.
The recipe is the same for every platform:
- One table per kind of record the API returns.
- The platform's IDs become primary keys.
- References between records become foreign keys.
- Keep the raw JSON as well. The tables are derived, and the next question will need a field they left out.
4chan¶
The 4chan API has three concepts:
- Post: the unit. Every post has a numeric ID (
no) and a timestamp (time). - Thread: an OP post plus its replies. A reply's
restofield holds the OP's post number; an OP hasresto = 0. - Board: a container of threads, identified by a short name like
pol.
There are no users to model, since posting is mostly anonymous.
Post numbers repeat across boards, so a post is identified by the pair (board, no):
CREATE TABLE posts (
board VARCHAR(10) NOT NULL,
no BIGINT NOT NULL,
resto BIGINT NOT NULL, -- 0 for an OP, else the thread's OP
time TIMESTAMP NOT NULL,
name TEXT,
com TEXT, -- the post body, HTML
PRIMARY KEY (board, no)
);
A thread is a query, not a table: WHERE board = %s AND (no = %s OR resto = %s).
YouTube¶
The YouTube Data API returns four kinds of records, each with its own ID:
- Video:
id,snippet.channelId,snippet.publishedAt, title, description, statistics. - Channel:
id, title, country. - Comment:
id, text, like count, andsnippet.parentIdwhen it replies to another comment. - Comment thread: a top-level comment on a video (
snippet.videoId) plus its replies.
Four record kinds, four tables, connected by the IDs the API already provides:
videos.channel_idreferenceschannels.idcomments.video_idreferencesvideos.idcomments.parent_idreferencescomments.id, NULL for a top-level comment
Bluesky¶
In the Bluesky lexicons:
- Post: the unit, identified by its
uri(and content hashcid), carrying the text inrecordand the engagement counts. - Replies are also posts. A reply's
replyobject points at two other posts:parentandroot. - Profile: the author. The
didis the stable identifier; thehandlecan change, so it is an attribute, not a key.
CREATE TABLE profiles (
did TEXT PRIMARY KEY,
handle TEXT,
created_at TIMESTAMP
);
CREATE TABLE posts (
uri TEXT PRIMARY KEY,
author_did TEXT REFERENCES profiles(did),
text TEXT,
created_at TIMESTAMP,
parent_uri TEXT, -- NULL when the post is not a reply
root_uri TEXT,
like_count INT,
repost_count INT
);
parent_uri is deliberately not declared as a foreign key: a collection often contains a reply without its parent, and a strict constraint would reject the reply.
Which constraints to enforce is a design decision.
Engagement counts are a snapshot of the moment of collection.
If the change over time matters, store one row per observation with a collected_at timestamp instead of overwriting.
Worked example: YouTube videos shared on Bluesky¶
A study of which YouTube videos circulate on Bluesky needs both platforms in one database. The collector stores Bluesky posts. A script then extracts the YouTube links from the post text and fetches each video from the YouTube API.
The recipe still applies: four kinds of records, four tables, and the references become foreign keys.
The posts table gains one column, video_id, which is the bridge between the two platforms.
The YouTube tables are created first, because a foreign key's target table must exist before the key can point at it:
CREATE TABLE channels (
id TEXT PRIMARY KEY,
title TEXT
);
CREATE TABLE videos (
id TEXT PRIMARY KEY,
channel_id TEXT REFERENCES channels(id),
title TEXT,
published_at TIMESTAMP
);
CREATE TABLE profiles (
did TEXT PRIMARY KEY,
handle TEXT
);
CREATE TABLE posts (
uri TEXT PRIMARY KEY,
author_did TEXT REFERENCES profiles(did),
text TEXT,
created_at TIMESTAMP,
video_id TEXT REFERENCES videos(id)
);
video_id is NULL when the post links no video.
This design keeps only the first video link per post.
A post with several links would need a separate table that maps posts to videos, one row per pair.
The payoff is that questions spanning both platforms become single queries. Which videos does one handle share?
SELECT videos.title
FROM posts
JOIN profiles ON posts.author_did = profiles.did
JOIN videos ON posts.video_id = videos.id
WHERE profiles.handle = 'example.bsky.social';
Databases behind dashboards¶
A database that a collector keeps filling is what sits under a live dashboard. Two examples from the Observatory on Social Media:
Final thoughts¶
The same recipe works for every platform: one table per kind of record, the API's IDs as primary keys, references as foreign keys, and the raw JSON kept alongside. Which constraints to enforce is a design decision. Start from the questions you want to ask, and check that each one is a query against your tables.