/
insert_upsert_sample.py
39 lines (33 loc) · 1.05 KB
/
insert_upsert_sample.py
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
'''
Copyright (c) 2022 Skyflow, Inc.
'''
from skyflow.errors import SkyflowError
from skyflow.service_account import generate_bearer_token, is_expired
from skyflow.vault import Client, InsertOptions, Configuration, UpsertOption
# cache token for reuse
bearerToken = ''
def token_provider():
global bearerToken
if is_expired(bearerToken):
bearerToken, _ = generate_bearer_token('<YOUR_CREDENTIALS_FILE_PATH>')
return bearerToken
try:
config = Configuration(
'<YOUR_VAULT_ID>', '<YOUR_VAULT_URL>', token_provider)
client = Client(config)
upsertOption = UpsertOption(table='<TABLE_NAME>',column='<UNIQUE_COLUMN_NAME>')
options = InsertOptions(tokens=True,upsert=[upsertOption])
data = {
'records': [
{
'table': '<TABLE_NAME>',
'fields': {
'<FIELDNAME>': '<VALUE>'
}
}
]
}
response = client.insert(data, options=options)
print('Response:', response)
except SkyflowError as e:
print('Error Occurred:', e)