|
| 1 | +package com.example.vault; |
| 2 | + |
| 3 | +import com.skyflow.Skyflow; |
| 4 | +import com.skyflow.config.Credentials; |
| 5 | +import com.skyflow.config.VaultConfig; |
| 6 | +import com.skyflow.enums.CustomHeaderKey; |
| 7 | +import com.skyflow.enums.Env; |
| 8 | +import com.skyflow.enums.LogLevel; |
| 9 | +import com.skyflow.enums.UpsertType; |
| 10 | +import com.skyflow.vault.data.InsertOptions; |
| 11 | +import com.skyflow.vault.data.InsertRecord; |
| 12 | +import com.skyflow.vault.data.InsertRequest; |
| 13 | +import com.skyflow.vault.data.InsertResponse; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.UUID; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.CompletionException; |
| 20 | + |
| 21 | +public class CustomHeaderExample { |
| 22 | + public static void main(String[] args) { |
| 23 | + try { |
| 24 | + // Step 1: Initialize credentials with the path to your service account key file |
| 25 | +// String filePath = "<YOUR_CREDENTIALS_FILE_PATH>"; |
| 26 | + Credentials credentials = new Credentials(); |
| 27 | + credentials.setToken("<BEARER_TOKEN>"); |
| 28 | + |
| 29 | + // Step 2: Configure the vault with required parameters |
| 30 | + VaultConfig vaultConfig = new VaultConfig(); |
| 31 | + vaultConfig.setVaultId("<VAULT_ID>"); |
| 32 | + vaultConfig.setClusterId("<CLUSTER_ID>"); |
| 33 | + vaultConfig.setEnv(Env.DEV); |
| 34 | + vaultConfig.setCredentials(credentials); |
| 35 | + |
| 36 | + // Step 3: Create Skyflow client instance with error logging |
| 37 | + Skyflow skyflowClient = Skyflow.builder() |
| 38 | + .setLogLevel(LogLevel.DEBUG) |
| 39 | + .addVaultConfig(vaultConfig) |
| 40 | + .build(); |
| 41 | + ArrayList<InsertRecord> insertRecords = new ArrayList<>(); |
| 42 | + |
| 43 | + for (int i = 0; i < 100; i++) { |
| 44 | + // Step 4: Prepare first record for insertion |
| 45 | + HashMap<String, Object> recordData1 = new HashMap<>(); |
| 46 | + recordData1.put("<YOUR_COLUMN_NAME_2>", "<YOUR_VALUE_1>"); |
| 47 | + |
| 48 | + InsertRecord insertRecord1 = InsertRecord |
| 49 | + .builder() |
| 50 | + .data(recordData1) |
| 51 | + .build(); |
| 52 | + |
| 53 | + // Step 6: Combine records into a Insert record list |
| 54 | + insertRecords.add(insertRecord1); |
| 55 | + } |
| 56 | + ArrayList<String> upsertColumns = new ArrayList<>(); |
| 57 | + upsertColumns.add("<UPSERT_COLUMN_NAME>"); |
| 58 | + InsertRequest request = InsertRequest.builder() |
| 59 | + .table("<TABLE_NAME>") |
| 60 | + .upsert(upsertColumns) |
| 61 | + .upsertType(UpsertType.REPLACE) |
| 62 | + .records(insertRecords) |
| 63 | + .build(); |
| 64 | + InsertOptions options = InsertOptions.builder() |
| 65 | + .interceptor((ctx) ->{ |
| 66 | + ctx.addHeader(CustomHeaderKey.RequestIDHeader, getRequestId()); // pass the request id here |
| 67 | + }) |
| 68 | + .build(); |
| 69 | + // Step 8: Execute the async bulk insert operation and handle response using callbacks |
| 70 | + CompletableFuture<InsertResponse> future = skyflowClient.vault().bulkInsertAsync(request, options); |
| 71 | + // Add success and error callbacks |
| 72 | + future.thenAccept(response -> { |
| 73 | + System.out.println("Async bulk insert resolved with response:\t" + response); |
| 74 | + }).exceptionally(throwable -> { |
| 75 | + System.err.println("Async bulk insert rejected with error:\t" + throwable.getMessage()); |
| 76 | + throw new CompletionException(throwable); |
| 77 | + }); |
| 78 | + } catch (Exception e) { |
| 79 | + // Step 9: Handle any synchronous errors that occur during setup |
| 80 | + System.err.println("Error in Skyflow operations:\t" + e.getMessage()); |
| 81 | + } |
| 82 | + } |
| 83 | + public static String getRequestId(){ |
| 84 | + String id = UUID.randomUUID().toString(); |
| 85 | + System.out.println("id=>"+ id); |
| 86 | + return id; |
| 87 | + } |
| 88 | +} |
0 commit comments