Repository navigation
Expand file tree
/
Copy pathInsertExample.java
More file actions
84 lines (73 loc) · 3.55 KB
/
Copy pathInsertExample.java
File metadata and controls
84 lines (73 loc) · 3.55 KB
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
package com.example.vault;
import com.skyflow.Skyflow;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.CustomHeaderKey;
import com.skyflow.enums.Env;
import com.skyflow.enums.LogLevel;
import com.skyflow.errors.SkyflowException;
import com.skyflow.vault.data.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This sample demonstrates the Skyflow Java SDK's unary insert operation. Unlike bulkInsert,
* this makes exactly one API call per invocation: there is no internal batching or concurrency
* to configure.
*/
public class InsertExample {
public static void main(String[] args) {
try {
// Step 1: Initialize credentials with the path to your service account key file
// String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";
Credentials credentials = new Credentials();
credentials.setToken("<YOUR_BEARER_TOKEN>");
// Step 2: Configure the vault with required parameters
VaultConfig vaultConfig = new VaultConfig();
vaultConfig.setVaultId("<YOUR_VAULT_ID>");
vaultConfig.setClusterId("<YOUR_CLUSTER_ID>");
vaultConfig.setEnv(Env.PROD);
vaultConfig.setCredentials(credentials);
// Step 3: Create Skyflow client instance with error logging
Skyflow skyflowClient = Skyflow.builder()
.setLogLevel(LogLevel.ERROR)
.addVaultConfig(vaultConfig)
.build();
// Step 4: Prepare the record to insert
Map<String, Object> data = new HashMap<>();
data.put("<YOUR_COLUMN_NAME_1>", "<YOUR_VALUE_1>");
InsertRequestRecord record = InsertRequestRecord.builder()
.data(data)
.tableName("<YOUR_TABLE_NAME>")
.build();
List<InsertRequestRecord> records = new ArrayList<>();
records.add(record);
// Step 5: Build and execute the insert request
InsertRequest request = InsertRequest.builder()
// .tableName("<YOUR_TABLE_NAME>")
.records(records)
.build();
InsertOptions options = InsertOptions.builder()
.interceptor(ctx -> {
ctx.addHeader(CustomHeaderKey.REQUEST_ID_HEADER, "demo"); // pass the request id here
})
.build();
InsertResponse response = skyflowClient.vault().insert(request, options);
// Step 6: Print every field on each response record.
for (InsertResponseRecord insertedRecord : response.getRecords()) {
System.out.println("tableName:\t" + insertedRecord.getTableName());
System.out.println("skyflowId:\t" + insertedRecord.getSkyflowId());
System.out.println("tokens:\t\t" + insertedRecord.getTokens());
System.out.println("data:\t\t" + insertedRecord.getData());
System.out.println("hashedData:\t" + insertedRecord.getHashedData());
System.out.println("httpCode:\t" + insertedRecord.getHttpCode());
System.out.println("error:\t\t" + insertedRecord.getError());
// System.out.println("request id " +insertedRecord.ge);
}
} catch (SkyflowException e) {
// Step 7: Handle any errors that occur during the process
System.err.println("Error in insert operation:\t" + e);
}
}
}