Quellcode durchsuchen

fake-result-generate fc

dengjia vor 1 Monat
Ursprung
Commit
ad24630c66

+ 1 - 0
fake-feature-extract/src/main/java/com/yaoyicloud/ai/pipeline/fc/FakeFeatureExtractHandler.java

@@ -14,6 +14,7 @@ public class FakeFeatureExtractHandler implements PojoRequestHandler<TaskContent
 		System.out.println("Fake Features, Create Real Feature Extract FC to replace this in test channel or prod");
 		return new TaskContentMsgsWithFeature(
 			request.getItems(),
+			request.getIndexField(),
 			request.getItems().stream()
 				.map(msg -> IntStream.range(0, msg.getImgList().size()).mapToObj(i -> "base64_" + i).collect(Collectors.toList()))
 				.flatMap(List::stream)

+ 2 - 1
fake-feature-extract/src/test/java/com/yaoyicloud/ai/pipeline/fc/FakeFeatureExtractHandlerTest.java

@@ -29,7 +29,8 @@ public class FakeFeatureExtractHandlerTest {
     @Test
     public void testHandleRequest() {
         TaskContentMsgs request = new TaskContentMsgs(
-            List.of(new TaskContentMsgs.Message("appId1", "taskId1", "userId1", List.of("ossFile1", "ossFile2")))
+            List.of(new TaskContentMsgs.Message("appId1", "taskId1", "userId1", List.of("ossFile1", "ossFile2"))),
+            "indexField"
         );
 
         TaskContentMsgsWithFeature response = handler.handleRequest(request, context);

+ 84 - 0
fake-result-generate/README.md

@@ -0,0 +1,84 @@
+# java11 POJO 事件回调示例程序
+本示例为您展示了 Java runtime 的 POJO 事件回调示例程序。
+
+
+## 准备开始
+- [可选] 安装并配置 Serverless Devs 工具。(https://help.aliyun.com/document_detail/195474.html)
+
+## 快速开始
+### 方式一、使用 Serverless Devs 工具编译部署
+
+#### 1. 修改 s.yaml 配置
+- 根据需要修改 access 配置
+
+#### 2. 安装依赖并部署
+
+编译部署代码包
+```shell
+s deploy
+```
+> 注意: `pom.xml` 中有配置 `pre-deploy` 脚本 `mvn package`, 在部署前会调用 `mvn package` 编译打包。
+
+#### 3. 调用测试
+
+```shell
+s invoke --event-file event.json
+```
+
+调用函数时收到的响应如下所示:
+
+```bash
+
+Reading event file content:
+{
+  "firstName": "FC",
+  "lastName": "aliyun"
+}
+========= FC invoke Logs begin =========
+FC Invoke Start RequestId: fa3e5ad1-55dd-48ad-9d28-xxxxxxxxx
+FC Invoke End RequestId: fa3e5ad1-55dd-48ad-9d28-xxxxxxxxx
+
+Duration: 311.60 ms, Billed Duration: 312 ms, Memory Size: 128 MB, Max Memory Used: 97.50 MB
+========= FC invoke Logs end =========
+
+FC Invoke Result:
+{"message":"Hello, FC aliyun"}
+
+
+End of method: invoke
+```
+
+### 方式二、使用控制台创建
+
+#### 1. 编译打包
+```shell
+# 编译部署
+mvn package
+# 打包文件
+cd target && zip -r java11-pojo-event-blank.zip *
+```
+#### 2. 创建函数
+选择服务(或创建服务)后,单击创建函数,如图所示
+- 选择 `从零开始创建`
+- 填入函数名称
+- 选择运行环境 java11/java8
+- 选择函数触发方式:通过事件请求触发
+- [请求处理程序](https://help.aliyun.com/document_detail/432663.html)(函数入口)设为:example.PojoHandler::handleRequest
+- 其他设置使用默认
+
+![img_1.png](assets/20220608155705.jpg)
+
+> 详细创建函数流程见文档: [使用控制台创建函数](hhttps://help.aliyun.com/document_detail/51783.html)
+
+
+#### 3. 配置测试参数
+在函数管理页面的测试函数标签栏配置测试参数
+
+![img_2.png](assets/20220608155906.jpg)
+
+#### 4. 测试函数
+
+返回结果如下所示
+```bash
+{"message":"Hello, FC aliyun"}
+```

+ 21 - 0
fake-result-generate/event.json

@@ -0,0 +1,21 @@
+{
+  "items": [
+    {
+      "userId": "testUser1",
+      "imgList": [
+        "oss://yyc-algo-test/image-dup-check/multi-modal-embedding-img-search/1.jpg"
+      ],
+      "appId": "appId1",
+      "taskId": "testTask1"
+    },
+    {
+      "imgList": [
+        "oss://yyc-algo-test/image-dup-check/multi-modal-embedding-img-search/2.jpg"
+      ],
+      "appId": "appId1",
+      "taskId": "testTask1",
+      "userId": "testUser1"
+    }
+  ],
+  "indexField": "index1"
+}

+ 92 - 0
fake-result-generate/pom.xml

@@ -0,0 +1,92 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>yyc-ai</groupId>
+  <artifactId>fake-result-generate</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>FakeResultGenerate</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>com.yaoyicloud.ai.pipeline</groupId>
+      <artifactId>yyc-fc-api</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.13.2</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>4.11.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.aliyun.fc.runtime</groupId>
+      <artifactId>fc-java-core</artifactId>
+      <version>1.4.1</version>
+    </dependency>
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok</artifactId>
+      <version>1.18.30</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.8.1</version>
+        <configuration>
+          <source>11</source>
+          <target>11</target>
+          <annotationProcessorPaths>
+            <path>
+              <groupId>org.projectlombok</groupId>
+              <artifactId>lombok</artifactId>
+              <version>1.18.30</version>
+            </path>
+          </annotationProcessorPaths>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>3.2.1</version>
+        <executions>
+          <execution>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <filters>
+                <filter>
+                  <artifact>*:*</artifact>
+                  <excludes>
+                    <exclude>META-INF/*.SF</exclude>
+                    <exclude>META-INF/*.DSA</exclude>
+                    <exclude>META-INF/*.RSA</exclude>
+                  </excludes>
+                </filter>
+              </filters>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <properties>
+    <maven.compiler.target>11</maven.compiler.target>
+    <maven.compiler.source>11</maven.compiler.source>
+    <maven.test.skip>false</maven.test.skip>
+  </properties>
+</project>

+ 18 - 0
fake-result-generate/src/main/java/com/yaoyicloud/ai/pipeline/fc/FakeResultGenerateHandler.java

@@ -0,0 +1,18 @@
+package com.yaoyicloud.ai.pipeline.fc;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import com.aliyun.fc.runtime.Context;
+import com.aliyun.fc.runtime.PojoRequestHandler;
+
+public class FakeResultGenerateHandler implements PojoRequestHandler<TaskContentMsgs, TaskContentMsgs> {
+
+	@Override
+	public TaskContentMsgs handleRequest(TaskContentMsgs request, Context context) {
+		System.out.println("query search db to check duplication and save to algo service");
+		return request;
+	}
+
+}

+ 41 - 0
fake-result-generate/src/test/java/com/yaoyicloud/ai/pipeline/fc/FakeResultGenerateHandlerTest.java

@@ -0,0 +1,41 @@
+package com.yaoyicloud.ai.pipeline.fc;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import com.aliyun.fc.runtime.Context;
+
+@RunWith(MockitoJUnitRunner.class)
+public class FakeResultGenerateHandlerTest {
+
+    @Mock
+    private Context context;
+
+    private FakeResultGenerateHandler handler;
+
+    @Before
+    public void setUp() {
+        handler = new FakeResultGenerateHandler();
+    }
+
+    @Test
+    public void testHandleRequest() {
+        TaskContentMsgs request = new TaskContentMsgs(
+            List.of(new TaskContentMsgs.Message("appId1", "taskId1", "userId1", List.of("ossFile1", "ossFile2"))),
+            "indexField"
+        );
+
+        TaskContentMsgs response = handler.handleRequest(request, context);
+
+        assertNotNull(response);
+    }
+
+}

+ 2 - 1
mimic-real-msg/src/main/java/com/yaoyicloud/ai/pipeline/fc/MimicRealMsgHandler.java

@@ -14,7 +14,8 @@ public class MimicRealMsgHandler implements PojoRequestHandler<OssTestDataSet, T
 		return new TaskContentMsgs(
 			request.getOssFileList().stream()
 				.map(ossFile -> new TaskContentMsgs.Message("appId1", "testTask1", "testUser1", List.of(ossFile)))
-				.collect(Collectors.toList()));
+				.collect(Collectors.toList()),
+			null);
 	}
 
 }

+ 1 - 0
pom.xml

@@ -33,6 +33,7 @@
 
     <!-- YYC的算法测试云函数-->
     <module>fake-feature-extract</module>
+    <module>fake-result-generate</module>
   </modules>
 
   <properties>

+ 3 - 1
pre-process/src/main/java/com/yaoyicloud/ai/pipeline/fc/PreProcessHandler.java

@@ -9,7 +9,9 @@ public class PreProcessHandler implements PojoRequestHandler<TaskContentMsgsWith
 	public TaskContentMsgs handleRequest(TaskContentMsgsWithFeature request, Context context) {
 		System.out.println("write to search db");
 		return new TaskContentMsgs(
-			request.getItems());
+			request.getItems(),
+			request.getIndexField()
+		);
 	}
 
 }

+ 1 - 0
pre-process/src/test/java/com/yaoyicloud/ai/pipeline/fc/PreProcessHandlerTest.java

@@ -30,6 +30,7 @@ public class PreProcessHandlerTest {
     public void testHandleRequest() {
         TaskContentMsgsWithFeature request = new TaskContentMsgsWithFeature(
             List.of(new TaskContentMsgs.Message("appId1", "taskId1", "userId1", List.of("ossFile1", "ossFile2"))),
+            "indexField",
             List.of("base64_0", "base64_1"));
 
         TaskContentMsgs response = handler.handleRequest(request, context);

+ 2 - 0
yyc-fc-api/src/main/java/com/yaoyicloud/ai/pipeline/fc/TaskContentMsgs.java

@@ -13,6 +13,8 @@ public class TaskContentMsgs {
 
 	private List<Message> items;
 
+	private String indexField;
+
 	@Data
 	@NoArgsConstructor
 	@AllArgsConstructor

+ 2 - 2
yyc-fc-api/src/main/java/com/yaoyicloud/ai/pipeline/fc/TaskContentMsgsWithFeature.java

@@ -13,8 +13,8 @@ public class TaskContentMsgsWithFeature extends TaskContentMsgs {
 
 	private List<String> features; // base64,临时, 机械按顺序对应,""代表无feature
 
-	public TaskContentMsgsWithFeature(List<Message> items, List<String> features) {
-		super(items);
+	public TaskContentMsgsWithFeature(List<Message> items, String indexField, List<String> features) {
+		super(items, indexField);
 		this.features = features;
 	}
 }