Datafaker Documentation

repository·main·Indexed 23 days ago

https://github.com/datafaker-net/datafaker

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

Tokens
24.9K
Snippets
51
Records
81
Agent score
82%

What's inside Datafaker

  1. Overview of Datafaker

    main
    Datafaker is a modern Java library used to generate fake data for development, testing, and showcasing projects. It is a fork of 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.
  2. Explore Datafaker provider groups

    main

    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:

    • Base: Providers for everyday data (e.g., names, addresses, dates).
    • Entertainment: Providers for movies, shows, books, etc.
    • Food: Providers for various types of food.
    • Healthcare: Providers for diseases, medications, procedures, etc.
    • Sport: Providers for different types of sports.
    • Videogame: Providers for video game related data.
  3. Performance comparison: Datafaker vs other libraries

    main

    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:

    • JDK Versioning: Datafaker benefits significantly from newer JDKs. Moving from JDK 8 to JDK 18 can improve performance by up to 25%, whereas Java Faker shows little to no improvement from newer JDK versions.
    • Initialization: Datafaker's initialization (which involves loading provider objects and YAML files) is significantly faster in terms of throughput (ops/ms) compared to Java Faker and JFairy.
    • Simple Methods: For common calls like firstname, fullname, and address, Datafaker provides much higher throughput than its predecessors.
    • String Templates & Expressions: Datafaker is highly efficient at processing string template operations such as numerify, letterify, bothify, and regexify.
  4. Define a Datafaker Schema

    main

    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()));
  5. Reference the documentation site architecture and layout

    main

    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.

  6. Specify date formats for dates and timestamps

    main

    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"));
  7. Generate fake data with Datafaker in Java

    main

    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 449
  8. Transform data to SQL (INSERT statements)

    main

    The SqlTransformer generates INSERT statements. It supports two modes:

    1. Non-batch: One statement per row.
    2. Batch: One statement containing multiple rows (configured via .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);
  9. Customize the documentation site

    main

    Follow these guidelines to maintain a clean and upgradeable documentation codebase:

    • CSS: Prefer using docs/stylesheets/extra.css and Material CSS variables over vendoring or minifying CSS bundles.
    • JS/CSS Integration: Use extra_css and extra_javascript in mkdocs.yml instead of creating custom JS bundles whenever possible.
    • Templates: Keep overrides in material/overrides/ minimal. Always extend existing theme templates (like base.html or main.html) rather than copying the entire file.
  10. Implement weighted random selection in a custom provider

    main

    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());