{"id":15044626,"url":"https://github.com/ykayacan/java-dataloader","last_synced_at":"2025-04-10T00:42:41.474Z","repository":{"id":57735371,"uuid":"272110266","full_name":"ykayacan/java-dataloader","owner":"ykayacan","description":"This small library is a Java 11 port of GraphQL DataLoader","archived":false,"fork":false,"pushed_at":"2020-09-19T14:06:54.000Z","size":106,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T00:42:34.894Z","etag":null,"topics":["data-loader","graphql","graphql-dataloader","java","java-dataloader","java11"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ykayacan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-14T01:00:05.000Z","updated_at":"2024-08-17T19:48:12.000Z","dependencies_parsed_at":"2022-08-23T17:21:19.689Z","dependency_job_id":null,"html_url":"https://github.com/ykayacan/java-dataloader","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ykayacan%2Fjava-dataloader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ykayacan%2Fjava-dataloader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ykayacan%2Fjava-dataloader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ykayacan%2Fjava-dataloader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ykayacan","download_url":"https://codeload.github.com/ykayacan/java-dataloader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137998,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-loader","graphql","graphql-dataloader","java","java-dataloader","java11"],"created_at":"2024-09-24T20:50:49.170Z","updated_at":"2025-04-10T00:42:41.457Z","avatar_url":"https://github.com/ykayacan.png","language":"Java","readme":"# Java DataLoader\n\n![Publish Release](https://github.com/ykayacan/java-dataloader/workflows/Publish%20Release/badge.svg)\n![Publish Snapshot](https://github.com/ykayacan/java-dataloader/workflows/Publish%20Snapshot/badge.svg?branch=master)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/ykayacan/java-dataloader)\n![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/io.github.ykayacan/java-dataloader?server=https%3A%2F%2Foss.sonatype.org)\n\nThis small library is a Java 11 port of [GraphQL DataLoader](https://github.com/graphql/dataloader).\n\nDataLoader is a generic utility to be used as part of your application's data fetching layer to provide \na consistent API over various backends and reduce requests to those backends via batching and caching.\n\n## Prerequisites\n\nBefore you begin, ensure you have met the following requirements:\n- Java 11\n\n## Installation\n\n#### Gradle\n\n```groovy\ndependencies {\n  implementation 'io.github.ykayacan:java-dataloader:LATEST_VERSION'\n}\n```\n\n#### Maven\n\n```xml\n\u003cdependencies\u003e\n  \u003cdependency\u003e\n    \u003cgroupId\u003eio.github.ykayacan\u003c/groupId\u003e\n    \u003cartifactId\u003ejava-dataloader\u003c/artifactId\u003e\n    \u003cversion\u003eLATEST_VERSION\u003c/version\u003e\n  \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\n#### Building\nTo build from source use the Gradle wrapper:\n\n```bash\n./gradlew clean build\n```\n\n### Snapshots\n\nYou can access the latest snapshot by adding the repository `https://oss.sonatype.org/content/repositories/snapshots` \nto your build.\n\nSnapshots of the development version are available in [Sonatype's snapshots repository](https://oss.sonatype.org/content/repositories/snapshots/io/github/ykayacan/java-dataloader/).\n\n## Usage\n\nTo get started, create a `DataLoader`. Each `DataLoader` instance represents a unique cache.\nTypically, instances created per request when used within a web-server.\n\n### Batching\n\nBatching is DataLoader's primary feature. Create loaders by providing a `BatchLoader`.\n\n```java\nBatchLoader\u003cLong, Post\u003e batchLoader = new BatchLoader\u003cLong, Post\u003e() {\n    @Override\n    public CompletionStage\u003cList\u003cPost\u003e\u003e load(List\u003cLong\u003e keys) {\n        return CompletableFuture.supplyAsync(() -\u003e postRepository.loadByIds(keys));\n    }\n};\n\nDataLoader\u003cLong, Post\u003e dataLoader = DataLoader.create(batchLoader);\n```\n\nor shorter way\n\n```java\nDataLoader\u003cLong, Post\u003e dataLoader = \n    DataLoader.create(keys -\u003e CompletableFuture.supplyAsync(() -\u003e postRepository.loadByIds(keys)));\n```\n\n`BatchLoader` accepts a list of keys, and returns a `CompletionStage` which resolves to a list of values.\n\nThen load individual values from the loader. DataLoader will coalesce all individual loads which occur within \na single frame of execution (frame until calling `DataLoader.dispatch()`) and then call your `BatchLoader` \nwith all requested keys. \n\n``` java\ndataLoader.load(1)\n    .thenAccept(user -\u003e {\n        System.out.println(\"user = \" + user);\n        dataLoader.load(user.getInvitedByID())\n            .thenAccept(invitedBy -\u003e {\n                System.out.println(\"invitedBy = \" + invitedBy);\n            });\n    });\n    \n// Elsewhere in your application\ndataLoader.load(2)\n    .thenAccept(user -\u003e {\n        System.out.println(\"user = \" + user);\n        dataLoader.load(user.getInvitedByID())\n            .thenAccept(invitedBy -\u003e {\n                System.out.println(\"invitedBy = \" + invitedBy);\n            });\n    });\n    \ndataLoader.dispatch();\n```\n\nA naive application may have issued four round-trips to a backend for the required information,\nbut with DataLoader this application will make at most two.\n\nDataLoader allows you to decouple unrelated parts of your application without sacrificing the performance of \nbatch data-loading. While the loader presents an API that loads individual values, all concurrent requests will be \ncoalesced and presented to your batch loading function. This allows your application to safely distribute data \nfetching requirements throughout your application and maintain minimal outgoing data requests.\n\nBy default, batching is enabled and set size to 1. So, every dataLoader.load() operation issues a round-trip to backend.\nYou can override batchSize by providing a `DataLoaderOptions` to `DataLoader.create(..., options)`.\n\n``` java\nvar options = DataLoaderOptions.\u003cLong, Post\u003enewBuilder()\n    .maxBatchSize(2)\n    .build();\n    \nDataLoader\u003cLong, Post\u003e dataLoader = DataLoader.create(batchLoader, options);\n\ndataLoader.load(1);\ndataLoader.load(2);\ndataLoader.load(3);\ndataLoader.load(4);\n    \ndataLoader.dispatch();\n\n// Batched keys: [[1, 2], [3, 4]]\n```\n\n### Caching\n\nDataLoader provides a memoization cache for all loads which occur in a single request to your application. \nAfter .load() is called once with a given key, the resulting value cached to eliminate redundant loads. \n\n``` java\nvar options = DataLoaderOptions.\u003cLong, Post\u003enewBuilder()\n    .maxBatchSize(2)\n    .build();\n    \nDataLoader\u003cLong, Post\u003e dataLoader = DataLoader.create(batchLoader, options);\n\ndataLoader.load(1);\ndataLoader.load(2);\ndataLoader.load(1);\ndataLoader.load(3);\n    \ndataLoader.dispatch();\n\n// 1 is cached and never issued for a round-trip for the next loads\n// Batched keys: [[1, 2], [3]]\n```\n\nBy default, caching is enabled and use `ConcurrentHashMap` as in-memory cache under the hood. \nYou can override default `CacheMap` by providing a custom `CacheMap` to `DataLoader.create(..., options)`.\n\n``` java\nclass SimpleMap implements CacheMap\u003cLong, CompletableFuture\u003cPost\u003e\u003e {\n    private Map\u003cLong, CompletableFuture\u003cPost\u003e\u003e stash = new LinkedHashMap\u003c\u003e();\n    \n    // omitted methods for clarity\n}\n\nvar customCacheMap = new SimpleMap();\n\nvar options = DataLoaderOptions.\u003cLong, Post\u003enewBuilder()\n    .maxBatchSize(2)\n    .cacheMap(customCacheMap)\n    .build();\n    \nDataLoader\u003cLong, Post\u003e dataLoader = DataLoader.create(batchLoader, options);\n```\n\nYou can also provide a `CacheKey` to use your keys as complex types.\n\n``` java\nvar customCacheKey = new CustomCacheKey();\n\nvar options = DataLoaderOptions.\u003cLong, Post\u003enewBuilder()\n    .maxBatchSize(2)\n    .cacheKey(customCacheKey)\n    .build();\n    \nDataLoader\u003cLong, Post\u003e dataLoader = DataLoader.create(batchLoader, options);\n```\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n\n```text\nCopyright 2020 Yasin Sinan Kayacan\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fykayacan%2Fjava-dataloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fykayacan%2Fjava-dataloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fykayacan%2Fjava-dataloader/lists"}