|
1 | 1 | package io.qdrant.spark; |
2 | 2 |
|
3 | | -import io.qdrant.client.grpc.JsonWithInt.Value; |
4 | | -import io.qdrant.client.grpc.Points.PointId; |
5 | 3 | import io.qdrant.client.grpc.Points.PointStruct; |
6 | | -import io.qdrant.client.grpc.Points.Vectors; |
7 | 4 | import java.io.Serializable; |
8 | 5 | import java.net.URL; |
9 | 6 | import java.util.ArrayList; |
10 | | -import java.util.Map; |
| 7 | +import java.util.List; |
11 | 8 | import org.apache.spark.sql.catalyst.InternalRow; |
12 | 9 | import org.apache.spark.sql.connector.write.DataWriter; |
13 | 10 | import org.apache.spark.sql.connector.write.WriterCommitMessage; |
14 | 11 | import org.apache.spark.sql.types.StructType; |
15 | 12 | import org.slf4j.Logger; |
16 | 13 | import org.slf4j.LoggerFactory; |
17 | 14 |
|
18 | | -/** A DataWriter implementation that writes data to Qdrant. */ |
| 15 | +/** DataWriter implementation for writing data to Qdrant. */ |
19 | 16 | public class QdrantDataWriter implements DataWriter<InternalRow>, Serializable { |
| 17 | + |
| 18 | + private static final Logger LOG = LoggerFactory.getLogger(QdrantDataWriter.class); |
| 19 | + |
20 | 20 | private final QdrantOptions options; |
21 | 21 | private final StructType schema; |
22 | | - private final String qdrantUrl; |
23 | | - private final String apiKey; |
24 | | - private final Logger LOG = LoggerFactory.getLogger(QdrantDataWriter.class); |
25 | | - |
26 | | - private final ArrayList<PointStruct> points = new ArrayList<>(); |
| 22 | + private final List<PointStruct> pointsBuffer = new ArrayList<>(); |
27 | 23 |
|
28 | 24 | public QdrantDataWriter(QdrantOptions options, StructType schema) { |
29 | 25 | this.options = options; |
30 | 26 | this.schema = schema; |
31 | | - this.qdrantUrl = options.qdrantUrl; |
32 | | - this.apiKey = options.apiKey; |
33 | 27 | } |
34 | 28 |
|
35 | 29 | @Override |
36 | 30 | public void write(InternalRow record) { |
37 | | - PointStruct.Builder pointBuilder = PointStruct.newBuilder(); |
38 | | - |
39 | | - PointId pointId = QdrantPointIdHandler.preparePointId(record, this.schema, this.options); |
40 | | - pointBuilder.setId(pointId); |
41 | | - |
42 | | - Vectors vectors = QdrantVectorHandler.prepareVectors(record, this.schema, this.options); |
43 | | - pointBuilder.setVectors(vectors); |
44 | | - |
45 | | - Map<String, Value> payload = |
46 | | - QdrantPayloadHandler.preparePayload(record, this.schema, this.options); |
47 | | - pointBuilder.putAllPayload(payload); |
48 | | - |
49 | | - this.points.add(pointBuilder.build()); |
| 31 | + PointStruct point = createPointStruct(record); |
| 32 | + pointsBuffer.add(point); |
50 | 33 |
|
51 | | - if (this.points.size() >= this.options.batchSize) { |
52 | | - this.write(this.options.retries); |
| 34 | + if (pointsBuffer.size() >= options.batchSize) { |
| 35 | + writeBatch(options.retries); |
53 | 36 | } |
54 | 37 | } |
55 | 38 |
|
56 | | - @Override |
57 | | - public WriterCommitMessage commit() { |
58 | | - this.write(this.options.retries); |
59 | | - return new WriterCommitMessage() { |
60 | | - @Override |
61 | | - public String toString() { |
62 | | - return "point committed to Qdrant"; |
63 | | - } |
64 | | - }; |
| 39 | + private PointStruct createPointStruct(InternalRow record) { |
| 40 | + PointStruct.Builder pointBuilder = PointStruct.newBuilder(); |
| 41 | + pointBuilder.setId(QdrantPointIdHandler.preparePointId(record, schema, options)); |
| 42 | + pointBuilder.setVectors(QdrantVectorHandler.prepareVectors(record, schema, options)); |
| 43 | + pointBuilder.putAllPayload(QdrantPayloadHandler.preparePayload(record, schema, options)); |
| 44 | + return pointBuilder.build(); |
65 | 45 | } |
66 | 46 |
|
67 | | - public void write(int retries) { |
68 | | - LOG.info( |
69 | | - String.join( |
70 | | - "", "Uploading batch of ", Integer.toString(this.points.size()), " points to Qdrant")); |
71 | | - |
72 | | - if (this.points.isEmpty()) { |
| 47 | + private void writeBatch(int retries) { |
| 48 | + if (pointsBuffer.isEmpty()) { |
73 | 49 | return; |
74 | 50 | } |
| 51 | + |
75 | 52 | try { |
76 | | - // Instantiate a new QdrantGrpc object to maintain serializability |
77 | | - QdrantGrpc qdrant = new QdrantGrpc(new URL(this.qdrantUrl), this.apiKey); |
78 | | - qdrant.upsert(this.options.collectionName, this.points, this.options.shardKeySelector); |
79 | | - qdrant.close(); |
80 | | - this.points.clear(); |
| 53 | + doWriteBatch(); |
| 54 | + pointsBuffer.clear(); |
81 | 55 | } catch (Exception e) { |
82 | | - LOG.error(String.join("", "Exception while uploading batch to Qdrant: ", e.getMessage())); |
| 56 | + LOG.error("Exception while uploading batch to Qdrant: {}", e.getMessage()); |
83 | 57 | if (retries > 0) { |
84 | 58 | LOG.info("Retrying upload batch to Qdrant"); |
85 | | - write(retries - 1); |
| 59 | + writeBatch(retries - 1); |
86 | 60 | } else { |
87 | 61 | throw new RuntimeException(e); |
88 | 62 | } |
89 | 63 | } |
90 | 64 | } |
91 | 65 |
|
| 66 | + private void doWriteBatch() throws Exception { |
| 67 | + LOG.info("Uploading batch of {} points to Qdrant", pointsBuffer.size()); |
| 68 | + |
| 69 | + // Instantiate QdrantGrpc client for each batch to maintain serializability |
| 70 | + QdrantGrpc qdrant = new QdrantGrpc(new URL(options.qdrantUrl), options.apiKey); |
| 71 | + qdrant.upsert(options.collectionName, pointsBuffer, options.shardKeySelector); |
| 72 | + qdrant.close(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public WriterCommitMessage commit() { |
| 77 | + writeBatch(options.retries); |
| 78 | + return new WriterCommitMessage() { |
| 79 | + @Override |
| 80 | + public String toString() { |
| 81 | + return "point committed to Qdrant"; |
| 82 | + } |
| 83 | + }; |
| 84 | + } |
| 85 | + |
92 | 86 | @Override |
93 | 87 | public void abort() {} |
94 | 88 |
|
|
0 commit comments