Milvus
Since Camel 4.5
Only producer is supported
The Milvus Component provides support for interacting with the Milvus Vector Database.
URI format
milvus:collection[?options]
Where collection represents a named set of points (vectors with a payload) defined in your database.
The action option of the endpoint, or the CamelMilvusAction header, selects the action, such as milvus:myCollection?action=SEARCH. The header, when set, wins over the option, so one endpoint can also run several actions. Without either, the producer fails.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Milvus component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The action to perform. The CamelMilvusAction header, when set, wins over this option. Enum values:
| MilvusAction | ||
The configuration that the endpoints start from. Each endpoint gets a copy, and its options replace the values of the copy. It also holds the MilvusClient that MilvusConfiguration.setClient() sets, which the endpoints share. | MilvusConfiguration | ||
The host name or IP address of the Milvus server. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | localhost | String | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The gRPC port of the Milvus server. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | 19530 | int | |
The timeout in milliseconds for connecting to the Milvus server, 10000 if not set. The value must be greater than 0. It is not a timeout of each request: the requests have no deadline. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | long | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
The path of a PEM file with the certificate authority that the client trusts for mutual TLS. Set it together with clientPemPath and clientKeyPath; for one-way TLS use serverPemPath instead. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The path of a PEM file with the private key (PKCS#8) of the client for mutual TLS. Set it together with caPemPath and clientPemPath. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The path of a PEM file with the certificate of the client for mutual TLS. Set it together with caPemPath and clientKeyPath. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
Whether to connect to the Milvus server with TLS. If not set, the connection is plaintext, and the token is sent in clear text, unless serverPemPath, or caPemPath with clientPemPath and clientKeyPath, is set. Without these options, the client trusts the certificate authorities of the JVM. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | false | boolean | |
The name that the client expects in the certificate of the Milvus server (the authority of the gRPC channel), when it differs from the host. Only used with serverPemPath, where the host is used if not set, or with mutual TLS. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The path of a PEM file with the certificate of the Milvus server, or of the certificate authority that signed it, that the client trusts for one-way TLS. Setting it connects with TLS. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The token that the client sends to authenticate with Milvus: a user name and password joined by a colon, or an API key of Zilliz Cloud. If not set, the client sends no credentials. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String |
Endpoint Options
The Milvus endpoint is configured using URI syntax:
milvus:collection
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The action to perform. The CamelMilvusAction header, when set, wins over this option. Enum values:
| MilvusAction | ||
The host name or IP address of the Milvus server. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | localhost | String | |
The gRPC port of the Milvus server. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | 19530 | int | |
The timeout in milliseconds for connecting to the Milvus server, 10000 if not set. The value must be greater than 0. It is not a timeout of each request: the requests have no deadline. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | long | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The path of a PEM file with the certificate authority that the client trusts for mutual TLS. Set it together with clientPemPath and clientKeyPath; for one-way TLS use serverPemPath instead. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The path of a PEM file with the private key (PKCS#8) of the client for mutual TLS. Set it together with caPemPath and clientPemPath. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The path of a PEM file with the certificate of the client for mutual TLS. Set it together with caPemPath and clientKeyPath. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
Whether to connect to the Milvus server with TLS. If not set, the connection is plaintext, and the token is sent in clear text, unless serverPemPath, or caPemPath with clientPemPath and clientKeyPath, is set. Without these options, the client trusts the certificate authorities of the JVM. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | false | boolean | |
The name that the client expects in the certificate of the Milvus server (the authority of the gRPC channel), when it differs from the host. Only used with serverPemPath, where the host is used if not set, or with mutual TLS. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The path of a PEM file with the certificate of the Milvus server, or of the certificate authority that signed it, that the client trusts for one-way TLS. Setting it connects with TLS. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String | ||
The token that the client sends to authenticate with Milvus: a user name and password joined by a colon, or an API key of Zilliz Cloud. If not set, the client sends no credentials. Not used when a MilvusClient is set with MilvusConfiguration.setClient(). | String |
Message Headers
The Milvus component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The action that the producer performs. It wins over the action option. The milvus:embeddings transformer reads it too, and accepts only INSERT and UPSERT. Enum values:
| String | |
CamelMilvusOperationStatus (producer) Constant: | The status code of the response of Milvus. The producer sets it only after an action that succeeded, so it is always 0. When Milvus returns another code, the producer fails the exchange. | String | |
CamelMilvusOperationStatusValue (producer) Constant: | The status code of the response of Milvus as an int, the same value as the CamelMilvusOperationStatus header. | int | |
CamelMilvusTextFieldName (producer) Constant: | The name of the field for the text of the TextSegment, in the InsertParam or UpsertParam that the milvus:embeddings transformer builds. The default is text. | String | |
CamelMilvusVectorFieldName (producer) Constant: | The name of the field for the embedding vector, in the InsertParam or UpsertParam that the milvus:embeddings transformer builds. The default is vector. | String | |
CamelMilvusCollectionName (producer) Constant: | The name of the collection, in the InsertParam or UpsertParam that the milvus:embeddings transformer builds. The default is embeddings: the transformer does not use the collection of the endpoint. | String | |
| Constant: | The name of the primary key field, in the InsertParam or UpsertParam that the milvus:embeddings transformer builds. The default is id. The transformer adds the key field only when the CamelMilvusKeyValue header is set. | String | |
CamelMilvusKeyValue (producer) Constant: | The value of the primary key, in the InsertParam or UpsertParam that the milvus:embeddings transformer builds. If not set, the transformer adds no key field, as for a collection whose primary key has autoID enabled. | String |
Message Body
The message body at each side of the Milvus component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The request of the Milvus SDK for the action in the action option or the CamelMilvusAction header: a CreateCollectionParam, CreateIndexParam, InsertParam, UpsertParam, SearchSimpleParam, QueryParam or DeleteParam. Another body fails, a vector as a List too: the milvus:embeddings data type transformer builds the InsertParam or UpsertParam from the output of langchain4j-embeddings.
|
Producer output |
| The result of the Milvus SDK for the action: an RpcStatus for CREATE_COLLECTION and CREATE_INDEX, a MutationResult for INSERT, UPSERT and DELETE, a SearchResponse for SEARCH and a QueryResults for QUERY.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
TLS
By default, the component connects to Milvus over plaintext gRPC, and the token is sent in clear text. The following options of the endpoint (or of the component configuration) connect with TLS:
| Options | Connection |
|---|---|
| TLS, trusting the certificate authorities of the JVM, such as a Zilliz Cloud endpoint. |
| One-way TLS, trusting the server certificate (or the certificate authority that signed it) in the PEM file. |
| Mutual TLS: the client trusts the certificate authority in |
The file options connect with TLS also when secure is not set. The component fails at startup when only some of caPemPath, clientPemPath and clientKeyPath are set, when serverPemPath is set together with them, or when serverName is set without them or serverPemPath.
from("direct:search")
.to("milvus:myCollection?action=SEARCH&host=milvus.example.com&secure=true"
+ "&serverPemPath=/etc/milvus/certs/server.pem&token=RAW({{milvus.token}})"); The Milvus SDK reads the certificates and the key from files and builds its own SSL context, so the component does not support sslContextParameters. For other TLS settings, create a MilvusServiceClient with your own ConnectParam and set it with MilvusConfiguration.setClient(): the TLS options are then not used.
Examples
Collection Examples
In the route below, we use the milvus component to create a collection named test with the given parameters:
-
Java
FieldType fieldType1 = FieldType.newBuilder()
.withName("userID")
.withDescription("user identification")
.withDataType(DataType.Int64)
.withPrimaryKey(true)
.withAutoID(true)
.build();
FieldType fieldType2 = FieldType.newBuilder()
.withName("userFace")
.withDescription("face embedding")
.withDataType(DataType.FloatVector)
.withDimension(64)
.build();
FieldType fieldType3 = FieldType.newBuilder()
.withName("userAge")
.withDescription("user age")
.withDataType(DataType.Int8)
.build();
from("direct:in")
.setHeader(Milvus.Headers.ACTION)
.constant(MilvusAction.CREATE_COLLECTION)
.setBody()
.constant(
CreateCollectionParam.newBuilder()
.withCollectionName("test")
.withDescription("customer info")
.withShardsNum(2)
.withEnableDynamicField(false)
.addFieldType(fieldType1)
.addFieldType(fieldType2)
.addFieldType(fieldType3)
.build())
.to("milvus:test"); Points Examples
Insert
In the route below we use the milvus component to perform insert on points in the collection named test:
-
Java
private List<List<Float>> generateFloatVectors(int count) {
Random ran = new Random();
List<List<Float>> vectors = new ArrayList<>();
for (int n = 0; n < count; ++n) {
List<Float> vector = new ArrayList<>();
for (int i = 0; i < 64; ++i) {
vector.add(ran.nextFloat());
}
vectors.add(vector);
}
return vectors;
}
Random ran = new Random();
List<Integer> ages = new ArrayList<>();
for (long i = 0L; i < 2; ++i) {
ages.add(ran.nextInt(99));
}
List<InsertParam.Field> fields = new ArrayList<>();
fields.add(new InsertParam.Field("userAge", ages));
fields.add(new InsertParam.Field("userFace", generateFloatVectors(2)));
from("direct:in")
.setHeader(Milvus.Headers.ACTION)
.constant(MilvusAction.INSERT)
.setBody()
.constant(
InsertParam.newBuilder()
.withCollectionName("test")
.withFields(fields)
.build())
.to("milvus:test"); Search
In the route below, we use the milvus component to retrieve information by query from the collection named test:
-
Java
private List<Float> generateFloatVector() {
Random ran = new Random();
List<Float> vector = new ArrayList<>();
for (int i = 0; i < 64; ++i) {
vector.add(ran.nextFloat());
}
return vector;
}
from("direct:in")
.setHeader(Milvus.Headers.ACTION)
.constant(MilvusAction.SEARCH)
.setBody()
.constant(SearchSimpleParam.newBuilder()
.withCollectionName("test")
.withVectors(generateFloatVector())
.withFilter("userAge>0")
.withLimit(100L)
.withOffset(0L)
.withOutputFields(Lists.newArrayList("userAge"))
.withConsistencyLevel(ConsistencyLevelEnum.STRONG)
.build())
.to("milvus:myCollection"); Relation with Langchain4j-Embeddings component
The Milvus component provides a datatype transformer, from langchain4j-embeddings to an insert/upsert object compatible with Milvus.
As an example, you could think about these routes:
-
Java
-
YAML
protected RoutesBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:in")
.to("langchain4j-embeddings:test")
.setHeader(Milvus.Headers.ACTION).constant(MilvusAction.INSERT)
.setHeader(Milvus.Headers.KEY_NAME).constant("userID")
.setHeader(Milvus.Headers.KEY_VALUE).constant(Long.valueOf("3"))
.transformDataType(new org.apache.camel.spi.DataType("milvus:embeddings"))
.to(MILVUS_URI);
from("direct:up")
.to("langchain4j-embeddings:test")
.setHeader(Milvus.Headers.ACTION).constant(MilvusAction.UPSERT)
.setHeader(Milvus.Headers.KEY_NAME).constant("userID")
.setHeader(Milvus.Headers.KEY_VALUE).constant(Long.valueOf("3"))
.transformDataType(new org.apache.camel.spi.DataType("milvus:embeddings"))
.to(MILVUS_URI);
}
};
} - route:
from:
uri: direct:in
steps:
- to:
uri: langchain4j-embeddings:test
- setHeader:
name: CamelMilvusAction
expression:
constant:
expression: INSERT
- setHeader:
name: CamelMilvusKeyName
expression:
constant:
expression: userID
- setHeader:
name: CamelMilvusKeyValue
expression:
constant:
expression: "3"
resultType: java.lang.Long
- transformDataType:
toType: milvus:embeddings
- to:
uri: milvus:embeddings
- route:
from:
uri: direct:up
steps:
- to:
uri: langchain4j-embeddings:test
- setHeader:
name: CamelMilvusAction
expression:
constant:
expression: UPSERT
- setHeader:
name: CamelMilvusKeyName
expression:
constant:
expression: userID
- setHeader:
name: CamelMilvusKeyValue
expression:
constant:
expression: "3"
resultType: java.lang.Long
- transformDataType:
toType: milvus:embeddings
- to:
uri: milvus:embeddings It’s important to note that Milvus SDK doesn’t support upsert for autoID fields. This means if you set a field as key, and you set the autoID to true, the upsert won’t be possible.
That’s the reason why, in the example, we are setting the userID as keyName with a keyValue of 3. This is particularly important when you design your Milvus database.
The transformer only supports insert/upsert objects, so the only operation you can set via header are INSERT and UPSERT, otherwise the transformer will fail with an error log. The transformer runs before the endpoint, so it reads the CamelMilvusAction header only, not the action option of the endpoint.