-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from skyflowapi/SK-46/upsert-support
SK-46 add upsert support in insert method options.
- Loading branch information
Showing
16 changed files
with
347 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
samples/src/main/java/com/example/InsertWithUpsertExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright (c) 2022 Skyflow, Inc. | ||
*/ | ||
package com.example; | ||
|
||
import com.skyflow.entities.*; | ||
import com.skyflow.errors.SkyflowException; | ||
import com.skyflow.serviceaccount.util.Token; | ||
import com.skyflow.vault.Skyflow; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
|
||
public class InsertExample { | ||
|
||
public static void main(String[] args) { | ||
|
||
try { | ||
SkyflowConfiguration config = new SkyflowConfiguration("<your_vaultID>", | ||
"<your_vaultURL>", new DemoTokenProvider()); | ||
Skyflow skyflowClient = Skyflow.init(config); | ||
|
||
JSONObject records = new JSONObject(); | ||
JSONArray recordsArray = new JSONArray(); | ||
|
||
JSONObject record = new JSONObject(); | ||
record.put("table", "<your_table_name>"); | ||
|
||
JSONObject fields = new JSONObject(); | ||
fields.put("<field_name", "<your_field_value>"); | ||
|
||
record.put("fields", fields); | ||
recordsArray.add(record); | ||
|
||
records.put("records", recordsArray); | ||
|
||
// create an upsert option and insert in UpsertOptions array. | ||
UpsertOption[] upsertOptions = new UpsertOption[1]; | ||
upsertOptions[0] = new UpsertOption("<table_name>", "<unique_column_name>"); | ||
|
||
// pass upsert options in insert method options. | ||
InsertOptions insertOptions = new InsertOptions(true, upsertOptions); | ||
JSONObject res = skyflowClient.insert(records, insertOptions); | ||
|
||
System.out.println(res); | ||
} catch (SkyflowException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
static class DemoTokenProvider implements TokenProvider { | ||
|
||
private String bearerToken = null; | ||
|
||
@Override | ||
public String getBearerToken() throws Exception { | ||
ResponseToken response = null; | ||
try { | ||
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>"; | ||
if (Token.isExpired(bearerToken)) { | ||
response = Token.generateBearerToken(filePath); | ||
bearerToken = response.getAccessToken(); | ||
} | ||
} catch (SkyflowException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return bearerToken; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
Copyright (c) 2022 Skyflow, Inc. | ||
*/ | ||
package com.skyflow.entities; | ||
|
||
public class UpsertOption { | ||
private String table; | ||
private String column; | ||
|
||
public UpsertOption(String table, String column) { | ||
this.table = table; | ||
this.column = column; | ||
} | ||
|
||
public String getTable() { | ||
return table; | ||
} | ||
|
||
public String getColumn() { | ||
return column; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.