Hisoka-X commented on code in PR #4885:
URL: https://github.com/apache/seatunnel/pull/4885#discussion_r1303928601
##########
docs/en/connector-v2/sink/Milvus.md:
##########
@@ -0,0 +1,67 @@
+> Milvus sink connector
+
+## Support These Engines
+
+> Spark<br/>
+> Flink<br/>
+> SeaTunnel Zeta<br/>
+
+## Key Features
+
+- [ ] [exactly-once](../../concept/connector-v2-features.md)
+- [ ] [cdc](../../concept/connector-v2-features.md)
+
+## Description
+
+Write data to Apache milvus.
+
+## Sink Options
+
+| Name | Type | Required | Default |
Description |
+|-------------------|--------|----------|------------------------|-------------------------------------------------------------------------------|
+| milvus_host | String | Yes | - | The milvus
host. |
+| milvus_port | Int | No | 19530 | This port
is for gRPC. Default is 19530. |
+| username | String | Yes | - | The
username of milvus server. |
+| password | String | Yes | - | The
password of milvus server. |
+| collection_name | String | No | - | A
collection of milvus, which is similar to a table in a relational database. |
+| partition_field | String | No | - | Partition
fields, which must be included in the collection's schema. |
+| openai_engine | String | No | text-embedding-ada-002 | Text
embedding model. Default is 'text-embedding-ada-002'. |
+| openai_api_key | String | No | - | Use your
own Open AI API Key here. |
+| embeddings_fields | String | No | - | Fields to
be embedded,They use`,`for splitting. |
+
+### Data Type Mapping
+
+| Milvus Data type | SeaTunnel Data type |
+|------------------|---------------------|
+| Bool | BOOLEAN |
+| Int8 | TINYINT |
+| Int16 | SMALLINT |
+| Int32 | INT |
+| Int64 | BIGINT |
+| Float | FLOAT |
+| Double | DOUBLE |
+| VarChar | DECIMAL |
+| String | STRING |
+
+## Examples
+
+```hocon
+sink {
+ Milvus {
+ milvus_host = localhost
+ milvus_port = 19530
+ username = root
+ password = Milvus
+ collection_name = title_db
+ openai_engine = text-embedding-ada-002
+ openai_api_key = sk-xxxx
+ embeddings_fields = title_2
+ }
+```
Review Comment:
I think you should put a demo can run without any modify. Not just sink
config. It's more useful.
##########
seatunnel-connectors-v2/connector-milvus/src/main/java/org/apache/seatunnel/connectors/seatunnel/milvus/sink/MilvusSinkWriter.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.milvus.sink;
+
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import org.apache.seatunnel.connectors.seatunnel.milvus.config.MilvusOptions;
+import
org.apache.seatunnel.connectors.seatunnel.milvus.exception.MilvusConnectorException;
+
+import com.theokanning.openai.embedding.EmbeddingRequest;
+import com.theokanning.openai.embedding.EmbeddingResult;
+import com.theokanning.openai.service.OpenAiService;
+import io.milvus.client.MilvusServiceClient;
+import io.milvus.grpc.DataType;
+import io.milvus.grpc.DescribeCollectionResponse;
+import io.milvus.param.ConnectParam;
+import io.milvus.param.R;
+import io.milvus.param.collection.DescribeCollectionParam;
+import io.milvus.param.collection.FieldType;
+import io.milvus.param.collection.FlushParam;
+import io.milvus.param.collection.HasCollectionParam;
+import io.milvus.param.dml.InsertParam;
+import io.milvus.response.DescCollResponseWrapper;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static
org.apache.seatunnel.common.exception.CommonErrorCode.ILLEGAL_ARGUMENT;
+import static
org.apache.seatunnel.common.exception.CommonErrorCode.UNSUPPORTED_DATA_TYPE;
+import static
org.apache.seatunnel.connectors.seatunnel.milvus.exception.MilvusConnectorErrorCode.RESPONSE_FAILED;
+
+public class MilvusSinkWriter extends AbstractSinkWriter<SeaTunnelRow, Void> {
+
+ private final MilvusServiceClient milvusClient;
+
+ private final MilvusOptions milvusOptions;
+
+ private OpenAiService service;
+
+ private final List<FieldType> metaFields;
+
+ public MilvusSinkWriter(MilvusOptions milvusOptions) {
+ this.milvusOptions = milvusOptions;
+ ConnectParam connectParam =
+ ConnectParam.newBuilder()
+ .withHost(milvusOptions.getMilvusHost())
+ .withPort(milvusOptions.getMilvusPort())
+ .withAuthorization(milvusOptions.getUserName(),
milvusOptions.getPassword())
+ .build();
+ milvusClient = new MilvusServiceClient(connectParam);
+
+ handleResponseStatus(
+ milvusClient.hasCollection(
+ HasCollectionParam.newBuilder()
+
.withCollectionName(milvusOptions.getCollectionName())
+ .build()));
+
+ R<DescribeCollectionResponse> describeCollectionResponseR =
+ milvusClient.describeCollection(
+ DescribeCollectionParam.newBuilder()
+
.withCollectionName(milvusOptions.getCollectionName())
+ .build());
+
+ handleResponseStatus(describeCollectionResponseR);
+
+ DescCollResponseWrapper wrapper =
+ new
DescCollResponseWrapper(describeCollectionResponseR.getData());
+
+ this.metaFields = wrapper.getFields();
+
+ if (milvusOptions.getEmbeddingsFields() != null) {
+ service = new OpenAiService(milvusOptions.getOpenaiApiKey());
+ }
+ }
+
+ @Override
+ public void write(SeaTunnelRow element) throws IOException {
+
+ List<InsertParam.Field> fields = new ArrayList<>();
+
+ InsertParam.Builder builder = InsertParam.newBuilder();
+
+ builder =
builder.withCollectionName(milvusOptions.getCollectionName());
+
+ for (int i = 0; i < this.metaFields.size(); i++) {
+
+ FieldType fieldType = this.metaFields.get(i);
+
+ if (fieldType.isPrimaryKey()) {
+ if (!(element.getField(i) instanceof Long)
+ && !(element.getField(i) instanceof Integer)
+ && !(element.getField(i) instanceof Byte)
+ && !(element.getField(i) instanceof Short)
+ && !(element.getField(i) instanceof String)) {
Review Comment:
How about use `switch`?
##########
seatunnel-connectors-v2/connector-milvus/src/main/java/org/apache/seatunnel/connectors/seatunnel/milvus/sink/MilvusSink.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.milvus.sink;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import org.apache.seatunnel.api.common.PrepareFailException;
+import org.apache.seatunnel.api.common.SeaTunnelAPIErrorCode;
+import org.apache.seatunnel.api.sink.SeaTunnelSink;
+import org.apache.seatunnel.api.sink.SinkWriter;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.config.CheckConfigUtil;
+import org.apache.seatunnel.common.config.CheckResult;
+import org.apache.seatunnel.common.constants.PluginType;
+import
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSimpleSink;
+import
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import org.apache.seatunnel.connectors.seatunnel.milvus.config.MilvusConfig;
+import org.apache.seatunnel.connectors.seatunnel.milvus.config.MilvusOptions;
+import
org.apache.seatunnel.connectors.seatunnel.milvus.exception.MilvusConnectorException;
+
+import com.google.auto.service.AutoService;
+
+import java.io.IOException;
+
+@AutoService(SeaTunnelSink.class)
+public class MilvusSink extends AbstractSimpleSink<SeaTunnelRow, Void> {
+
+ private SeaTunnelRowType seaTunnelRowType;
+
+ private MilvusOptions milvusOptions;
+
+ @Override
+ public String getPluginName() {
+ return "Milvus";
+ }
+
+ @Override
+ public void prepare(Config pluginConfig) throws PrepareFailException {
+ milvusOptions = new MilvusOptions(pluginConfig);
+ CheckResult result =
+ CheckConfigUtil.checkAllExists(
+ pluginConfig,
+ MilvusConfig.MILVUS_HOST.key(),
+ MilvusConfig.MILVUS_PORT.key(),
+ MilvusConfig.COLLECTION_NAME.key());
Review Comment:
Use OptionRule to check config is valid or not.
eg:
```java
ConfigValidator.of(config).validate(new JdbcSourceFactory().optionRule());
```
##########
seatunnel-connectors-v2/connector-milvus/src/main/java/org/apache/seatunnel/connectors/seatunnel/milvus/config/MilvusOptions.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.milvus.config;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class MilvusOptions implements Serializable {
+
+ private String milvusHost;
+ private Integer milvusPort;
+ private String collectionName;
+ private String partitionField;
+ private String userName;
+ private String password;
+ private String openaiEngine;
+ private String openaiApiKey;
+ private String embeddingsFields;
+
+ public MilvusOptions(Config config) {
+ this.milvusHost = config.getString(MilvusConfig.MILVUS_HOST.key());
+ if (config.hasPath(MilvusConfig.MILVUS_PORT.key())) {
+ this.milvusPort = config.getInt(MilvusConfig.MILVUS_PORT.key());
+ } else {
+ this.milvusPort = MilvusConfig.MILVUS_PORT.defaultValue();
+ }
+ this.collectionName =
config.getString(MilvusConfig.COLLECTION_NAME.key());
+ this.userName = config.getString(MilvusConfig.USERNAME.key());
+ this.password = config.getString(MilvusConfig.PASSWORD.key());
+
+ if (config.hasPath(MilvusConfig.PARTITION_FIELD.key())) {
+ this.partitionField =
config.getString(MilvusConfig.PARTITION_FIELD.key());
+ }
+ if (config.hasPath(MilvusConfig.OPENAI_ENGINE.key())) {
+ this.openaiEngine =
config.getString(MilvusConfig.OPENAI_ENGINE.key());
+ } else {
+ this.openaiEngine = MilvusConfig.OPENAI_ENGINE.defaultValue();
+ }
+ if (config.hasPath(MilvusConfig.OPENAI_API_KEY.key())) {
+ this.openaiApiKey =
config.getString(MilvusConfig.OPENAI_API_KEY.key());
+ }
+ if (config.hasPath(MilvusConfig.EMBEDDINGS_FIELDS.key())) {
+ this.embeddingsFields =
config.getString(MilvusConfig.EMBEDDINGS_FIELDS.key());
+ }
+ }
Review Comment:
You can use `ReadonlyConfig readonlyConfig =
ReadonlyConfig.fromConfig(config);`. Then just use
`readonlyConfig.get(MilvusConfig.MILVUS_HOST)`, it will hande default value.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]