|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import com.skyflow.api.ApiClient; |
| 4 | +import com.skyflow.api.core.ApiClientApiException; |
| 5 | +import com.skyflow.api.core.RequestOptions; |
| 6 | +import com.skyflow.api.resources.flowservice.FlowserviceClient; |
| 7 | +import com.skyflow.api.resources.flowservice.requests.V1GetRequest; |
| 8 | +import com.skyflow.api.types.V1ColumnRedactions; |
| 9 | +import com.skyflow.api.types.V1GetResponse; |
| 10 | +import okhttp3.OkHttpClient; |
| 11 | +import okhttp3.Request; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +/** |
| 17 | + * This example demonstrates how to use the Skyflow SDK to securely get records from flowdb vault based on the Skyflow Ids & column values. |
| 18 | + * It includes: |
| 19 | + * 1. Setting up the auth client. |
| 20 | + * 2. Creating a Skyflow client. |
| 21 | + * 3. Setting up vault configurations. |
| 22 | + * 4. Creating a get request with the vaultId, tableName, and skyflowIDs. |
| 23 | + * 5. Getting records using Skyflow IDs and column values. |
| 24 | + */ |
| 25 | + |
| 26 | +public class GetExample { |
| 27 | + |
| 28 | + // Example 1: Get records by Skyflow IDs from the flowdb vault. |
| 29 | + public static void getRecordsById(FlowserviceClient client, String vaultId, String tableName, |
| 30 | + List<String> skyflowIDs, Integer limit, Integer offset, RequestOptions requestOptions) { |
| 31 | + try { |
| 32 | + // Step 4: Creating a get request with the vaultId, tableName, and skyflowIDs. |
| 33 | + V1GetRequest getRequest = V1GetRequest.builder() |
| 34 | + .vaultId(vaultId) |
| 35 | + .tableName(tableName) |
| 36 | + .skyflowIDs(skyflowIDs) |
| 37 | + .limit(limit) |
| 38 | + .offset(offset) |
| 39 | + .build(); |
| 40 | + |
| 41 | + // Step 5: Getting records using Skyflow IDs |
| 42 | + V1GetResponse records = client.get(getRequest, requestOptions); |
| 43 | + System.out.println("Get Response by id: " + records); |
| 44 | + } catch (Exception ex) { |
| 45 | + System.out.println("Error during get by id: " + ex); |
| 46 | + ex.printStackTrace(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Example 2: Get records by column values & column redaction from the flowdb vault. |
| 51 | + public static void getRecordsByColumnValues(FlowserviceClient client, String vaultId, String tableName, |
| 52 | + List<String> skyflowIDs, Integer limit, Integer offset, RequestOptions requestOptions) { |
| 53 | + try { |
| 54 | + // Creating a list of column values to get the records. |
| 55 | + List<String> columnValues = new ArrayList<>(); |
| 56 | + columnValues.add("<COLUMN_1>"); // Replace with the column name present in the table. |
| 57 | + columnValues.add("<COLUMN_2>"); |
| 58 | + |
| 59 | + // (Optional) Creating a list of column redactions to get the records in redacted format. |
| 60 | + List<V1ColumnRedactions> columnRedactions = new ArrayList<>(); |
| 61 | + V1ColumnRedactions column1Redaction = V1ColumnRedactions.builder() |
| 62 | + .redaction("<COLUMN_NAME_1_REDACTION>") // Replace with the redaction. |
| 63 | + .columnName("<COLUMN_NAME_1>") // Replace with the column name present in the table. |
| 64 | + .build(); |
| 65 | + |
| 66 | + V1ColumnRedactions column2Redaction = V1ColumnRedactions.builder() |
| 67 | + .redaction("<COLUMN_NAME_2_REDACTION>") // Replace with the redaction. |
| 68 | + .columnName("<COLUMN_NAME_2>") // Replace with the column name present in the table. |
| 69 | + .build(); |
| 70 | + |
| 71 | + columnRedactions.add(column1Redaction); |
| 72 | + columnRedactions.add(column2Redaction); |
| 73 | + |
| 74 | + // Step 4: Get request with column values and column redactions. |
| 75 | + V1GetRequest getRecordsByColumnValuesRequest = V1GetRequest.builder() |
| 76 | + .vaultId(vaultId) |
| 77 | + .skyflowIDs(skyflowIDs) |
| 78 | + .tableName(tableName) |
| 79 | + .columnRedactions(columnRedactions) |
| 80 | + .columns(columnValues) |
| 81 | + .limit(limit) |
| 82 | + .offset(offset) |
| 83 | + .build(); |
| 84 | + |
| 85 | + // Step 5: Getting records using column values. |
| 86 | + V1GetResponse records = client.get(getRecordsByColumnValuesRequest, requestOptions); |
| 87 | + System.out.println("Get Response by column values: " + records); |
| 88 | + } catch (Exception ex) { |
| 89 | + System.out.println("Error during get by column values: " + ex); |
| 90 | + ex.printStackTrace(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static void main(String[] args) { |
| 95 | + // Step 1: Setting up the auth client. |
| 96 | + String token = "<BEARER_TOKEN>"; // Replace with the actual bearer token. |
| 97 | + OkHttpClient authClient = new OkHttpClient.Builder().addInterceptor(chain -> { |
| 98 | + Request original = chain.request(); |
| 99 | + Request requestWithAuth = original.newBuilder() |
| 100 | + .header("Authorization", "Bearer " + token) |
| 101 | + .build(); |
| 102 | + return chain.proceed(requestWithAuth); |
| 103 | + }).build(); |
| 104 | + |
| 105 | + // Step 2: Creating a Skyflow client. |
| 106 | + ApiClient skyflowApiClient = ApiClient.builder() |
| 107 | + .url("<VAULT_URL>") // Replace with the vault URL. |
| 108 | + .httpClient(authClient) |
| 109 | + .build(); |
| 110 | + FlowserviceClient flowserviceClient = skyflowApiClient.flowservice(); |
| 111 | + |
| 112 | + // Step 3: Setting up vault configurations. |
| 113 | + String vaultId = "<VAULT_ID>"; // Replace with the vault ID. |
| 114 | + String tableName = "<TABLE_NAME>"; // Replace with the table name in the vault. |
| 115 | + |
| 116 | + // List of Skyflow IDs to get the record. |
| 117 | + List<String> skyflowIDs = new ArrayList<>(); |
| 118 | + skyflowIDs.add("<SKYFLOW_ID_1>"); // Replace with the record Skyflow ID |
| 119 | + skyflowIDs.add("<SKYFLOW_ID_2>"); |
| 120 | + |
| 121 | + // Setting up request options |
| 122 | + RequestOptions requestOptions = RequestOptions.builder() |
| 123 | + .timeout(5000) // Replace with the desired timeout in milliseconds. |
| 124 | + .build(); |
| 125 | + |
| 126 | + // The limit and offset are optional parameters. |
| 127 | + Integer limit = 5; // Replace with the desired limit. |
| 128 | + Integer offset = 0; // Replace with the desired offset. |
| 129 | + |
| 130 | + |
| 131 | + // Call the getRecordsById and getRecordsByColumnValues methods to get records from the vault. |
| 132 | + getRecordsById(flowserviceClient, vaultId, tableName, skyflowIDs, limit, offset, requestOptions); |
| 133 | + getRecordsByColumnValues(flowserviceClient, vaultId, tableName, skyflowIDs, limit, offset, requestOptions); |
| 134 | + } |
| 135 | +} |
0 commit comments