コードの例Foundry APIsローカル環境
Warning

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

ローカル環境

Python

プロジェクトにリソースを追加する

API コールを使用してプロジェクトにリソースを追加するにはどうすればよいですか?

このコードは requests ライブラリを使用して Compass API に HTTP POST リクエストを送信し、指定されたプロジェクトに作成物のリファレンスをインポートします。エラーハンドリング、ロギング、およびオプションのプロキシ設定が含まれています。

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 from requests.adapters import HTTPAdapter import requests from urllib3 import Retry import logging import json ''' 特定のプロジェクトに対するアーティファクトの参照をインポートする ''' # ヘッダー headers = { 'Authorization': 'Bearer xxx', # 'xxx' を自身のベアラートークンに置き換えてください 'Content-Type': 'application/json', } # ホスト host = 'subdomain.domain.extension:port' # プロキシ proxyDict = { 'https': 'protocol://subdomain.domain.extension:port' } # リトライ設定 retry = Retry(connect=1, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) http = requests.Session() http.mount('https://', adapter) # ログのレベルを設定 logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[ logging.FileHandler("debug.log"), logging.StreamHandler() ] ) ############### ## 変数定義 ## ############### RESSOURCE_TO_ADD = "ri......" PROJECT_TO_ADD_TO = "ri.compass.main.folder.xxxx-xxx-xxx-xxx-xxxx" # リクエストが何らかの形で失敗した場合にエラーを投げ、情報を表示する try: print('スクリプトを開始します...') # データ source_data = { "requests": [ {"resourceRid": f"{RESSOURCE_TO_ADD}"} ] } # JSON をシリアライズ data = json.dumps(source_data) response = http.post(f'https://{host}/compass/api/projects/imports/{PROJECT_TO_ADD_TO}/import', data=data, headers=headers, # プロキシが必要な場合はコメントを外します # proxies=proxyDict ) print('リクエストが完了しました') print('スクリプトの結果は...') raw_response = response.text print(raw_response) print(response.status_code) except requests.exceptions.RequestException as e: raise Exception( f"リクエスト中にエラーが発生しました。\n失敗の原因: {response.status_code} - {response.text}\n例外: {e}")
  • Date submitted: 2024-03-26
  • Tags: API, python, compass

データセットの行数

多くのデータセットに対して一括で行数を計算するにはどうすればよいですか?

このコードは Foundry API を使用して、データセット RID のリストに対して行数計算をトリガーします。データセット RID とブランチをパラメーターとして Foundry Stats API に POST リクエストを送信します。

  • Date submitted: 2024-03-26
  • Tags: export, python, metrics, metadata, local

データセット全体の列のスーパーセットを取得する

複数のデータセット全体のすべての列のセットを取得するにはどうすればよいですか?

このコードは requests ライブラリを使用して、ターゲットデータセットのリスト内の各データセットのスキーマを取得し、次にスキーマ内のフィールドを反復して、列のスーパーセット内の各列の頻度を含む辞書を作成します。

  • 提出日: 2024-03-26
  • タグ: python, API, metadata, code repositories, Code Authoring, local

OSS 直接呼び出し

Object Set Service (OSS) を使用してオブジェクトセットに対して集計を行うにはどうすればよいですか?

