简体   繁体   中英

Call a sagemaker inference endpoint using the java sdk v2

I am trying to call a sagemaker inference endpoint from Java. I can do it without an issue from Python, running this after installing the sagemaker package:

predictor = TensorFlowPredictor('my-endpoint')
data = {'foo': 'bar'}
result = predictor.predict(data)

How can I do this using the latest official library? I'm assuming it is software.amazon.awssdk:sagemaker:2.17.167

There are similar questions on this site, but they seem to use an older version of the client. I also found a github repository with examples, but it does not show how to call an inference endpoint.

Answering my own question. I was using the wrong library. For inference there is a separate "runtime" dependency: software.amazon.awssdk:sagemakerruntime:2.17.167

Something like this then works, this is Kotlin code:

val runtime = SageMakerRuntimeClient.builder()
    .region(Region.EU_CENTRAL_1)
    .build()

val requestString = """
    {"foo": "bar"}
"""

val request = InvokeEndpointRequest.builder()
    .endpointName("my-endpoint")
    .contentType("application/json")
    .body(SdkBytes.fromString(requestString, Charset.defaultCharset()))
    .build()

val response = runtime.invokeEndpoint(request)

println(response.body().asString(Charset.defaultCharset()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM