# java-client
**Repository Path**: ylb521/java-client
## Basic Information
- **Project Name**: java-client
- **Description**: No description available
- **Primary Language**: Java
- **License**: BSD-3-Clause
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2026-03-27
- **Last Updated**: 2026-03-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Weaviate Java client
[](https://github.com/weaviate/java-client/actions/workflows/.github/workflows/test.yaml)
Official Weaviate Java Client.
> [!IMPORTANT]
> `client6` does not support many of the legacy features supported in other clients. Ensure your instance is running at least `v1.32` to avoid compatibility issues.
## Usage
To start using Weaviate Java Client add the dependency to `pom.xml`:
```xml
io.weaviate
client6
6.1.0
```
### Uber JAR🫙
If you're building a uber-JAR with something like `maven-assembly-plugin`, use a shaded version with classifier `all`.
This ensures that all dynamically-loaded dependecies of `io.grpc` are resolved correctly.
```xml
io.weaviate
client6
6.1.0
all
```
### SNAPSHOT releases
The latest development version of `client6` is released after every merged pull request. To include it in you project set the version to `6.0.0-SNAPSHOT` and [configure your `` section accordingly](https://central.sonatype.org/publish/publish-portal-snapshots/#consuming-snapshot-releases-for-your-project).
Please be mindful of the fact that this is not a stable release and breaking changes may be introduced.
Snapshot releases overwrite each other, so no two releases are alike. If you find a bug in one of the `SNAPSHOT` versions that you'd like to report, please include the output of `Debug.printBuildInfo()` in the ticket's description.
```java
import io.weaviate.client6.v1.internal.Debug;
public class App {
public static void main(String[] args) {
Debug.printBuildInfo();
// ...the rest of your application code...
}
}
```
### Gson and reflective access to internal JDK classes
The client uses Google's [`gson`](https://github.com/google/gson) for JSON de-/serialization which does reflection on internal `java.lang` classes. This is _not allowed by default_ in Java 9 and above.
To work around this, it's necessary to add this JVM command line argument:
```
--add-opens=java.base/java.lang=ALL-UNNAMED
```
If you're using Gradle, you can add this instead to your `application` block in your `build.gradle.kts` file:
```kotlin
applicationDefaultJvmArgs += listOf(
"--add-opens=java.base/java.lang=ALL-UNNAMED",
)
```
## Supported APIs
### Tucked Builder
Tucked Builder is an iteration of the Builder pattern that reduces boilerplate and leverages static typing and autocompletion to help API discovery.
It is well-worth getting familiar with Tucked Builders before diving into the next sections, as the library makes intensive use of this pattern across its API surface.
If you've worked with Elasticserch Java API Client before, you'll recognize this pattern as [Builder lamba expressions](https://www.elastic.co/docs/reference/elasticsearch/clients/java/api-conventions/building-objects#_builder_lambda_expressions).
Most operations in Weaviate have multiple optional parameters and Builder patter is a common way to implement that. For example, here's what a nearVector query _could_ look like:
```java
import io.weaviate.client6.v1.api.collections.query.Hybrid;
import io.weaviate.client6.v1.api.collections.query.NearText;
import io.weaviate.client6.v1.api.collections.query.NearText.Move;
Move moveTo = Move.builder()
.force(.5f)
.concepts("lullaby")
.build();
NearText nearText = NearText.builder()
.concepts("sunshine", "butterflies")
.distance(.4f)
.moveTo(moveTo)
.build();
Hybrid hybridQuery = Hybrid.builder()
.concepts("rainbow")
.nearText(nearText)
.queryProperties("title", "lyrics")
.returnProperties("album", "author")
.build();
songs.query.hybrid(hybridQuery);
```
The Tucked Builder pattern replaces repetitive `.builder() [...] .build()` with a **lambda expression** which accepts the pre-instantiated builder object as its only argument.
If that's a mouthful, take a look at what the query above looks like in `client6`. After all, seeing is believing:
```java
import io.weaviate.client6.v1.api.collections.query.NearText;
songs.query.hybrid(
"rainbow",
/* Hybrid.Builder */ h -> h
.nearText(NearText.of(
List.of("sunshine", "butterflies"),
/* NearText.Builder */ nt -> nt
.distance(.4f)
.moveTo(.5f, /* NearText.Move.Builder */ to -> to.concepts("lullaby"))
)
.queryProperties("title", "lyrics")
.returnProperties("album", "author")
);
```
Notice how the type of each lambda argument can be automatically deduced from the methods' signatures. This allows the autocomplete to correctly suggest possible arguments, guiding you through the query API. The builder itself is "tucked" in the method's internals, so you needn't remember how to access or import it. What's more, the code reads a lot more like a query thanks to improved [locality](https://htmx.org/essays/locality-of-behaviour/). As you'll see in the examples below, you can also get creative with naming the lambda argument to act as hint for future readers.
In real-world programs there will be cases where you need to inject some control-flow statements in the query builder code. Consider an example of limiting the number of query results based on some external value, such as a URL query paramter. Lambda expressions are fully-fledged functions, so you could add a if-statement right in the middle of it:
```java
songs.query.hybrid("rainbow", h -> {
if (limitURL != null) {
h.limit(limitURL);
}
return h;
});
```
This may get out of hand quickly if complex logic is involved. Or you may simply prefer the standard Builder pattern. Whichever's the case, `client6` has got you covered. Tucked builders are public members of the classes they build, so they can be used directly.
```java
Hybrid.Builder builder = new Hybrid.Builder("rainbow");
if (limitURL != null) {
builder.limit(limitURL)
}
// more complex steps...
songs.query.hybrid(/* Hybrid */ builder.build());
```
Finally, if you need to separate "query definition" from "performing the query", most objects provide two static factories: one with only the required arguments and one with the required aruments and a tucked builder.
```java
Hybrid requiredOnly = Hybrid.of("rainbow");
Hybrid withOptional = Hybrid.of("rainbow", opt -> opt.limit(10));
```
### Connecting to a Weaviate instance
```java
WeaviateClient client = WeaviateClient.connectToCustom(
conn -> conn
.scheme("http")
.httpPort(8080).httpHost("localhost")
.grpcPort(50051).grpcHost("localhost")
.setHeader("X-Custom-Header", "Readme")
);
```
Shorthand methods for connecting to a local instance and a Weaviate Cloud cluster are available too:
```java
// Defaults to scheme=http, host=localhost, port=8080, grpcHost=locahost, grpcPort=50051
WeaviateClient local = WeaviateClient.connectToLocal(local -> local.port(9090));
// Always uses httpPort=443, grpcPort=443, httpHost == gprcHost == , and API Key authentication
WeaviateClient wcd = WeaviateClient.connectToWeaviateCloud("my-cluster-url.io", "my-api-key");
```
> [!TIP]
> The client holds a number of resources (HTTP connection pools, gRPC channel) which must be disposed of correclty then they are no longer needed.
> If the client's lifecycle is tied to that of your app, closing the client via `client.close()` is a good way to do that.
>
> Otherwise, use the client inside a [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) statement:
>
>```java
> try (final var client = new WeaviateClient(config)) {
> // Do something with the client
> }
> ```
> WeaviateClient will be automatically closed when execution exits the block.
#### Authentication
Weaviate supports several authentication methods:
| Method | Client API reference |
| ----------------------- | --------------------------------------------------------------- |
| API Key | `Authentication.apiKey("my-api-key")` |
| Resource owner password | `Authentication.resourceOwnerPassword("username", "password")` |
| Client credentials | `Authentication.clientCredentials("clientKey", "clientSecret")` |
| Existing bearer token | `Authentication.apiKey("access-token", "refresh-token", 900)` |
Pass the preferred authentication method as an argument to the connection builder to use it in the client:
```java
WeaviateClient.connectToCustom(
conn -> conn.authentication(Authentication.apiKey("my-api-key")
);
```
Follow the [documentation](https://docs.weaviate.io/deploy/configuration/authentication) for a detailed discussion.
### Collection management
```java
client.collections.create(
"Songs",
collection -> collection
.properties(
Property.text("title"),
Property.text("lyrics", p -> p.tokenization(Tokenization.WORD)),
Property.integer("yearReleased"),
Property.blob("albumCover"),
Property.bool("isSingle")
)
.references(
ReferenceProperty.to("hasAwards", "GrammyAwards", "BillboardMusicAwards")
)
.vectorConfig(
VectorConfig.text2vecWeaviate("title_vec", t2v -> t2v.sourceProperties("title")),
VectorConfig.text2vecWeaviate("lyrics_vec", t2v -> t2v.sourceProperties("lyrics")),
VectorConfig.img2vecNeural("cover_vec", i2v -> i2v.imageFields("albumCover"))
)
);
assert client.collections.exists("Songs");
client.collections.delete("Songs");
assert !client.collections.exists("Songs");
```
Other methods in `collections` namespace include:
- `getConfig(String collection)` to fetch collection configuration;
- `list()` to fetch collection configurations for all existing collections
- `deleteAll()` to drop all collections and their data
#### Using a Collection Handle
Once a collection is created, you can obtain another client object that's scoped to that collection, called a _"handle"_.
```java
CollectionHandle