Development

How to Connect To Weaviate Database Using Java

In this tutorial, we will learn how to connect a Java application to a Weaviate server and carry out operations on the server.
Captain Salem 2 min read
How to Connect To Weaviate Database Using Java

Weaviate is an open-source, decentralized knowledge graph system. It utilizes a graph database and vectorization to efficiently store and retrieve complex data. It supports semantic search, NLP, and machine learning tasks and can handle unstructured data.

Weaviate provides various tools and methods for connecting and interacting with the Weaviate server.

Requirements

Before proceeding with this tutorial, ensure you have the following:

  1. Java JDK Version 8.0 and above.
  2. A running instance of the Weaviate database.
  3. Basic Java programming knowledge.
  4. Maven installed on your machine.

Once installed, create a Java project that uses Maven as the package manager. We will use Maven to install the required packages on our project.

Installing the Weaviate Java Client

Once you have all the tools and your project configured, we can install the latest stable version of the Weaviate Java client. We can add it as a dependency in your pom.xml file.

<dependency>
<groupId>io.weaviate</groupId>
<artifactId>client</artifactId>
<version>4.0.0</version> <!-- Check latest version -->
</dependency>

Once installed, you can refresh your pom.xml and allow Maven to download all the required dependencies.

Connecting to Weaviate Server

Having everything set up, we can use the Weaviate library to create a connection to the Weaviate server and perform various actions.

The following code demonstrates using this library to connect to the Weaviate server.

package io.weaviate;

import io.weaviate.client.Config;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.base.Result;
import io.weaviate.client.v1.misc.model.Meta;

public class App {
  public static void main(String[] args) {
    Config config = new Config("http", "localhost:8080");
    WeaviateClient client = new WeaviateClient(config);
    Result<Meta> meta = client.misc().metaGetter().run();
    if (meta.getError() == null) {
      System.out.printf("meta.hostname: %s\n", meta.getResult().getHostname());
      System.out.printf("meta.version: %s\n", meta.getResult().getVersion());
      System.out.printf("meta.modules: %s\n", meta.getResult().getModules());
    } else {
      System.out.printf("Error: %s\n", meta.getError().getMessages());
    }
  }
}

The code above uses the Weaviate package to create a client that connects to the Weaviate server running on localhost and port 8080.

Once connected, we use the provided methods and modules to fetch information about the server, such as the hostname, version, modules, and more.

API Authentication

If your server is configured to authenticate via an API key, you can define the credentials in the configuration object as shown:

import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;

Config config = new Config("https", "some-endpoint.weaviate.network");
WeaviateClient client = WeaviateAuthClient.apiKey(config, "YOUR-WEAVIATE-API-KEY");

This should allow the client to authenticate to the Weaviate server and enable you to execute commands on the server.

Conclusion

This tutorial explored the basics of installing the Weaviate Java client in your project. We also explored the basics of using the Weaviate client to connect to the Weaviate server and gather information about the server.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.