소스 검색

pre-process fc

dengjia 1 개월 전
부모
커밋
ecc84aabd2

+ 1 - 0
pom.xml

@@ -29,6 +29,7 @@
     <!-- YYC的框架云函数 -->
     <module>read-oss-test-data</module>
     <module>mimic-real-msg</module>
+    <module>pre-process</module>
 
     <!-- YYC的算法测试云函数-->
     <module>fake-feature-extract</module>

+ 84 - 0
pre-process/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"}
+```

+ 24 - 0
pre-process/event.json

@@ -0,0 +1,24 @@
+{
+  "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"
+    }
+  ],
+  "features": [
+    "base64_1",
+    "base64_2"
+  ]
+}

+ 92 - 0
pre-process/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>pre-process</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>PreProcess</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>

+ 15 - 0
pre-process/src/main/java/com/yaoyicloud/ai/pipeline/fc/PreProcessHandler.java

@@ -0,0 +1,15 @@
+package com.yaoyicloud.ai.pipeline.fc;
+
+import com.aliyun.fc.runtime.Context;
+import com.aliyun.fc.runtime.PojoRequestHandler;
+
+public class PreProcessHandler implements PojoRequestHandler<TaskContentMsgsWithFeature, TaskContentMsgs> {
+
+	@Override
+	public TaskContentMsgs handleRequest(TaskContentMsgsWithFeature request, Context context) {
+		System.out.println("write to search db");
+		return new TaskContentMsgs(
+			request.getItems());
+	}
+
+}

+ 41 - 0
pre-process/src/test/java/com/yaoyicloud/ai/pipeline/fc/PreProcessHandlerTest.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 PreProcessHandlerTest {
+
+    @Mock
+    private Context context;
+
+    private PreProcessHandler handler;
+
+    @Before
+    public void setUp() {
+        handler = new PreProcessHandler();
+    }
+
+    @Test
+    public void testHandleRequest() {
+        TaskContentMsgsWithFeature request = new TaskContentMsgsWithFeature(
+            List.of(new TaskContentMsgs.Message("appId1", "taskId1", "userId1", List.of("ossFile1", "ossFile2"))),
+            List.of("base64_0", "base64_1"));
+
+        TaskContentMsgs response = handler.handleRequest(request, context);
+
+        assertNotNull(response);
+        assertEquals(1, response.getItems().size());
+    }
+
+}