如何使用Sagemaker Java API调用端点?

官方文档链接(以下有示例代码了,这个链接没啥用了,兄弟们,简单看看得了)

https://docs.aws.amazon.com/zh_tw/AWSJavaSDK/latest/javadoc/overview-summary.html

在这里会教你如何获得你的AWS Access Key ID and Secret Access Key。

1 首先来到 Amazon Web Services web console
2.用你的amazon账号登陆,点击右上角你的用户名,下拉
3.点击向下箭头选择“Security Credentials” 选项
4.在弹出窗口点击“Continue to Security Credentials”按钮
5.点击“Access Keys (Access Key ID and Secret Access Key)”
6.这里会显示你的Access Key IDs.保存下来(如果你会显示一些账号,那么你可以删除delete现有access key然后重新建立一套key,这是会显示AWS Access Key ID和Secret Access Key,就不用做后续的操作了)
7. 找到你的Secret Key并点击“Security Credentials”
8. 如果你需要登陆amazon账号,点击 “Sign in using our secure server”按钮登陆
9. 你现在会看到 legacy Security Credentials page页面。下拉页面找到the Access Credentials box。这里会再次看到你的 Access Key IDs。
10. 点击“Show”会显示你的“Secret Access Key” 对话框。
11.保存好你的Secret Access Key。

然后直接贴代码(以下是我多线程测试的代码,核心代码就只有run方法)

import com.alibaba.fastjson.JSONObject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntime;
import com.amazonaws.services.sagemakerruntime.AmazonSageMakerRuntimeClientBuilder;
import com.amazonaws.services.sagemakerruntime.model.InvokeEndpointRequest;
import com.amazonaws.services.sagemakerruntime.model.InvokeEndpointResult;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AmazonUtilTest implements Runnable {

    private static int POOL_NUM = 15;//线程池数量

    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < POOL_NUM; i++) {
            AmazonUtilTest poolThread = new AmazonUtilTest();
            threadPool.execute(poolThread);
        }
        threadPool.shutdown();//关闭线程池

    }

    @Override
    public void run() {

        AmazonSageMakerRuntimeClientBuilder amazonSageMakerRuntimeClientBuilder = AmazonSageMakerRuntimeClientBuilder
                .standard();
        amazonSageMakerRuntimeClientBuilder.setRegion("us-west-XX");
        String accessKey = "XXXXXX";
        String secretKey = "yyyyyyyyyyyyy";
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
        AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(basicAWSCredentials);
        amazonSageMakerRuntimeClientBuilder.setCredentials(awsStaticCredentialsProvider);
        AmazonSageMakerRuntime runtime = amazonSageMakerRuntimeClientBuilder.build();

        //多线程跑测试
        //根据自己的算法入参规定格式,我这边是json,默认含有字段是ask
        //String body = "{\"instances\": [1.0,2.0,5.0],\"ask\":\"Have you eaten yet?\" }";
        String body = "{\"ask\":\"写一首关于上班摸鱼的五言押韵绝句\"}";
        //String body = "Have you eaten yet?";

        ByteBuffer bodyBuffer = ByteBuffer.wrap(body.getBytes());

        InvokeEndpointRequest request = new InvokeEndpointRequest()
        		//访问的端点名称
                .withEndpointName("pytorch-inference-XXXXXXXXX")
                .withBody(bodyBuffer);
        long start = System.currentTimeMillis();
        InvokeEndpointResult invokeEndpointResult = runtime.invokeEndpoint(request);
        long end = System.currentTimeMillis();
        System.out.println("请求耗时:" + (end - start) / 1000 + "秒");
        String bodyResponse = new String(invokeEndpointResult.getBody().array());

        System.out.println("bodyResponse:" + bodyResponse);
        JSONObject jsonObject = (JSONObject) JSONObject.parse(bodyResponse);
        System.err.println("bodyResponse:" + jsonObject.get("answer").toString());
    }
}

see you again

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值