-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTDClient.java
More file actions
1156 lines (1006 loc) · 40.7 KB
/
TDClient.java
File metadata and controls
1156 lines (1006 loc) · 40.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.treasuredata.client;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Multimap;
import com.treasuredata.client.model.ObjectMappers;
import com.treasuredata.client.model.TDApiKey;
import com.treasuredata.client.model.TDAuthenticationResult;
import com.treasuredata.client.model.TDBulkImportParts;
import com.treasuredata.client.model.TDBulkImportSession;
import com.treasuredata.client.model.TDBulkLoadSessionStartRequest;
import com.treasuredata.client.model.TDBulkLoadSessionStartResult;
import com.treasuredata.client.model.TDColumn;
import com.treasuredata.client.model.TDConnectionLookupResult;
import com.treasuredata.client.model.TDDatabase;
import com.treasuredata.client.model.TDExportJobRequest;
import com.treasuredata.client.model.TDExportResultJobRequest;
import com.treasuredata.client.model.TDFederatedQueryConfig;
import com.treasuredata.client.model.TDImportResult;
import com.treasuredata.client.model.TDJob;
import com.treasuredata.client.model.TDJobList;
import com.treasuredata.client.model.TDJobRequest;
import com.treasuredata.client.model.TDJobSubmitResult;
import com.treasuredata.client.model.TDJobSummary;
import com.treasuredata.client.model.TDPartialDeleteJob;
import com.treasuredata.client.model.TDResultFormat;
import com.treasuredata.client.model.TDSaveQueryRequest;
import com.treasuredata.client.model.TDSavedQuery;
import com.treasuredata.client.model.TDSavedQueryHistory;
import com.treasuredata.client.model.TDSavedQueryStartRequest;
import com.treasuredata.client.model.TDSavedQueryStartRequestV4;
import com.treasuredata.client.model.TDSavedQueryStartResultV4;
import com.treasuredata.client.model.TDSavedQueryUpdateRequest;
import com.treasuredata.client.model.TDTable;
import com.treasuredata.client.model.TDTableDistribution;
import com.treasuredata.client.model.TDTableList;
import com.treasuredata.client.model.TDTableType;
import com.treasuredata.client.model.TDUpdateTableResult;
import com.treasuredata.client.model.TDUser;
import com.treasuredata.client.model.TDUserList;
import com.treasuredata.client.model.impl.TDScheduleRunResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Function;
import java.util.regex.Pattern;
import static java.util.Objects.requireNonNull;
/**
*
*/
public class TDClient
implements TDClientApi<TDClient>
{
private static final Logger logger = LoggerFactory.getLogger(TDClient.class);
private static final String version;
public static String getVersion()
{
return version;
}
static String readMavenVersion(URL mavenProperties)
{
String v = "unknown";
if (mavenProperties != null) {
try (InputStream in = mavenProperties.openStream()) {
Properties p = new Properties();
p.load(in);
v = p.getProperty("version", "unknown");
}
catch (Throwable e) {
logger.warn("Error in reading pom.properties file", e);
}
}
return v;
}
static {
URL mavenProperties = TDClient.class.getResource("/META-INF/maven/com.treasuredata.client/td-client/pom.properties");
version = readMavenVersion(mavenProperties);
logger.info("td-client version: " + version);
}
public static TDClient newClient()
{
return new TDClientBuilder(true).build();
}
public static TDClientBuilder newBuilder()
{
return new TDClientBuilder(true);
}
public static TDClientBuilder newBuilder(boolean loadTDConf)
{
return new TDClientBuilder(loadTDConf);
}
/**
* Create a new TDClient that uses the given api key for the authentication.
* The new instance of TDClient shares the same HttpClient, so closing this will invalidate the other copy of TDClient instances
*
* @param newApiKey
* @return
*/
@Override
public TDClient withApiKey(String newApiKey)
{
return new TDClient(config, httpClient, Optional.of(newApiKey));
}
/**
* @deprecated Use {@link #withHeaders(Map)} instead.
* @param headers
* @return
*/
@Deprecated
@Override
public TDClient withHeaders(Multimap<String, String> headers)
{
return new TDClient(config, httpClient.withHeaders(headers), apiKeyCache);
}
@Override
public TDClient withHeaders(Map<String, ? extends Collection<String>> headers)
{
return new TDClient(config, httpClient.withHeaders(headers), apiKeyCache);
}
/**
* Visible for testing.
*/
protected final TDClientConfig config;
/**
* Visible for testing.
*/
protected final TDHttpClient httpClient;
protected final Optional<String> apiKeyCache;
public TDClient(TDClientConfig config)
{
this(config, new TDHttpClient(config), config.apiKey);
}
protected TDClient(TDClientConfig config, TDHttpClient httpClient, Optional<String> apiKeyCache)
{
this.config = config;
this.httpClient = httpClient;
this.apiKeyCache = apiKeyCache;
}
public void close()
{
httpClient.close();
}
protected static String buildUrl(String urlPrefix, String... args)
{
StringBuilder s = new StringBuilder();
s.append(urlPrefix);
for (String a : args) {
s.append("/");
s.append(UrlPathSegmentEscaper.escape(a));
}
return s.toString();
}
protected <ResultType> ResultType doGet(String path, Class<ResultType> resultTypeClass)
throws TDClientException
{
requireNonNull(path, "path is null");
requireNonNull(resultTypeClass, "resultTypeClass is null");
TDApiRequest request = TDApiRequest.Builder.GET(path).build();
return httpClient.call(request, apiKeyCache, resultTypeClass);
}
protected <ResultType> ResultType doGet(String path, TypeReference<ResultType> resultTypeReference)
throws TDClientException
{
requireNonNull(path, "path is null");
requireNonNull(resultTypeReference, "resultTypeReference is null");
TDApiRequest request = TDApiRequest.Builder.GET(path).build();
return httpClient.call(request, apiKeyCache, resultTypeReference);
}
protected <ResultType> ResultType doGet(String path, JavaType resultType)
throws TDClientException
{
requireNonNull(path, "path is null");
requireNonNull(resultType, "resultType is null");
TDApiRequest request = TDApiRequest.Builder.GET(path).build();
return httpClient.call(request, apiKeyCache, resultType);
}
protected <ResultType> ResultType doPost(String path, Map<String, String> queryParam, Optional<String> jsonBody, Class<ResultType> resultTypeClass)
throws TDClientException
{
requireNonNull(path, "path is null");
requireNonNull(queryParam, "param is null");
requireNonNull(jsonBody, "body is null");
requireNonNull(resultTypeClass, "resultTypeClass is null");
TDApiRequest.Builder request = TDApiRequest.Builder.POST(path);
for (Map.Entry<String, String> e : queryParam.entrySet()) {
request.addQueryParam(e.getKey(), e.getValue());
}
if (jsonBody.isPresent()) {
request.setPostJson(jsonBody.get());
}
return httpClient.call(request.build(), apiKeyCache, resultTypeClass);
}
protected <ResultType> ResultType doPost(String path, Class<ResultType> resultTypeClass)
throws TDClientException
{
return this.<ResultType>doPost(path, Collections.emptyMap(), Optional.empty(), resultTypeClass);
}
protected <ResultType> ResultType doPost(String path, Map<String, String> queryParam, Class<ResultType> resultTypeClass)
throws TDClientException
{
return this.<ResultType>doPost(path, queryParam, Optional.empty(), resultTypeClass);
}
protected <ResultType> ResultType doPut(String path, Map<String, String> queryParam, File file, Class<ResultType> resultTypeClass)
throws TDClientException
{
requireNonNull(file, "file is null");
requireNonNull(resultTypeClass, "resultTypeClass is null");
TDApiRequest.Builder request = buildPutRequest(path, queryParam);
request.setFile(file);
return httpClient.call(request.build(), apiKeyCache, resultTypeClass);
}
protected <ResultType> ResultType doPut(String path, Map<String, String> queryParam, byte[] content, int offset, int length, Class<ResultType> resultTypeClass)
throws TDClientException
{
requireNonNull(content, "content is null");
requireNonNull(resultTypeClass, "resultTypeClass is null");
TDApiRequest.Builder request = buildPutRequest(path, queryParam);
request.setContent(content, offset, length);
return httpClient.call(request.build(), apiKeyCache, resultTypeClass);
}
private TDApiRequest.Builder buildPutRequest(String path, Map<String, String> queryParam)
{
requireNonNull(path, "path is null");
requireNonNull(queryParam, "param is null");
TDApiRequest.Builder request = TDApiRequest.Builder.PUT(path);
for (Map.Entry<String, String> e : queryParam.entrySet()) {
request.addQueryParam(e.getKey(), e.getValue());
}
return request;
}
protected String doPost(String path)
throws TDClientException
{
requireNonNull(path, "path is null");
TDApiRequest request = TDApiRequest.Builder.POST(path).build();
return httpClient.call(request, apiKeyCache);
}
protected String doPost(String path, Map<String, String> queryParam)
throws TDClientException
{
requireNonNull(path, "path is null");
requireNonNull(queryParam, "param is null");
TDApiRequest.Builder request = TDApiRequest.Builder.POST(path);
for (Map.Entry<String, String> e : queryParam.entrySet()) {
request.addQueryParam(e.getKey(), e.getValue());
}
return httpClient.call(request.build(), apiKeyCache);
}
protected String doPut(String path, File filePath)
throws TDClientException
{
requireNonNull(path, "path is null");
requireNonNull(filePath, "filePath is null");
TDApiRequest request = TDApiRequest.Builder.PUT(path).setFile(filePath).build();
return httpClient.call(request, apiKeyCache);
}
@Override
public TDClient authenticate(String email, String password)
{
Map<String, String> m = new HashMap<>();
m.put("user", email);
m.put("password", password);
TDAuthenticationResult authResult = doPost("/v3/user/authenticate", Collections.unmodifiableMap(m), TDAuthenticationResult.class);
return withApiKey(authResult.getApikey());
}
@Override
public TDUser getUser()
{
return doGet("/v3/user/show", TDUser.class);
}
@Override
public TDApiKey validateApiKey()
{
return doGet("/v3/user/apikey/validate", TDApiKey.class);
}
@Override
public TDUserList listUsers()
{
return doGet("/v3/user/list", TDUserList.class);
}
@Override
public String serverStatus()
{
// No API key is requried for server_status
return httpClient.call(TDApiRequest.Builder.GET("/v3/system/server_status").build(), Optional.empty());
}
@Override
public List<String> listDatabaseNames()
throws TDClientException
{
ArrayList<String> tableList = new ArrayList<>();
for (TDDatabase db : listDatabases()) {
tableList.add(db.getName());
}
return tableList;
}
@Override
public List<TDDatabase> listDatabases()
throws TDClientException
{
return doGet("/v3/database/list", new TypeReference<List<TDDatabase>>() {});
}
@Override
public TDDatabase showDatabase(String databaseName)
throws TDClientException
{
return doGet(buildUrl("/v3/database/show", databaseName), TDDatabase.class);
}
private static Pattern acceptableNamePattern = Pattern.compile("^([a-z0-9_]+)$");
static String validateDatabaseName(String databaseName)
{
return validateName(databaseName, "Database");
}
static String validateTableName(String tableName)
{
return validateName(tableName, "Table");
}
private static String validateName(String name, String type)
{
// Validate database name
if (name.length() < 3 || name.length() > 256) {
throw new TDClientException(
TDClientException.ErrorType.INVALID_INPUT, String.format(
"%s name must be 3 to 256 characters but got %d characters: %s", type, name.length(), name));
}
if (!acceptableNamePattern.matcher(name).matches()) {
throw new TDClientException(TDClientException.ErrorType.INVALID_INPUT,
String.format("%s name must follow this pattern %s: %s", type, acceptableNamePattern.pattern(), name));
}
return name;
}
@Override
public void createDatabase(String databaseName)
throws TDClientException
{
doPost(buildUrl("/v3/database/create", validateDatabaseName(databaseName)));
}
@Override
public void createDatabaseIfNotExists(String databaseName)
throws TDClientException
{
try {
createDatabase(databaseName);
}
catch (TDClientHttpConflictException e) {
// This can be thrown when the database already exists or Nginx returns conflict(409) upon request retry
}
}
@Override
public void deleteDatabase(String databaseName)
throws TDClientException
{
doPost(buildUrl("/v3/database/delete", validateDatabaseName(databaseName)));
}
@Override
public void deleteDatabaseIfExists(String databaseName)
throws TDClientException
{
try {
deleteDatabase(databaseName);
}
catch (TDClientHttpNotFoundException e) {
// This will be thrown when the database does not exist, or Nginx calls this delete request twice
}
}
@Override
public TDTable showTable(String databaseName, String tableName)
{
return doGet(buildUrl("/v3/table/show", databaseName, tableName), TDTable.class);
}
@Override
public List<TDTable> listTables(String databaseName)
throws TDClientException
{
TDTableList tableList = doGet(buildUrl("/v3/table/list", databaseName), TDTableList.class);
return tableList.getTables();
}
@Override
public boolean existsDatabase(String databaseName)
throws TDClientException
{
return listDatabaseNames().contains(databaseName);
}
@Override
public boolean existsTable(String databaseName, String tableName)
throws TDClientException
{
try {
for (TDTable table : listTables(databaseName)) {
if (table.getName().equals(tableName)) {
return true;
}
}
return false;
}
catch (TDClientHttpException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND_404) {
return false;
}
else {
throw e;
}
}
}
@Override
public void createTable(String databaseName, String tableName)
throws TDClientException
{
doPost(buildUrl("/v3/table/create", databaseName, validateTableName(tableName), TDTableType.LOG.getTypeName()));
}
@Override
public void createTable(String databaseName, String tableName, String idempotentKey)
throws TDClientException
{
// Idempotent key support is EXPERIMENTAL.
doPost(buildUrl("/v3/table/create", databaseName, validateTableName(tableName), TDTableType.LOG.getTypeName()),
Collections.singletonMap("idempotent_key", idempotentKey));
}
@Override
public void createTableIfNotExists(String databaseName, String tableName)
throws TDClientException
{
try {
createTable(databaseName, tableName);
}
catch (TDClientHttpConflictException e) {
// This can be thrown when the table already exists or Nginx returns conflict(409) upon request retry
}
}
@Override
public void renameTable(String databaseName, String tableName, String newTableName)
throws TDClientException
{
renameTable(databaseName, tableName, newTableName, false);
}
@Override
public void renameTable(String databaseName, String tableName, String newTableName, boolean overwrite)
throws TDClientException
{
doPost(buildUrl("/v3/table/rename", databaseName, tableName, validateTableName(newTableName)),
Collections.singletonMap("overwrite", Boolean.toString(overwrite)),
TDUpdateTableResult.class
);
}
@Override
public void deleteTable(String databaseName, String tableName)
throws TDClientException
{
doPost(buildUrl("/v3/table/delete", databaseName, tableName));
}
@Override
public void deleteTableIfExists(String databaseName, String tableName)
throws TDClientException
{
try {
deleteTable(databaseName, tableName);
}
catch (TDClientHttpNotFoundException e) {
// This will be thrown the table does not exists or Nginx calls this API request twice
}
}
@Override
public TDPartialDeleteJob partialDelete(String databaseName, String tableName, long from, long to)
throws TDClientException
{
return partialDelete(databaseName, tableName, from, to, null);
}
@Override
public TDPartialDeleteJob partialDelete(String databaseName, String tableName, long from, long to, String domainKey)
throws TDClientException
{
if ((from % 3600 != 0) || (to % 3600 != 0)) {
throw new TDClientException(TDClientException.ErrorType.INVALID_INPUT, String.format("from/to value must be a multiple of 3600: [%s, %s)", from, to));
}
Map<String, String> queryParams = new HashMap<>();
queryParams.put("from", Long.toString(from));
queryParams.put("to", Long.toString(to));
if (domainKey != null) {
queryParams.put("domain_key", domainKey);
}
TDPartialDeleteJob job = doPost(buildUrl("/v3/table/partialdelete", databaseName, tableName), Collections.unmodifiableMap(queryParams), TDPartialDeleteJob.class);
return job;
}
@Override
public void swapTables(String databaseName, String tableName1, String tableName2)
{
doPost(buildUrl("/v3/table/swap", databaseName, tableName1, tableName2));
}
@Override
public void updateTableSchema(String databaseName, String tableName, List<TDColumn> newSchema)
{
updateTableSchema(databaseName, tableName, newSchema, false);
}
@Override
public void updateTableSchema(String databaseName, String tableName, List<TDColumn> newSchema, boolean ignoreDuplicate)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(newSchema, "newSchema is null");
List<List<String>> builder = new ArrayList<>(newSchema.size());
for (TDColumn newColumn : newSchema) {
builder.add(Arrays.asList(newColumn.getKeyString(), newColumn.getType().toString(), newColumn.getName()));
}
Map<String, Object> m = new HashMap<>();
m.put("schema", Collections.unmodifiableList(builder));
m.put("ignore_duplicate_schema", ignoreDuplicate);
String schemaJson = toJSONString(m);
doPost(buildUrl("/v3/table/update-schema", databaseName, tableName), Collections.emptyMap(), Optional.of(schemaJson), String.class);
}
private static final ObjectMapper objectMapper = new ObjectMapper();
private static String toJSONString(Map<String, Object> map)
{
try {
return objectMapper.writeValueAsString(map);
}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Override
public void appendTableSchema(String databaseName, String tableName, List<TDColumn> appendedSchema)
{
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
requireNonNull(appendedSchema, "appendedSchema is null");
List<List<String>> builder = new ArrayList<>(appendedSchema.size());
for (TDColumn appendedColumn : appendedSchema) {
// Unlike update-schema API, append-schema API can generate alias for column name.
// So we should not pass `appendedColumn.getName()` here.
builder.add(Arrays.asList(appendedColumn.getKeyString(), appendedColumn.getType().toString()));
}
String schemaJson = toJSONString(Collections.singletonMap("schema", Collections.unmodifiableList(builder)));
doPost(buildUrl("/v3/table/append-schema", databaseName, tableName), Collections.emptyMap(), Optional.of(schemaJson), String.class);
}
@Override
public String submit(TDJobRequest jobRequest)
throws TDClientException
{
Map<String, String> queryParam = new HashMap<>();
queryParam.put("query", jobRequest.getQuery());
queryParam.put("version", getVersion());
if (jobRequest.getResultOutput().isPresent()) {
queryParam.put("result", jobRequest.getResultOutput().get());
}
queryParam.put("priority", Integer.toString(jobRequest.getPriority().toInt()));
if (jobRequest.getRetryLimit().isPresent()) {
queryParam.put("retry_limit", Integer.toString(jobRequest.getRetryLimit().get()));
}
if (jobRequest.getPoolName().isPresent()) {
queryParam.put("pool_name", jobRequest.getPoolName().get());
}
if (jobRequest.getTable().isPresent()) {
queryParam.put("table", jobRequest.getTable().get());
}
if (jobRequest.getScheduledTime().isPresent()) {
queryParam.put("scheduled_time", String.valueOf(jobRequest.getScheduledTime().get()));
}
if (jobRequest.getDomainKey().isPresent()) {
queryParam.put("domain_key", jobRequest.getDomainKey().get());
}
if (jobRequest.getResultConnectionId().isPresent()) {
queryParam.put("result_connection_id", String.valueOf(jobRequest.getResultConnectionId().get()));
}
if (jobRequest.getResultConnectionSettings().isPresent()) {
queryParam.put("result_connection_settings", jobRequest.getResultConnectionSettings().get());
}
if (jobRequest.getEngineVersion().isPresent()) {
queryParam.put("engine_version", jobRequest.getEngineVersion().get().getEngineVersion());
}
if (logger.isDebugEnabled()) {
logger.debug("submit job: " + jobRequest);
}
TDJobSubmitResult result =
doPost(
buildUrl("/v3/job/issue", jobRequest.getType().getType(), jobRequest.getDatabase()),
queryParam,
jobRequest.getConfig().map((config) -> {
ObjectNode body = config.objectNode();
body.set("config", config);
return body.toString();
}),
TDJobSubmitResult.class);
return result.getJobId();
}
@Override
public TDJobList listJobs()
throws TDClientException
{
return doGet("/v3/job/list", TDJobList.class);
}
@Override
public TDJobList listJobs(long from, long to)
throws TDClientException
{
return doGet(String.format("/v3/job/list?from=%d&to=%d", from, to), TDJobList.class);
}
@Override
public void killJob(String jobId)
throws TDClientException
{
doPost(buildUrl("/v3/job/kill", jobId));
}
@Override
public TDJobSummary jobStatus(String jobId)
throws TDClientException
{
return doGet(buildUrl("/v3/job/status", jobId), TDJobSummary.class);
}
@Override
public TDJobSummary jobStatusByDomainKey(String domainKey)
{
return doGet(buildUrl("/v3/job/status_by_domain_key", domainKey), TDJobSummary.class);
}
@Override
public TDJob jobInfo(String jobId)
throws TDClientException
{
return doGet(buildUrl("/v3/job/show", jobId), TDJob.class);
}
@Override
public <Result> Result jobResult(String jobId, TDResultFormat format, Function<InputStream, Result> resultStreamHandler)
throws TDClientException
{
return jobResult(jobId, format, false, resultStreamHandler);
}
@Override
public <Result> Result jobResult(String jobId, TDResultFormat format, boolean includeHeader, Function<InputStream, Result> resultStreamHandler)
throws TDClientException
{
TDApiRequest request = TDApiRequest.Builder
.GET(buildUrl("/v3/job/result", jobId))
.addQueryParam("format", format.getName())
.addQueryParam("header", Boolean.toString(includeHeader))
.build();
return httpClient.<Result>call(request, apiKeyCache, resultStreamHandler);
}
@Override
public List<TDBulkImportSession> listBulkImportSessions()
{
return doGet(buildUrl("/v3/bulk_import/list"), new TypeReference<List<TDBulkImportSession>>() {});
}
@Override
public List<String> listBulkImportParts(String sessionName)
{
return doGet(buildUrl("/v3/bulk_import/list_parts", sessionName), TDBulkImportParts.class).getParts();
}
@Override
public void createBulkImportSession(String sessionName, String databaseName, String tableName)
{
doPost(buildUrl("/v3/bulk_import/create", sessionName, databaseName, tableName));
}
@Override
public TDBulkImportSession getBulkImportSession(String sessionName)
{
return doGet(buildUrl("/v3/bulk_import/show", sessionName), TDBulkImportSession.class);
}
@Override
public void uploadBulkImportPart(String sessionName, String uniquePartName, File path)
{
doPut(buildUrl("/v3/bulk_import/upload_part", sessionName, uniquePartName), path);
}
public void deleteBulkImportPart(String sessionName, String uniquePartName)
{
doPost(buildUrl("/v3/bulk_import/delete_part", sessionName, uniquePartName));
}
@Override
public void freezeBulkImportSession(String sessionName)
{
doPost(buildUrl("/v3/bulk_import/freeze", sessionName));
}
@Override
public void unfreezeBulkImportSession(String sessionName)
{
doPost(buildUrl("/v3/bulk_import/unfreeze", sessionName));
}
@Override
public void performBulkImportSession(String sessionName)
{
performBulkImportSession(sessionName, TDJob.Priority.NORMAL);
}
@Override
public void performBulkImportSession(String sessionName, Optional<String> poolName)
{
performBulkImportSession(sessionName, poolName, TDJob.Priority.NORMAL);
}
@Override
public void performBulkImportSession(String sessionName, TDJob.Priority priority)
{
performBulkImportSession(sessionName, Optional.empty(), priority);
}
@Override
public void performBulkImportSession(String sessionName, Optional<String> poolName, TDJob.Priority priority)
{
Optional<String> jsonBody = Optional.empty();
if (poolName.isPresent()) {
jsonBody = Optional.of(toJSONString(Collections.singletonMap("pool_name", poolName.get())));
}
doPost(buildUrl("/v3/bulk_import/perform", sessionName), Collections.singletonMap("priority", Integer.toString(priority.toInt())), jsonBody, String.class);
}
@Override
public void commitBulkImportSession(String sessionName)
{
doPost(buildUrl("/v3/bulk_import/commit", sessionName));
}
@Override
public void deleteBulkImportSession(String sessionName)
{
doPost(buildUrl("/v3/bulk_import/delete", sessionName));
}
@Override
public <Result> Result getBulkImportErrorRecords(String sessionName, Function<InputStream, Result> resultStreamHandler)
{
TDApiRequest request = TDApiRequest.Builder
.GET(buildUrl("/v3/bulk_import/error_records", sessionName))
.build();
return httpClient.<Result>call(request, apiKeyCache, resultStreamHandler);
}
@Override
public String startSavedQuery(String name, Date scheduledTime)
{
return startSavedQuery(TDSavedQueryStartRequest.builder()
.name(name)
.scheduledTime(scheduledTime)
.build());
}
@Override
public String startSavedQuery(long id, Date scheduledTime)
{
return startSavedQueryV4(TDSavedQueryStartRequest.builder()
.name("")
.id(id)
.scheduledTime(scheduledTime)
.build());
}
@Override
public String startSavedQuery(TDSavedQueryStartRequest request)
{
if (request.id().isPresent()) {
return startSavedQueryV4(request);
}
else {
return startSavedQueryV3(request);
}
}
private String startSavedQueryV4(TDSavedQueryStartRequest request)
{
if (request.num().isPresent() && request.num().get() != 1) {
throw new UnsupportedOperationException("num must be 1");
}
TDSavedQueryStartResultV4 result =
doPost(buildUrl("/v4/queries", Long.toString(request.id().get()), "jobs"),
Collections.emptyMap(),
Optional.of(toJson(TDSavedQueryStartRequestV4.from(request))),
TDSavedQueryStartResultV4.class);
return result.getId();
}
private String startSavedQueryV3(TDSavedQueryStartRequest request)
{
Map<String, String> queryParams = new HashMap<>();
Optional<Integer> num = request.num();
if (num.isPresent()) {
queryParams.put("num", Integer.toString(num.get()));
}
Optional<String> domainKey = request.domainKey();
if (domainKey.isPresent()) {
queryParams.put("domain_key", domainKey.get());
}
TDScheduleRunResult result =
doPost(buildUrl("/v3/schedule/run", request.name(), Long.toString(request.scheduledTime().getTime() / 1000)),
queryParams,
TDScheduleRunResult.class);
return result.getJobs().get(0).getJobId();
}
@Override
public List<TDSavedQuery> listSavedQueries()
{
return doGet(buildUrl("/v3/schedule/list"), new TypeReference<List<TDSavedQuery>>() {});
}
@Override
public TDSavedQueryHistory getSavedQueryHistory(String name)
{
return getSavedQueryHistory(name, 0L, 20L);
}
@Override
public TDSavedQueryHistory getSavedQueryHistory(String name, Long from, Long to)
{
TDApiRequest.Builder builder = TDApiRequest.Builder
.GET(buildUrl("/v3/schedule/history", name));
if (from != null) {
builder.addQueryParam("from", String.valueOf(from));
}
if (to != null) {
builder.addQueryParam("to", String.valueOf(to));
}
TDApiRequest request = builder.build();
return httpClient.call(request, apiKeyCache, TDSavedQueryHistory.class);
}
protected String toJson(Object any)
{
try {
return httpClient.getObjectMapper().writeValueAsString(any);
}
catch (JsonProcessingException e) {
logger.error("Failed to produce json", e);
throw new TDClientException(TDClientException.ErrorType.INVALID_INPUT, String.format("Failed to create JSON string from %s", any));
}
}
@Override
public TDSavedQuery saveQuery(TDSaveQueryRequest request)
{
String json = toJson(request);
logger.debug("saveQuery request:" + json);
TDSavedQuery result =
doPost(
buildUrl("/v3/schedule/create", request.getName()),
Collections.emptyMap(),
Optional.of(json),
TDSavedQuery.class);
return result;
}
@Override
public TDSavedQuery updateSavedQuery(String name, TDSavedQueryUpdateRequest request)
{
String json = request.toJson();
logger.debug("updateSaveQuery request:" + json);
TDSavedQuery result =
doPost(
buildUrl("/v3/schedule/update", name),
Collections.emptyMap(),
Optional.of(json),
TDSavedQuery.class);
return result;
}
@Override
public TDSavedQuery deleteSavedQuery(String name)
{
TDSavedQuery result = doPost(
buildUrl("/v3/schedule/delete", name),