このコードは、Object Set Service (OSS) に直接呼び出しを行い、オブジェクトセットに対して集計を行う方法を示しています。これは、OSDK やフロントエンドなどの他のサービスが使用する OSS によって返されるデータをデバッグしたり理解したりするのに役立ちます。

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 from requests.adapters import HTTPAdapter import requests from urllib3 import Retry import logging import json import pprint ''' Direct calls to object-set-service (OSS) to perform aggregations, etc. This is not intended for "actual use" in production, but can be useful for debugging or going one layer deeper, for instance to debug or understand what is actually returned by OSS. OSS is used under the hood by other services, like OSDK and frontends. OSSに直接呼び出して集計などを実行します。 これは本番環境での「実際の使用」を意図したものではありませんが、 デバッグやOSSが実際に返すものを理解するために役立つことがあります。 OSSは他のサービス(OSDKやフロントエンドなど)によって内部で使用されます。 ''' # Headers headers = { 'Authorization': 'Bearer xxx', # 'xxx' をあなたのベアラートークンに置き換えてください 'Content-Type': 'application/json', } # Host host = 'subdomain.domain.extension:port' # ホスト名とポート番号 # Proxies proxyDict = { 'https': 'protocol://subdomain.domain.extension:port' # プロキシ設定 } # Retries retry = Retry(connect=1, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) http = requests.Session() http.mount('https://', adapter) # Set the level of logging to be shown logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[ logging.FileHandler("debug.log"), logging.StreamHandler() ] ) ############### ## VARIABLES ## ############### # リクエストが何らかの方法で失敗した場合、エラーを投げてその情報を表示 try: print(f'Beginning script for ...') # データ - OSSへのペイロードの例 data = { "executionMode":"PREFER_ACCURACY", # OSSはデフォルトで"PREFER_SPEED"を使用し、これにより不正確な結果が返される可能性があります "objectSet": { "base": { "objectTypeId": "af-20m-instances-obv2" # オブジェクトインスタンスID }, "type": "base" }, "aggregation": { "metrics": {}, "subAggregations": { "test": { "type": "metrics", "metrics": { "dimension": { "type": "propertyValue", "propertyValue": { "propertyId": "example_bucket", # 集計対象のオブジェクトのプロパティ "bucketing": { "type": "exactValue", "exactValue": { "maxBuckets": 10 # 応答に含まれるバケットの数 } } } }, "ordering": [ { "type": "valueOrdering", "valueOrdering": { "direction": "DESCENDING", # 降順 "metricName": "countM" } } ], "metrics": { "countM": { "type": "count", "count": {} } } } } } } } # JSONのシリアライズ data = json.dumps(source_data) response = http.put(f'https://{host}/object-set-service/api/aggregate', data=data, headers=headers, # プロキシが必要な場合はコメントを解除 # proxies=proxyDict ) print('Completed request') print(f'The result of the script is ...') pprint.pprint(response.json()) except requests.exceptions.RequestException as e: raise Exception(f"An error occurred in the request.\nIt failed due to: {response.status_code} - {response.text}\nException: {e}")
  • 提出日: 2024-03-26
  • タグ: ontology, aggregation, objects, python, API, local

Foundry への Ping: トークン不要

認証トークンなしで Foundry インスタンスまたはアカウントに ping を送信するにはどうすればよいですか?

