Developer toolchainCompute ModulesSDKsJava SDK

注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。

Java SDK

Compute Module 用の Java SDK を使用すると、Foundry と統合するデプロイ済み関数を構築できます。この SDK は、関数登録のためのビルダーパターン、認証ヘルパー、構造化ロギング、Foundry リソースを扱うためのユーティリティを提供します。

Java SDK は、com.palantir.computemodules パッケージから次の主要クラスを提供します。

クラス/パッケージ用途
ComputeModuleモジュールを作成して起動するためのビルダーパターン
functions.Contextすべての関数に渡されるコンテキストオブジェクト(メタデータ、資格情報)
auth.PipelinesAuthPipeline モード用のパイプライントークンの取得
auth.RefreshingOauthTokenアプリケーション権限用の自動更新される OAuth トークンラッパー

関数を定義する

ComputeModule ビルダー

関数の作成と登録には、主に ComputeModule ビルダーを使用します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import com.palantir.computemodules.ComputeModule; import com.palantir.computemodules.functions.Context; public class App { public static void main(String[] args) { ComputeModule.builder() .add(App::hello, String.class, String.class, "hello") .add(App::goodbye, Payload.class, Integer.class, "goodbye") .build() .start(); } record Payload(String name) {} static String hello(Context context, String name) { return "Hello, " + name; } static Integer goodbye(Context context, Payload payload) { return payload.name.length(); } }

.add() メソッドのパラメーター:

  1. 関数参照 (App::hello): 呼び出す関数への参照
  2. 入力のデータ型 (String.class): 2番目のパラメーターのデータ型と一致します
  3. 出力タイプ (String.class): 戻り値の型と一致します
  4. エンドポイント名 ("hello"): 関数を呼び出すための公開名

すべての関数は、1番目のパラメーターとして Context を、2番目のパラメーターとしてペイロードのデータ型を受け取る必要があります。

入出力のデータ型を定義する

Java レコード(推奨):

レコードを使用すると、不変のデータ型を簡潔に定義できます。

Copied!
1 2 3 4 5 public record Car(String brand, int doors) {} public record CalculateInput(double x, double y, String operation) {} public record CalculateOutput(double result, String operation) {}

@JsonProperty 注釈を持つクラス:

より複雑なシナリオでは、Jackson 注釈を持つクラスを使用します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import com.fasterxml.jackson.annotation.JsonProperty; public static class Car { @JsonProperty("brand") private String brand; @JsonProperty("doors") private int doors; Car() {} // Jackson のデシリアライズに必要です public String brand() { return brand; } public int doors() { return doors; } }

型システムとスキーマ推論について詳しくはこちら。

認証

Pipeline モードのトークン

Pipeline モードでは、build2 トークンを取得します。

Copied!
1 2 3 import com.palantir.computemodules.auth.PipelinesAuth; String token = PipelinesAuth.retrievePipelineToken();

アプリケーション権限 (3PA)

RefreshingOauthToken を使用して、自動更新されるトークンで Foundry リソースにアクセスします。

Copied!
1 2 3 4 5 6 7 8 9 10 11 import com.palantir.computemodules.auth.RefreshingOauthToken; String HOSTNAME = "myenrollment.palantirfoundry.com"; RefreshingOauthToken refreshingToken = new RefreshingOauthToken( HOSTNAME, List.of("api:datasets-read", "api:datasets-write") ); // トークンは有効期限が切れる前に自動更新されます String token = refreshingToken.getToken();

RefreshingOauthToken ラッパーはトークンの更新を自動的に処理し、長時間実行される処理の間もトークンが有効な状態を維持します。

認証モードについて詳しくはこちら。

Foundry リソースを扱う

オントロジー SDK (OSDK) を使用する

Java SDK は、オントロジーオブジェクトを扱うためにオントロジー SDK と統合されています。まず Developer Console で Java 用のオントロジー SDK を生成し、次のように使用します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import com.palantir.computemodules.client.config.EnvVars; import com.palantir.foundry.your_osdk_package.FoundryClient; import com.palantir.foundry.your_osdk_package.objects.Employee; import com.palantir.osdk.api.Auth; import com.palantir.osdk.api.auth.ConfidentialClientAuth; import com.palantir.osdk.internal.api.FoundryConnectionConfig; import java.util.List; static String returnObject() { Auth auth = ConfidentialClientAuth.builder() .clientId(EnvVars.Reserved.CLIENT_ID.get()) .clientSecret(EnvVars.Reserved.CLIENT_SECRET.get()) .build(); FoundryClient client = FoundryClient.builder() .auth(auth) .connectionConfig(FoundryConnectionConfig.builder() .foundryUri("https://" + System.getenv("FOUNDRY_URL")) .build()) .build(); List<Employee> objects = client.ontology().objects().Employee().fetchStream().toList(); return objects.get(0).toString(); }

アプリケーション権限を使用する場合、環境変数 CLIENT_ID と CLIENT_SECRET は自動的に設定されます。これらには、EnvVars.Reserved.CLIENT_ID.get() と EnvVars.Reserved.CLIENT_SECRET.get() を使用するか、System.getenv() を直接使用してアクセスできます。

オントロジー SDK の統合について詳しくはこちら。

ロギング

SDK は、start() が呼び出されるとロギングを自動的に設定します。SLS (service.1) の JSON レイアウトを使用して STDOUT に出力し、すべてのログ行に session_id、job_id、pid を自動的に含めます。

SafeLogger を使用する(推奨)

構造化された安全なロギングには、SafeLogger を使用します。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.logger.SafeLogger; import com.palantir.logsafe.logger.SafeLoggerFactory; public class App { private static final SafeLogger log = SafeLoggerFactory.get(App.class); static String hello(Context context, String name) { log.info("Greeting user", SafeArg.of("name", name)); return "Hello, " + name; } static Integer calculate(Context context, CalculateInput input) { log.debug("Starting calculation", SafeArg.of("operation", input.operation()), SafeArg.of("x", input.x()), SafeArg.of("y", input.y())); try { int result = performCalculation(input); log.info("Calculation successful", SafeArg.of("result", result)); return result; } catch (Exception e) { log.error("Calculation failed", e); throw e; } } }

デバッグとログの表示について詳しくはこちら。

重要な設定

メインクラスとして ComputeModule ではなく DeployedAppRuntime を使用する古いバージョンの Java SDK を使用している場合は、設定タブに移動し、ランタイムを含めるオプションがオフになっていることを確認してください。これは、DeployedAppRuntime を使用する Java Compute Module が正しく動作するために必要です。

GitHub リポジトリ

Java SDK はオープンソースで、GitHub で利用可能です。