Designing a key hierarchy for indexing tweets by both their ad content and the time they were scraped involves creating an efficient and scalable data structure. Here's a step-by-step guide to achieve this using a NoSQL database like Amazon DynamoDB, MongoDB, or Google Firestore. For this example, I'll use DynamoDB due to its robust indexing capabilities.
Step 1: Define Your Data Model
Each tweet will have the following attributes:
tweet_id (String): Unique identifier for the tweet.
ad_content (String): The content of the tweet ad.
scraped_at (ISO 8601 String or Timestamp): The time when the tweet was scraped.
other_fields (Various Types): Any other relevant information about the tweet.
Step 2: Choose a Partition Key and Sort Key
In DynamoDB, you need to define a partition key (hash key) and optionally a sort key (range key). To allow indexing by both ad_content and scraped_at, you can use a composite key strategy.
Option 1: Using scraped_at as the Partition Key and tweet_id as the Sort Key
-
Table Definition:
- Partition Key:
scraped_at
- Sort Key:
tweet_id
-
Global Secondary Index (GSI):
- GSI Name:
AdContentIndex
- GSI Partition Key:
ad_content
- GSI Sort Key:
scraped_at
With this setup:
- You can query tweets by
scraped_at directly using the primary key.
- You can query tweets by
ad_content using the AdContentIndex.
- To query tweets by both
ad_content and scraped_at, you can use the GSI with a composite key condition.
Step 3: Create the Table and Index
DynamoDB Table Creation:
{
"TableName": "Tweets",
"KeySchema": [
{
"AttributeName": "scraped_at",
"KeyType": "HASH"
},
{
"AttributeName": "tweet_id",
"KeyType": "RANGE"
}
],
"AttributeDefinitions": [
{
"AttributeName": "scraped_at",
"AttributeType": "S"
},
{
"AttributeName": "tweet_id",
"AttributeType": "S"
},
{
"AttributeName": "ad_content",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"GlobalSecondaryIndexes": [
{
"IndexName": "AdContentIndex",
"KeySchema": [
{
"AttributeName": "ad_content",
"KeyType": "HASH"
},
{
"AttributeName": "scraped_at",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
]
}
Step 4: Inserting Data
When inserting a tweet, ensure you populate all necessary attributes.
import boto3
from datetime import datetime
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Tweets')
tweet = {
'tweet_id': '123456',
'ad_content': 'Sample ad content',
'scraped_at': datetime.utcnow().isoformat(), # ISO 8601 format
'other_fields': {'likes': 100, 'retweets': 50}
}
table.put_item(Item=tweet)
Step 5: Querying Data
Query by scraped_at
response = table.query(
KeyConditionExpression=Key('scraped_at').eq('2023-10-04T12:34:56')
)
Query by ad_content using the GSI
response = table.query(
IndexName='AdContentIndex',
KeyConditionExpression=Key('ad_content').eq('Sample ad content')
)
Query by both ad_content and scraped_at using the GSI
response = table.query(
IndexName='AdContentIndex',
KeyConditionExpression=Key('ad_content').eq('Sample ad content') & Key('scraped_at').eq('2023-10-04T12:34:56')
)
Alternative: Using MongoDB
If you prefer using MongoDB the process would be as follows:
Step 2: Define Your Collection
Create a collection named tweets with the following documents:
{
"tweet_id": "123456",
"ad_content": "Sample ad content",
"scraped_at": ISODate("2023-10-04T12:34:56Z"),
"other_fields": {"likes": 100, "retweets": 50}
}
Step 3: Create Indexes
Create indexes to allow efficient querying.
db.tweets.createIndex({ scraped_at: 1 });
db.tweets.createIndex({ ad_content: 1 });
db.tweets.createIndex({ ad_content: 1, scraped_at: 1}); //Composite index
Step 4: Inserting Data
db.tweets.insertOne({
"tweet_id": "123456",
"ad_content": "Sample ad content",
"scraped_at": ISODate("2023-10-04T12:34:56Z"),
"other_fields": { "likes": 100, "retweets": 50 }
});
Step 5: Querying Data
Query by scraped_at:
db.tweets.find({ scraped_at: ISODate("2023-10-04T12:34:56Z") });
Query by ad_content:
db.tweets.find({ ad_content: "Sample ad content" });
Query by both ad_content and scraped_at:
db.tweets.find({ ad_content: "Sample ad content", scraped_at: ISODate("2023-10-04T12:34:56Z") });
Conclusion
By using a composite key strategy and global secondary indexes in DynamoDB, or by creating appropriate indexes in MongoDB you can efficiently index and query tweets by both their ad content and the time they were scraped. This setup ensures that you can perform both types of queries quickly and effectively.