このコードは、指定されたプロキシおよびリトライ設定を使用して Palantir Cloud Stack に ping リクエストを送信するための Python requests ライブラリの使用方法を示しています。

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 29 from requests.packages.urllib3.util.retry import Retry from requests.adapters import HTTPAdapter import requests headers = { # 原則としてトークンは不要です! 'Authorization': 'Bearer eyg_PUT_YOUR_TOKEN_HERE_xxxx', 'Content-Type': 'application/json', } ## STACK base_url = "https://STACK_NAME.palantircloud.com" # プロキシ設定 proxyDict = { "https": "http://proxy.host.com:3333" } # リトライ設定 retry = Retry(connect=1, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) http = requests.Session() http.mount("https://", adapter) print("Pinging ... ") response = http.get(f"{base_url}/compass/api/ping", headers=headers, proxies=proxyDict) print("Ping response :") raw_response = response.text print(raw_response)
  • 提出日: 2024-03-26
  • タグ: API, python, compass, local

指定されたリソースのRIDのパスを取得する

RIDからリソースのパスを見つけるにはどうすればよいですか?

このコードは requests ライブラリを使用して、指定されたホストに与えられた RID を含む HTTP GET リクエストを送信し、リソースのパスを取得します。また、リトライとプロキシの処理も行います。

  • 提出日: 2024-03-26
  • タグ: api, python, metadata, local

API を使用してアクションをトリガーする

オブジェクトに対してアクションを手動でトリガーするにはどうすればよいですか?

このコードは、requests ライブラリを使用してアクション API に HTTP リクエストを送信し、ID のリストを反復処理して各 ID にカスタムパラメーターでアクションをトリガーします。

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 from requests.packages.urllib3.util.retry import Retry from requests.adapters import HTTPAdapter import pprint import uuid import requests import json import time ''' Script that will trigger an action. ''' # アクションをトリガーするスクリプト headers = { 'Authorization': 'Bearer eyg_PUT_YOUR_TOKEN_HERE_xxxx', # 認証トークンをここに入れる 'Content-Type': 'application/json' # コンテンツタイプをJSONに指定 } # Name of the stack # スタックの名前 STACK = "STACK_NAME.palantircloud.com" # List of ids, could be any list of parameters you want to iterate on # IDのリスト、任意のパラメータのリストにすることができます list_ids = ["123", "456"] # Proxies # プロキシ proxyDict = { "https": "https://proxyIfNeeded:port" # 必要に応じてプロキシを指定 } # Retries # リトライ設定 retry = Retry(connect=1, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) http = requests.Session() http.mount("https://", adapter) # Iterate over the list of ids and trigger one action per ID. # IDのリストを繰り返し処理し、IDごとにアクションをトリガーします for curr_id in list_ids: curr_uuid = "GENERATED-OBJECT-" + str(uuid.uuid4()) # UUIDを生成 curr_title = "GENERATED-" + str(time.time()) # タイムスタンプを生成 user_rid = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx" # ユーザーRIDが必要な場合 try: # Generate the payload of the action. Look at a network tab from slate/Workshop to obtain it - or build it from scratch. # アクションのペイロードを生成します。Slate/Workshopのネットワークタブを参照するか、ゼロから構築します。 payload = r'{"actionTypeRid":"ri.actions.main.action-type.xxxxx-xxxx-xxxx-xxxxxxxx",' \ r'"parameters":{"ri.actions.main.parameter.xxxxx-xxxx-xxxx-xxxxxxxx":{"timestamp":"2021-09-30T23:59:59+02:00","type":"timestamp"},' \ r'"ri.actions.main.parameter.xxxxx-xxxx-xxxx-xxxxxxxx":{"stringList":{"strings":["mystring1","mystring2"]},"type":"stringList"},' \ r'"ri.actions.main.parameter.xxxxx-xxxx-xxxx-xxxxxxxx":{"string":"my_string","type":"string"},' \ r'"ri.actions.main.parameter.xxxxx-xxxx-xxxx-xxxxxxxx":{"timestamp":"2022-01-01:23:26+00:00","type":"timestamp"}}' response = http.post(f'https://{STACK}/actions/api/actions', headers=headers, proxies=proxyDict, data=payload) print(f"Raw response of action call with : {curr_id}\r\n") raw_response = response.json() pprint.pprint(raw_response, indent=4) except Exception as e: print(e)
  • 提出日: 2024-03-26
  • タグ: action, objects, オントロジー, actions on objects, python, api, local

ローカルファイルをデータセットにアップロードする

Foundry でローカルファイルをデータセットにアップロードするには、どの API を使用できますか?

このコードは Foundry API を使用して指定されたデータセットにファイルをアップロードします。リクエストのためにヘッダー、ホスト、プロキシ、リトライを設定し、ファイルを読み込んでデータセットの files エンドポイントに POST リクエストとして送信します。

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 from requests.adapters import HTTPAdapter import requests from urllib3 import Retry import logging import json # ヘッダー headers = { 'Authorization': 'Bearer xxx', # 'xxx' をあなたのベアラートークンに置き換えてください 'Content-type': 'application/octet-stream', ### 重要! } # ホスト host = 'subdomain.domain.extension:port' # プロキシ proxyDict = { 'https': 'protocol://subdomain.domain.extension:port' } # リトライ設定 retry = Retry(connect=1, backoff_factor=0.5) adapter = HTTPAdapter(max_retries=retry) http = requests.Session() http.mount('https://', adapter) # ロギングのレベルを設定 logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[ logging.FileHandler("debug.log"), logging.StreamHandler() ] ) ############### ## 変数定義 ## ############### # アップロードするデータセットのRID datasetRid = "rid.123..." params = { 'filePath': 'folder_name/my-file.csv', } # リクエストが何らかの理由で失敗した場合にエラーをスローし、その情報を表示 try: print(f'Beginning script for one-call upload') # データ with open('./data_example_file.csv') as f: data = f.read().replace('\n', '').replace('\r', '').encode() response = http.post(f'https://{host}/api/v1/datasets/{datasetRid}/files:upload', params=params, data=data, headers=headers, # プロキシが必要な場合はコメントを外してください # proxies=proxyDict ) print('Completed request') print(f'The result of the script is {response.status_code} - {response.text}') except requests.exceptions.RequestException as e: raise Exception( f"リクエスト中にエラーが発生しました。\n失敗の原因: {response.status_code} - {response.text}\n例外: {e}")
  • 提出日: 2024-04-04
  • タグ: API, file upload, dataset, python, csv, local