DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Building an AI/ML Data Lake With Apache Iceberg

Trending

  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  • How to Convert XLS to XLSX in Java
  • Data Quality: A Novel Perspective for 2025
  • Advancing Your Software Engineering Career in 2025
  1. DZone
  2. Data Engineering
  3. Data
  4. Generating Simulated Streaming Data

Generating Simulated Streaming Data

In this article, learn more about using the Python library, Faker, to build synthetic data for tests and utilize Pulsar to send messages to topics at scale.

By 
Tim Spann user avatar
Tim Spann
DZone Core CORE ·
Timothy Spann user avatar
Timothy Spann
·
Mar. 06, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
8.2K Views

Join the DZone community and get the full member experience.

Join For Free

For demos, system tests, and other purposes, it is good to have a way to easily produce realistic data at scale utilizing a schema of our own choice.

Fortunately, there is a great library for Python called Faker that lets us build synthetic data for tests. With a simple loop and a Pulsar produce call, we can send messages to topics at scale.

pip3 install Faker

Let's build a topic to send data to as below:

bin/pulsar-admin topics create persistent://public/default/fakeuser


Step 1:  

We build a Faker record by adding providers to the Faker object.

Python
 
from faker import Faker
from faker.providers import internet, address, automotive, barcode, company, date_time, geo, job, misc, person
from faker.providers import phone_number, user_agent

fake = Faker()
fake.add_provider(internet)


Step 2:  

Create or Import Your Schema to model your data and have consistent data in your Pulsar topic for consumers to consume.

Python
 
# Pulsar Message Schema
class PulsarUser (Record):
    created_dt = String()
    user_id = String()
    ipv4_public = String()
    email = String()
    user_name = String()
    cluster_name = String()
    city = String()
    country = String()
    postcode = String()
    street_address = String()
    license_plate = String()
    ean13 = String()
    response  = String()
    comment   = String()
    company = String()
    latitude = Float()
    longitude = Float()
    job = String()
    email_me = Boolean()
    secret_code = String()
    password = String()
    first_name = String()
    last_name = String()
    phone_number = String()
    user_agent = String()


Step 3:  

Connect to your cluster and build a producer.

Python
 
client = pulsar.Client('pulsar://pulsar1:6650')
producer = client.create_producer(topic='persistent://public/default/fakeuser',
                                  schema=JsonSchema(PulsarUser),
                                  properties={"producer-name": "fake-py-sensor","producer-id": "fake-user" })


Step 4: 

Create records (you can do this in a finite or infinite loop).

Python
 
userRecord = PulsarUser()
uuid_key = '{0}_{1}'.format(strftime("%Y%m%d%H%M%S",gmtime()),uuid.uuid4())
userRecord.created_dt = fake.date() 
userRecord.user_id = uuid_key
userRecord.ipv4_public = fake.ipv4_public()


Step 5:  

Send record:

Python
 
producer.send(userRecord,partition_key=str(uuid_key))


An example row of produced JSON data is as follows:

JSON
 
{
 'created_dt': '1974-01-07', 
  'user_id': '20220304192446_045a2724-6f9e-4968-ae19-a5a1a095e57b', 
  'ipv4_public': '207.116.194.88', 
  'email': 'hsanchez@chandler.com', 
  'user_name': 'qpearson', 
  'cluster_name': 'memory-story-see', 
  'city': 'Elizabethview', 
  'country': 'Mauritius', 
  'postcode': '01045', 
  'street_address': '352 Rodriguez Rue', 
  'license_plate': '6-79707I', 
  'ean13': '3151191404713', 
  'response': 'Quality-focused logistical conglomeration', 
  'comment': 'implement value-added relationships', 
  'company': 'Harper LLC', 
  'latitude': 88.5392145, 'longitude': -7.466258, 
  'job': 'Development worker, community', 
  'email_me': None,
  'secret_code': '28b5519f4bb38cd9fc52aa9bb7bca1aa', 
  'password': '+5u&%TTkHt', 
  'first_name': 'Johnny', 
  'last_name': 'Hoffman', 
  'phone_number': '9669534677', 
 'user_agent': 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/3.1)'
}


We can check that our schema is loaded for the topic with the following command.

bin/pulsar-admin schemas get persistent://public/default/fakeuser

We can consume the test messagges with a command line consumer.

bin/pulsar-client consume "persistent://public/default/fakeuser" -s fakeuser-consumer -n 0

We can query the topic like a table via Pulsar SQL.

fakeuser table

fakeuser table 2

Source

https://github.com/tspannhw/FLiP-PY-FakeDataPulsar

Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Building an AI/ML Data Lake With Apache Iceberg

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!