-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathFileIngestionCompletableFuture.java
More file actions
99 lines (87 loc) · 4.88 KB
/
Copy pathFileIngestionCompletableFuture.java
File metadata and controls
99 lines (87 loc) · 4.88 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import com.microsoft.azure.kusto.data.auth.ConnectionStringBuilder;
import com.microsoft.azure.kusto.ingest.IngestClient;
import com.microsoft.azure.kusto.ingest.IngestClientFactory;
import com.microsoft.azure.kusto.ingest.IngestionMapping;
import com.microsoft.azure.kusto.ingest.IngestionProperties;
import com.microsoft.azure.kusto.ingest.exceptions.IngestionClientException;
import com.microsoft.azure.kusto.ingest.exceptions.IngestionServiceException;
import com.microsoft.azure.kusto.ingest.result.IngestionResult;
import com.microsoft.azure.kusto.ingest.source.FileSourceInfo;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
/**
* This class includes a sample of how to use the ingestFromFile() method within a CompletableFuture
*/
public class FileIngestionCompletableFuture {
public static void main(String[] args) {
try {
// Creating the connection string:
ConnectionStringBuilder csb = ConnectionStringBuilder.createWithAadApplicationCredentials(
System.getProperty("clusterPath"),
System.getProperty("appId"),
System.getProperty("appKey"),
System.getProperty("appTenant"));
CompletableFuture<IngestionResult> cf; // TODO: adjust this to use the async API instead of using CompletableFuture or not?
try (IngestClient client = IngestClientFactory.createClient(csb)) {
// Creating the ingestion properties:
IngestionProperties ingestionProperties = new IngestionProperties(
System.getProperty("dbName"),
System.getProperty("tableName"));
ingestionProperties.setIngestionMapping(System.getProperty("dataMappingName"), IngestionMapping.IngestionMappingKind.JSON);
FileSourceInfo fileSourceInfo = new FileSourceInfo(System.getProperty("filePath"));
// Ingest From File ASYNC returns a CompletableFuture:
cf = ingestFromFileAsync(client, fileSourceInfo, ingestionProperties);
}
// In case of exception during File Ingestion, a CompletionException will be thrown by the
// CompletableFuture, that contains in its cause the original exception that occurred during the
// ingestion itself.
// In this case we print an error message and the StackTrace of the cause, and return null, Else (if
// no exception was thrown), the IngestionResult will be returned by the completable future.
CompletableFuture<IngestionResult> cf2 = cf.exceptionally(ex -> {
System.err.println("Error in File Ingestion:");
// Here the getCause() will return the exception from the file ingestion operation
ex.getCause().printStackTrace();
return null;
});
// The developer can decide how to continue when the CompletableFuture ends. At this case,
// print a message, and apply the method doSomethingWithIngestionResult() on the IngestionResult:
cf2.thenRun(() -> System.out.println("File Ingestion ended."));
cf2.thenAccept(FileIngestionCompletableFuture::doSomethingWithIngestionResult);
System.out.println("(Press any key to terminate the program)");
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method wraps the client's ingestFromFile() method, and returns a CompletableFuture.
*
* @param client IngestClient that is connected to the Kusto cluster.
* @param fileSourceInfo The specific FileSourceInfo to be ingested
* @param ingestionProperties Settings used to customize the ingestion operation
* @return a {@link CompletableFuture}
*/
private static CompletableFuture<IngestionResult> ingestFromFileAsync(
IngestClient client, FileSourceInfo fileSourceInfo, IngestionProperties ingestionProperties) {
return CompletableFuture.supplyAsync(
() -> {
try {
return client.ingestFromFile(fileSourceInfo, ingestionProperties);
} catch (IngestionClientException | IngestionServiceException e) {
throw new CompletionException(e);
}
});
}
/**
* In this example we just printing a message to the standard output, but the user can decide what to do here.
*/
private static void doSomethingWithIngestionResult(IngestionResult ingestionResult) {
if (ingestionResult != null) {
System.out.println("IngestionResults: " + ingestionResult.toString());
} else {
System.out.println("No IngestionResults available");
}
}
}