文档

Java SDK快速入门

更新时间:

本文介绍如何使用Java SDK对智能召回引擎进行读写操作。

前提条件

已完成Java SDK安装。更多信息,请参见 安装Java SDK

示例代码

本示例中,初始化智能召回引擎的客户端,并调用接口完成对服务的查询操作,对详情表的写操作。示例如下:

package com.aliyuncs.be.client;

import com.aliyuncs.be.client.exception.InvalidParameterException;
import com.aliyuncs.be.client.protocol.BeWriteType;
import com.aliyuncs.be.client.protocol.RecallParam;
import com.aliyuncs.be.client.protocol.RecallType;
import com.aliyuncs.be.client.protocol.clause.FilterClause;
import com.aliyuncs.be.client.protocol.clause.FilterOperator;
import com.aliyuncs.be.client.protocol.clause.SingleFilter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

@Slf4j
public class AimeDemo {

    public static void main(String[] args) throws InvalidParameterException {
        BeClient client = new BeClient("http://be-cn-xxxx.be.aliyuncs.com", 80, "username", "password");

        // x2i召回
        RecallParam x2iRecallParam = RecallParam.builder().recallType(RecallType.X2I)
                .triggerItems(Lists.newArrayList("100:1", "200:1"))
                .build();
        BeReadRequest x2iRequest = BeReadRequest.builder()
                .bizName("x2i_stress_biz_test")
                .recallParams(Lists.newArrayList(x2iRecallParam))
                .returnCount(10)
                .filterClause(new FilterClause(new SingleFilter("field1", FilterOperator.EQ, "'100'")))
                .build();
        BeResponse<BeResult> x2iResponse = client.query(x2iRequest);
        System.out.println("X2I resp:\n" + new Gson().toJson(x2iResponse));

        // 向量召回
        RecallParam vectorRecallParam = RecallParam.builder().recallType(RecallType.VECTOR)
                .triggerItems(Lists.newArrayList(
                        "0.579158749845,0.470209030437,0.749097467349,0.445656739882,0.844264905484,0.712275306635,0.844056298441,0.276650665052,0.949767836991,0.649219221171,0.66098161068,0.0691913368449,0.354189112041,0.447059806687,0.0549635567026,0.460209072549,0.65463754064,0.274392229466,0.0938289210632,0.424779378871,0.724381527526,0.207687200109,0.0439432787604,0.559263209734,0.0595299411194,0.949378904378,0.184843100327,0.208226326752,0.702787280776,0.211002033769,0.305579165662,0.0689032954442",
                        "0.608493077698,0.364550556482,0.22805051152,0.0762799657836,0.625648915719,0.57495010351,0.513739996805,0.851899131358,0.890538053817,0.915521223743,0.896193552683,0.809539003532,0.624342285425,0.777748612316,0.835579622526,0.988614572482,0.817653978433,0.807812712295,0.789510115629,0.96497810191,0.044510097916,0.810315469502,0.902480341306,0.698768508509,0.962197754709,0.74970913387,0.274244754285,0.756700157766,0.911915116709,0.407594729035,0.0998223357224,0.744411396701"
                ))
                .build();
        BeReadRequest vectorRequest = BeReadRequest.builder()
                .bizName("vector_stress_biz_test")
                .recallParams(Lists.newArrayList(vectorRecallParam))
                .returnCount(10)
                .build();
        BeResponse<BeResult> vectorResponse = client.query(vectorRequest);
        System.out.println("Vector resp:\n" + new Gson().toJson(vectorResponse));

        // 多路召回
        x2iRecallParam.setRecallName("vector_recall");
        x2iRecallParam.setReturnCount(5);
        vectorRecallParam.setRecallName("xi2_recall");
        vectorRecallParam.setReturnCount(5);
        BeReadRequest multiRequest = BeReadRequest.builder()
                .bizName("multi_stress_biz_test")
                .recallParams(Lists.newArrayList(x2iRecallParam, vectorRecallParam))
                .returnCount(10)
                .build();
        BeResponse<BeResult> multiResponse = client.query(multiRequest);
        System.out.println("Multi resp:\n" + new Gson().toJson(multiResponse));

        String tableName = "be_example_expose";
        
        // 写add消息
        HashMap<String, String> addContents = Maps.newHashMap();
        addContents.put("user_id", "u0003");
        addContents.put("item_id", "10001");
        addContents.put("time", "1640242662");
        BeWriteRequest addRequest = BeWriteRequest.builder()
                .primaryKey("user_id")
                .type(BeWriteType.ADD)
                .tableName(tableName)
                .contents(addContents)
                .build();
        BeResponse writeResponse = client.write(addRequest);
        System.out.println("Add resp:\n" + new Gson().toJson(writeResponse));

        // 写delete消息
        HashMap<String, String> deleteContents = Maps.newHashMap();
        deleteContents.put("user_id", "u0003");
        deleteContents.put("item_id", "10001");
        BeWriteRequest deleteRequest = BeWriteRequest.builder()
                .primaryKey("user_id")
                .type(BeWriteType.DELETE)
                .tableName(tableName)
                .contents(deleteContents)
                .build();
        writeResponse = client.write(deleteRequest);
        System.out.println("Delete resp:\n" + new Gson().toJson(writeResponse));
    }
}

返回结果

返回结果示例如下:

X2I resp:
{"success":true,"result":{"errorCode":0,"errorMessage":"none","matchItems":{"fieldNames":["__score__","content","field1","field2","weight","item_id","match_type"],"fieldValues":[["0.988155007362366","949553_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","952","954","949553","0"],["0.935471892356873","161613_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","968","499","161613","0"],["0.915997266769409","517663_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","835","676","517663","0"],["0.873209953308105","248070_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","986","262","248070","0"],["0.86224102973938","779967_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","32","418","779967","0"],["0.680734694004059","327807_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","886","74","327807","0"],["0.666359663009644","542137_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","786","831","542137","0"],["0.496475785970688","759533_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","957","886","759533","0"],["0.36130827665329","873588_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","285","96","873588","0"],["0.301691889762878","112848_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","100","131","833","112848","0"]]}}}
Vector resp:
{"success":true,"result":{"errorCode":0,"errorMessage":"none","matchItems":{"fieldNames":["match_type","__score__","content","field1","field2","weight","item_id"],"fieldValues":[["0","15.965773582458496","136453_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","159","368","261","136453"],["0","15.827646255493164","207968_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","29","652","484","207968"],["0","15.694404602050782","542054_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","446","618","968","542054"],["0","15.608759880065918","250028_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","275","58","159","250028"],["0","15.49856948852539","681135_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","18","541","417","681135"],["0","15.47065258026123","831861_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","81","243","552","831861"],["0","15.469311714172364","636470_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","251","245","534","636470"],["0","15.46304702758789","367727_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","170","52","999","367727"],["0","15.376935005187989","647959_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","314","877","375","647959"],["0","15.37316608428955","702045_0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","460","580","397","702045"]]}}}
Multi resp:
{"success":true,"result":{"errorCode":0,"errorMessage":"none","matchItems":{"fieldNames":["__score__","content","field1","field2","item_id","match_type","weight"],"fieldValues":[]}}}
Add resp:
{"success":true}
Delete resp:
{"success":true}
  • 本页导读 (0)