use of com.treasuredata.client.model.TDSaveQueryRequest in project td-client-java by treasure-data.
the class Example method saveQueryExample.
public static void saveQueryExample() {
TDClient client = TDClient.newClient();
// Register a new scheduled query
TDSaveQueryRequest query = TDSavedQuery.newBuilder("my_saved_query", TDJob.Type.PRESTO, "testdb", "select 1", "Asia/Tokyo").setCron("40 * * * *").setResult("mysql://testuser:pass@somemysql.address/somedb/sometable").build();
client.saveQuery(query);
// List saved queries
List<TDSavedQuery> savedQueries = client.listSavedQueries();
// Run a saved query
Date scheduledTime = new Date(System.currentTimeMillis());
client.startSavedQuery(query.getName(), scheduledTime);
// Get saved query job history (first page)
TDSavedQueryHistory firstPage = client.getSavedQueryHistory(query.getName());
// Get second page
long from = firstPage.getTo().get();
long to = from + 20;
TDSavedQueryHistory secondPage = client.getSavedQueryHistory(query.getName(), from, to);
// Get result of last job
TDJob lastJob = firstPage.getHistory().get(0);
System.out.println("Last job:" + lastJob);
// Update a saved query
TDSavedQueryUpdateRequest updateRequest = TDSavedQuery.newUpdateRequestBuilder().setQuery("select 2").setDelay(3600).build();
client.updateSavedQuery("my_saved_query", updateRequest);
// Delete a saved query
client.deleteSavedQuery(query.getName());
}
use of com.treasuredata.client.model.TDSaveQueryRequest in project td-client-java by treasure-data.
the class TestTDClient method saveAndDeleteQuery.
@Test
public void saveAndDeleteQuery() {
String queryName = newTemporaryName("td_client_test");
TDSaveQueryRequest query = TDSavedQuery.newBuilder(queryName, TDJob.Type.PRESTO, SAMPLE_DB, "select 1", "Asia/Tokyo").setCron("0 * * * *").setPriority(-1).setRetryLimit(2).setResult("mysql://testuser:pass@somemysql.address/somedb/sometable").build();
try {
TDSavedQuery result = client.saveQuery(query);
assertThat(result.getId(), not(isEmptyOrNullString()));
Optional<TDSavedQuery> q = findSavedQuery(queryName);
assertTrue(String.format("saved query %s is not found", queryName), q.isPresent());
validateSavedQuery(query, result);
// password will be hidden
assertTrue(result.getResult().startsWith("mysql://testuser:"));
assertTrue(result.getResult().contains("@somemysql.address/somedb/sometable"));
// Update
TDSavedQueryUpdateRequest query2 = TDSavedQuery.newUpdateRequestBuilder().setCron("15 * * * *").setType(TDJob.Type.HIVE).setQuery("select 2").setTimezone("UTC").setDelay(20).setDatabase(SAMPLE_DB).setPriority(-1).setRetryLimit(2).setResult("mysql://testuser2:pass@somemysql.address/somedb2/sometable2").build();
TDSaveQueryRequest expected = query2.merge(result);
TDSavedQuery updated = client.updateSavedQuery(queryName, query2);
assertThat(updated.getId(), is(result.getId()));
validateSavedQuery(expected, updated);
// password will be hidden
assertTrue(updated.getResult().startsWith("mysql://testuser2:"));
assertTrue(updated.getResult().contains("@somemysql.address/somedb2/sometable2"));
} catch (TDClientException e) {
logger.error("failed", e);
} finally {
client.deleteSavedQuery(queryName);
}
Optional<TDSavedQuery> q = findSavedQuery(queryName);
assertTrue(String.format("saved query %s should be deleted", queryName), !q.isPresent());
}
use of com.treasuredata.client.model.TDSaveQueryRequest in project td-client-java by treasure-data.
the class TestTDClient method startSavedQueryWithDomainKey.
@Test
public void startSavedQueryWithDomainKey() throws Exception {
String domainKey = randomDomainKey();
String queryName = newTemporaryName("td_client_test");
TDSaveQueryRequest query = TDSavedQuery.newBuilder(queryName, TDJob.Type.PRESTO, SAMPLE_DB, "select 1", "Asia/Tokyo").setCron("0 * * * *").setPriority(-1).setRetryLimit(2).setResult("mysql://testuser:pass@somemysql.address/somedb/sometable").build();
try {
client.saveQuery(query);
int epoch1 = 1457046001;
int epoch2 = epoch1 + 1;
// Claim the domain key
TDSavedQueryStartRequest request1 = TDSavedQueryStartRequest.builder().name(queryName).scheduledTime(new Date(epoch1 * 1000L)).domainKey(domainKey).build();
String jobId = client.startSavedQuery(request1);
// Attempt to use the same domain key again and verify that we get a conflict
TDSavedQueryStartRequest request2 = TDSavedQueryStartRequest.builder().name(queryName).scheduledTime(new Date(epoch2 * 1000L)).domainKey(domainKey).build();
try {
client.startSavedQuery(request2);
fail("Expected " + TDClientHttpConflictException.class.getName());
} catch (TDClientHttpConflictException e) {
assertThat(e.getConflictsWith(), is(Optional.of(jobId)));
}
} finally {
client.deleteSavedQuery(queryName);
}
}
Aggregations