def create_user_producer(users: List[Tuple[str]], photos: Deque[Tuple[str, str]], tags: List[Tuple[str]], locations: List[Tuple[str, str]], producer: KeyedProducer) -> Dict[str, str]: """ Produce create-user events to Kafka Arguments: users: List of users who can produce an event photos: Queue of recent photos and their usernames tags: List of company names locations: List of possible global lat/long coordinates producer: Kafka producer object to post messages Returns: Kafka message """ username, full_name = fake_user() created_time, partition_date = get_datetime() record = { "username": username, "full_name": full_name, "created_time": created_time, "partition_date": partition_date, "event": "create-user" } producer.send_messages("create-user", bytes(username, 'utf-8'), json.dumps(record).encode('utf-8')) users.append((username,)) return record
def unfollow_producer(users: List[Tuple[str]], photos: Deque[Tuple[str, str]], tags: List[Tuple[str]], locations: List[Tuple[str, str]], producer: KeyedProducer) -> Dict[str, str]: """ Produce unfollow events to Kafka Arguments: users: List of users who can produce an event photos: Queue of recent photos and their usernames tags: List of company names locations: List of possible global lat/long coordinates producer: Kafka producer object to post messages Returns: Kafka message """ followee, follower = random.choice(users)[0], random.choice(users)[0] created_time, partition_date = get_datetime() record = { "follower_username": follower, "followed_username": followee, "created_time": created_time, "partition_date": partition_date, "event": "unfollow" } producer.send_messages("unfollow", bytes(followee, 'utf-8'), json.dumps(record).encode('utf-8')) return record
def create_photo_producer(users: List[Tuple[str]], photos: Deque[Tuple[str, str]], tags: List[Tuple[str, str]], locations: List[Tuple[str, str]], producer: KeyedProducer) -> Dict[str, str]: """ Produce photo-upload events to Kafka Arguments: users: List of users who can produce an event photos: Queue of recent photos and their usernames tags: List of company names locations: List of possible global lat/long coordinates producer: Kafka producer object to post messages Returns: Kafka message """ user = random.choice(users)[0] tag, link = random.choice(tags) latitude, longitude = random.choice(locations) created_time, partition_date = get_datetime() record = { "username": user, "tags": tag, "photo_link": link, "created_time": created_time, "partition_date": partition_date, "latitude": latitude, "longitude": longitude, "event": "photo-upload" } producer.send_messages('photo-upload', bytes(user, 'utf-8'), json.dumps(record).encode('utf-8')) photos.append((created_time, user)) return record
def comment_producer(users: List[Tuple[str]], photos: Deque[Tuple[str, str]], tags: List[Tuple[str]], locations: List[Tuple[str, str]], producer: KeyedProducer) -> Optional[Dict[str, str]]: """ Produce comment events to Kafka Arguments: users: List of users who can produce an event photos: Queue of recent photos and their usernames tags: List of company names locations: List of possible global lat/long coordinates producer: Kafka producer object to post messages Returns: Kafka message """ if not photos: return None follower = random.choice(users)[0] photo, followee = random.choice(photos) text = get_text() created_time, partition_date = get_datetime() if not all([photo, follower, followee]): return None record = { "follower_username": follower, "followed_username": followee, "photo_id": photo, "text": text, "created_time": created_time, "partition_date": partition_date, "event": "comment" } producer.send_messages("comment", bytes(followee, 'utf-8'), json.dumps(record).encode('utf-8')) return record