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.
Join the DZone community and get the full member experience.
Join For FreeThe 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!
Published at DZone with permission of Anthony Bruno. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments