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
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

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

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

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Mastering Redirects With Cloudflare Bulk Redirects
  • How to Convert Excel and CSV Documents to HTML in Java
  • Datafaker 2.0
  • Serverless Extraction and Processing of CSV Content From a Zip File With Zero Coding

Trending

  • The Scrum Guide Expansion Pack
  • A Complete Guide to Modern AI Developer Tools
  • Building an AI Nutrition Coach With OpenAI, Gradio, and gTTS
  • Streamlining DevOps: How Containers and Kubernetes Deliver

Generating CSV Files With SDGen and Faker

Learn about SDGen, an open-source Java library that helps developers generate randomized CSV data files for testing purposes.

By 
Anthony Bruno user avatar
Anthony Bruno
·
Dec. 29, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.0K Views

Join the DZone community and get the full member experience.

Join For Free

The CSV format is a simple and commonly used format for exchanging data. Many applications support the import and export of information as CSV files. Due to the popularity of this format, there is a requirement for developers to generate large amounts of CSV files for testing. This is where my latest open-source project, SDGen, comes into play.

SDGen is a Java library that helps developers generate randomized data files for testing purposes. It supports CSV and fixed width formats, with more formats such as JSON planned for the future.

This guide will show you how to generate a simple CSV file using SDGen and Faker. Faker will be used to assist creating random values.

Maven

For a Maven project, we can add the required libraries by inserting the following XML into the pom.xml file of a project.

<dependencies>
    <dependency>
        <groupId>au.com.anthonybruno</groupId>
        <artifactId>SdGen</artifactId>
        <version>0.3.0</version>
    </dependency>
  
    <dependency>
        <groupId>com.github.javafaker</groupId>
        <artifactId>javafaker</artifactId>
        <version>0.14</version>
    </dependency>
</dependencies>

Instructions

First, we need to get the Faker instance by writing:

Fake faker = faker.instance();

We can then use the Faker instance to generate values such as URLs, i.e. faker.internet().url(), and planet names, i.e. faker.space().planet().

Next, we'll use SDGen's fluent builder to create the schema for the CSV file we want to create. To begin, we write:

    Gen.start()

We can then add fields (AKA columns) using the addField  method. addField  takes two parameters: a String name, which will be to identify the field in the produced file, and a Generator. A Generator is a simple interface with a single method, generate. This is the way that random values are created and added to a field.

We are going to make a simple CSV file of people. To do that, we will add a First Name and a Last Name column using the corresponding Faker methods to generate values for these:

Gen.start()
    .addField("First Name", () -> faker.name().firstName())
.addField("Last Name", () -> faker.name().lastName())

Note: Using lambdas, i.e. () -> faker.name().firstName(), is the equivalent of writing:

new Generator() {
    @Override
    public Object generate() {
        return faker.name().firstName();
    }
}

We also want to add an Age field. To do this, we can use SDGen's built-in IntGenerator. We can give it a sensible minimum and maximum value to limit the range of numbers it will generate. SDGen provides generators for all primitive types.

Gen.start()
    .addField("First Name", () -> faker.name().firstName())
    .addField("Last Name", () -> faker.name().lastName())
    .addField("Age", new IntGenerator(18, 80))

Next, we specify how many rows to generate by using the generate method. We also want to select the format of the generated data. We will be using asCsv to generate the data in CSV format. As I mentioned previously, SDGen also supports the fixed width format and will support other data formats such as JSON in the future.

Gen.start()
    .addField("First Name", () -> faker.name().firstName())
    .addField("Last Name", () -> faker.name().lastName())
    .addField("Age", new IntGenerator(18, 80))
    .generate(1000) //1000 rows will be generated
    .asCsv()

Finally, we specify how the data will be output. We will use the toFile method to put the information into a file.

Gen.start()
    .addField("First Name", () -> faker.name().firstName())
    .addField("Last Name", () -> faker.name().lastName())
    .addField("Age", new IntGenerator(18, 80))
    .generate(1000)
    .asCsv()
    .toFile("people.csv");

And that's it! Running the code will produce a CSV file in the project's working directory. Here is some data that was produced when I ran it:

First Name,Last Name,Age
Corrine,Berge,78
Gerald,Carter,63
Enid,Padberg,66
Eleanora,Murray,79
Coy,Okuneva,76
Jovan,Reynolds,77
Lane,Haag,48

For more information about SDGen, please visit it on GitHub!

CSV

Published at DZone with permission of Anthony Bruno. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Redirects With Cloudflare Bulk Redirects
  • How to Convert Excel and CSV Documents to HTML in Java
  • Datafaker 2.0
  • Serverless Extraction and Processing of CSV Content From a Zip File With Zero Coding

Partner Resources

×

Comments

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
  • [email protected]

Let's be friends: