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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AWS Glue Crawlers: Common Pitfalls, Schema Challenges, and Best Practices
  • Wait, What Format Is That? A Cross—Domain Guide for Everyone
  • Mastering Redirects With Cloudflare Bulk Redirects
  • How to Convert Excel and CSV Documents to HTML in Java

Trending

  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty
  • Spring AI on Local LLMs
  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • LangChainGo vs Genkit Go: Where Genkit Shines

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Dec. 29, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • AWS Glue Crawlers: Common Pitfalls, Schema Challenges, and Best Practices
  • Wait, What Format Is That? A Cross—Domain Guide for Everyone
  • Mastering Redirects With Cloudflare Bulk Redirects
  • How to Convert Excel and CSV Documents to HTML in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook