use of com.treasuredata.client.model.TDSavedQueryUpdateRequest 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.TDSavedQueryUpdateRequest 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());
}
Aggregations