# Spring Boot with Qdrant for Vector Database
In recent years, the rise of machine learning and deep learning has made vector databases increasingly important for managing high-dimensional data. Qdrant, a high-performance vector database, is designed to store, query, and manage embeddings generated by machine learning models. Pairing Qdrant with Spring Boot, a Java-based microservices framework, can enable developers to build powerful applications for similarity search, recommendation systems, and other AI-driven workflows.
This blog will guide you through setting up Spring Boot with Qdrant, explaining the underlying concepts, and providing sample code to integrate Qdrant into a Spring Boot application.
—
## What is Qdrant?
Qdrant is an open-source vector database optimized for storing and searching high-dimensional vectors. It is designed to handle machine learning embeddings efficiently and provides features such as:
– **Scalable Vector Search**: Perform fast similarity searches across millions of vectors.
– **Filtering**: Combine vector similarity with metadata-based filtering.
– **High Availability**: Supports clustering for fault tolerance and scalability.
– **Easy Integration**: Qdrant offers REST and gRPC APIs for seamless integration.
—
## Why Use Qdrant with Spring Boot?
Spring Boot is a popular framework for building microservices and web applications. Integrating Qdrant into a Spring Boot application allows developers to leverage the benefits of vector search while maintaining the flexibility and scalability of the Spring ecosystem. Some use cases include:
1. **Recommendation Systems**: Suggest products, movies, or articles based on similarity in embedding space.
2. **Image Search**: Find visually similar images using deep learning-generated embeddings.
3. **Natural Language Processing (NLP)**: Perform semantic searches on text data.
—
## Setting Up Qdrant and Spring Boot
### Step 1: Install and Run Qdrant
You can install Qdrant using Docker for quick setup:
“`bash
docker run -p 6333:6333 qdrant/qdrant
“`
By default, Qdrant runs on port `6333` and exposes a REST API for communication.
—
### Step 2: Create a Spring Boot Application
Use Spring Initializr (https://start.spring.io/) to bootstrap your Spring Boot application with the following dependencies:
– **Spring Web**: For building REST APIs.
– **Spring Boot Starter**: Core Spring Boot features.
– **Jackson**: For JSON serialization and deserialization.
Once the project is created, open the `application.properties` file and configure the `qdrant.url`:
“`properties
qdrant.url=http://localhost:6333
“`
—
### Step 3: Define a Qdrant Client in Spring Boot
To interact with Qdrant, we can create a custom `QdrantClient` using Spring’s `RestTemplate`. Below is an example implementation:
“`java
package com.example.qdrantdemo.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class QdrantClient {
@Value(“${qdrant.url}”)
private String qdrantUrl;
private final RestTemplate restTemplate = new RestTemplate();
public String createCollection(String collectionName) {
String endpoint = qdrantUrl + “/collections/” + collectionName;
String payload = “{\”vector_size\”: 128, \”distance\”: \”Cosine\”}”;
return restTemplate.postForObject(endpoint, payload, String.class);
}
public String addVector(String collectionName, String vectorId, double[] vector) {
String endpoint = qdrantUrl + “/collections/” + collectionName + “/points”;
String payload = String.format(
“{\”points\”: [{\”id\”: \”%s\”, \”vector\”: %s}]}”,
vectorId,
vectorToJson(vector)
);
return restTemplate.postForObject(endpoint, payload, String.class);
}
private String vectorToJson(double[] vector) {
StringBuilder sb = new StringBuilder(“[“);
for (int i = 0; i < vector.length; i++) {
sb.append(vector[i]);
if (i < vector.length – 1) {
sb.append(“,”);
}
}
sb.append(“]”);
return sb.toString();
}
}
“`
—
### Step 4: Integrate Qdrant with REST API
Create a Spring Boot controller to expose endpoints for interacting with Qdrant. Here’s an example:
“`java
package com.example.qdrantdemo.controller;
import com.example.qdrantdemo.client.QdrantClient;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(“/api/qdrant”)
public class QdrantController {
private final QdrantClient qdrantClient;
public QdrantController(QdrantClient qdrantClient) {
this.qdrantClient = qdrantClient;
}
@PostMapping(“/collections/{name}”)
public String createCollection(@PathVariable String name) {
return qdrantClient.createCollection(name);
}
@PostMapping(“/collections/{name}/points”)
public String addVector(
@PathVariable String name,
@RequestParam String id,
@RequestBody double[] vector
) {
return qdrantClient.addVector(name, id, vector);
}
}
“`
—
### Step 5: Test the Integration
Run your Spring Boot application and use tools like Postman or curl to test the endpoints.
**Create a Collection**:
“`bash
curl -X POST http://localhost:8080/api/qdrant/collections/my_collection
“`
**Add a Vector**:
“`bash
curl -X POST \
http://localhost:8080/api/qdrant/collections/my_collection/points \