Explorar o código

fake-feature-extract

dengjia hai 1 mes
pai
achega
d7d80da6ed

+ 84 - 0
fake-feature-extract/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"}
+```

+ 20 - 0
fake-feature-extract/event.json

@@ -0,0 +1,20 @@
+{
+  "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"
+    }
+  ]
+}

+ 92 - 0
fake-feature-extract/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-feature-extract</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>FakeFeatureExtract</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>

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

@@ -0,0 +1,24 @@
+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 FakeFeatureExtractHandler implements PojoRequestHandler<TaskContentMsgs, TaskContentMsgsWithFeature> {
+
+	@Override
+	public TaskContentMsgsWithFeature handleRequest(TaskContentMsgs request, Context context) {
+		System.out.println("Fake Features, Create Real Feature Extract FC to replace this in test channel or prod");
+		return new TaskContentMsgsWithFeature(
+			request.getItems(),
+			request.getItems().stream()
+				.map(msg -> IntStream.range(0, msg.getImgList().size()).mapToObj(i -> "base64_" + i).collect(Collectors.toList()))
+				.flatMap(List::stream)
+				.collect(Collectors.toList())
+		);
+	}
+
+}

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

@@ -0,0 +1,42 @@
+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 FakeFeatureExtractHandlerTest {
+
+    @Mock
+    private Context context;
+
+    private FakeFeatureExtractHandler handler;
+
+    @Before
+    public void setUp() {
+        handler = new FakeFeatureExtractHandler();
+    }
+
+    @Test
+    public void testHandleRequest() {
+        TaskContentMsgs request = new TaskContentMsgs(
+            List.of(new TaskContentMsgs.Message("appId1", "taskId1", "userId1", List.of("ossFile1", "ossFile2")))
+        );
+
+        TaskContentMsgsWithFeature response = handler.handleRequest(request, context);
+
+        assertNotNull(response);
+        assertEquals(2, response.getFeatures().size());
+        assertEquals(List.of("base64_0", "base64_1"), response.getFeatures());
+    }
+
+}

+ 3 - 0
pom.xml

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

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

@@ -0,0 +1,20 @@
+package com.yaoyicloud.ai.pipeline.fc;
+
+import java.util.List;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@EqualsAndHashCode(callSuper = false)
+public class TaskContentMsgsWithFeature extends TaskContentMsgs {
+
+	private List<String> features; // base64,临时, 机械按顺序对应,""代表无feature
+
+	public TaskContentMsgsWithFeature(List<Message> items, List<String> features) {
+		super(items);
+		this.features = features;
+	}
+}