Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ float[][] v = vectors.getMulti("custom_2d"); // 2-d vector with a custom name

#### Batch insert

> [!NOTE]
> Support for Dynamic Batching in `client6` will be added once Server-Side Batching becomes GA in Weaviate (est. `v1.34`)
`insertMany` sends all objects in a single request:

```java
InsertManyResponse response = songs.data.insertMany(
Expand All @@ -369,6 +368,59 @@ songs.data.insertMany(
)
```

#### Server-side batching

For large imports, use server-side batching, which is generally available since Weaviate `v1.36`. The client streams objects to Weaviate over one gRPC stream, and the server tells the client how fast to send based on its current load. You don't have to tune batch sizes or concurrency, and the client doesn't flood a busy server.

```java
try (BatchContext<Map<String, Object>> batch = songs.batch.start()) {
for (Map<String, Object> song : catalog) {
batch.add(WeaviateObject.of(o -> o.properties(song)));
}
} // close() returns once every object has a final result and the server has closed the stream
```

`add` returns a `TaskHandle`. Its `done()` future completes when Weaviate accepts the object, and completes exceptionally with the server's error when Weaviate rejects it. `batch.numberOfErrors()` counts the rejected objects.

```java
List<TaskHandle> handles = new ArrayList<>();
try (var batch = songs.batch.start()) {
for (Map<String, Object> song : catalog) {
handles.add(batch.add(WeaviateObject.of(o -> o.properties(song))));
}
}
for (TaskHandle handle : handles) {
try {
handle.done().get();
} catch (ExecutionException e) {
System.err.println("rejected: " + e.getCause().getMessage());
}
}
```

`start` takes optional settings, e.g. `songs.batch.start(b -> b.batchSize(200).maxReconnectRetries(5))`. The server still adjusts the pace from there. If the stream breaks, for example because a node restarts, the client reconnects and re-sends unacknowledged objects, so `close()` can take a while on an unstable cluster. Use `withTenant(...)` on the collection handle to batch into a tenant.

On Weaviate versions before `v1.36`, use `insertMany`.

If several batches write at once into a collection that auto-schema has to create, the server can reject some objects with `class name ... already exists`. Retry those objects once; by then the collection exists:

```java
RetryPolicy retryAutoSchemaRace = new RetryPolicy(1) {
@Override
protected boolean canRetryThrowable(Throwable t) {
return t instanceof ServerException
&& t.getMessage() != null
&& t.getMessage().contains("already exists");
}
};

try (var batch = songs.batch.start(b -> b.retryPolicy(retryAutoSchemaRace))) {
...
}
```

Creating the collection before the import avoids the race altogether.
Comment on lines +404 to +422

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If several batches write at once into a collection that auto-schema has to create, the server can reject some objects with `class name ... already exists`. Retry those objects once; by then the collection exists:
```java
RetryPolicy retryAutoSchemaRace = new RetryPolicy(1) {
@Override
protected boolean canRetryThrowable(Throwable t) {
return t instanceof ServerException
&& t.getMessage() != null
&& t.getMessage().contains("already exists");
}
};
try (var batch = songs.batch.start(b -> b.retryPolicy(retryAutoSchemaRace))) {
...
}
```
Creating the collection before the import avoids the race altogether.

IMO this is too much detail for the client's README. This kind of thing is best placed in Weaviate Docs.


### Querying data

Query methods are concentrated behind the `.query` namespace.
Expand Down
Loading