Overview of Datafaker
mainjava-faker that includes updated libraries and new fake generators. It is designed to provide high-quality, realistic data similar to Faker libraries available in Ruby, Perl, Python, PHP, and JavaScript.repository·main·Indexed 23 days ago
https://github.com/datafaker-net/datafakerA modern Java library for generating realistic fake data for development, testing, and showcasing projects. As a maintained successor to java-faker, it supports Java 17 (v2.x) and provides features such as localized data via Locales, unique value generation, string-based expressions, and structured data output in CSV and JSON formats using Schemas and Transformers.
java-faker that includes updated libraries and new fake generators. It is designed to provide high-quality, realistic data similar to Faker libraries available in Ruby, Perl, Python, PHP, and JavaScript.Datafaker organizes its fake data generation capabilities into several logical provider groups. Depending on your use case, you can access specialized data from the following categories:
Datafaker (v1.4.0+) is designed for high performance, often performing 10x-100x faster than older libraries like Java Faker (1.0.2), Kotlin-faker (1.11.0), or JFairy (0.6.5) in various use cases.
Key performance observations:
firstname, fullname, and address, Datafaker provides much higher throughput than its predecessors.numerify, letterify, bothify, and regexify.A Schema is a set of rules used to transform data from Datafaker's internal representation into supported formats (CSV, JSON, SQL, etc.). You can use a schema to either generate data from scratch or transform existing data collections.
Schemas support nested (composite) fields using compositeField.
Schema<String, String> schema =
Schema.of(
field("first_name", () -> faker.name().firstName()),
field("last_name", () -> faker.name().lastName()),
field("address", () -> faker.address().streetAddress()));The documentation site is built using MkDocs and Material for MkDocs. The repository structure is organized as follows:
mkdocs.yml: Site configuration, theme features, and plugin definitions.requirements-docs.txt: Pinned Python dependencies (Material, plugins).docs/: Markdown content and static assets.docs/assets/images/: Favicon and hero illustrations.docs/stylesheets/extra.css: Site-specific CSS.material/overrides/: Jinja template overrides only (e.g., home.html, main.html)..github/workflows/deploy-docs.yml: CI/CD workflow for production deployment.Important: material/overrides/ is the only content that belongs in the material/ directory. Base templates, partials, icons, and bundled JS/CSS are provided by the pip-installed theme.
Since version 1.2.0, Datafaker allows you to pass a format string as an argument to date and time methods to control the output format of generated dates and timestamps. This works with standard Java date-time pattern strings.
Faker faker = new Faker();
System.out.println(faker.timeAndDate().future(1, TimeUnit.HOURS, "yyyy MM.dd mm:hh:ss"));
System.out.println(faker.timeAndDate().past(1, TimeUnit.HOURS, "yyyy-MM-dd mm:hh:ss"));
System.out.println(faker.timeAndDate().birthday(1, 99, "yyyy/MM/dd"));To generate fake data in Java, instantiate a Faker object and access various provider methods (e.g., name(), address()) to retrieve generated strings.
import net.datafaker.Faker;
Faker faker = new Faker();
String name = faker.name().fullName(); // Miss Samanta Schmidt
String firstName = faker.name().firstName(); // Emory
String lastName = faker.name().lastName(); // Barton
String streetAddress = faker.address().streetAddress(); // 60018 Sawayn Brooks Suite 449The SqlTransformer generates INSERT statements. It supports two modes:
.batch(n)).You can specify the table name and a SqlDialect (e.g., SqlDialect.POSTGRES, SqlDialect.ORACLE) to handle identifier quoting and syntax specifics.
SqlTransformer<String> transformer =
new SqlTransformer.SqlTransformerBuilder<String>()
.batch(5)
.tableName("MY_TABLE")
.dialect(SqlDialect.POSTGRES)
.build();
String output = transformer.generate(schema, 10);To publish the Datafaker project to Maven Central, use the Maven deploy command from the project root.
mvn deployFollow these guidelines to maintain a clean and upgradeable documentation codebase:
docs/stylesheets/extra.css and Material CSS variables over vendoring or minifying CSS bundles.extra_css and extra_javascript in mkdocs.yml instead of creating custom JS bundles whenever possible.material/overrides/ minimal. Always extend existing theme templates (like base.html or main.html) rather than copying the entire file.Since version 2.4.2, you can implement weighted random selection in hardcoded providers. This allows you to return values based on specific probabilities.
To do this, add a WeightedRandomSelector to your provider and pass a list of maps containing value and weight keys to the selector.select() method.
Note: This feature is currently in the POC stage and is only available for custom hardcoded providers.
public static class Insect extends AbstractProvider<BaseProviders> {
private static final WeightedRandomSelector selector = new WeightedRandomSelector(new Random());
private static final List<Map<String, Object>> WEIGHTED_INSECTS = List.of(
Map.of("value", "Driver ant", "weight", 6.0),
Map.of("value", "Fire ant", "weight", 3.0),
Map.of("value", "Harvester ant", "weight", 1.0)
);
public Insect(BaseProviders faker) {
super(faker);
}
public String weightedInsectName() {
return selector.select(WEIGHTED_INSECTS);
}
}
// Usage
MyCustomFaker myFaker = new MyCustomFaker();
System.out.println(myFaker.insect().weightedInsectName());