Search in sources :

Example 1 with TDSavedQuery

use of com.treasuredata.client.model.TDSavedQuery 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());
}
Also used : TDJob(com.treasuredata.client.model.TDJob) TDSavedQueryHistory(com.treasuredata.client.model.TDSavedQueryHistory) TDSavedQueryUpdateRequest(com.treasuredata.client.model.TDSavedQueryUpdateRequest) TDSavedQuery(com.treasuredata.client.model.TDSavedQuery) Date(java.util.Date) TDSaveQueryRequest(com.treasuredata.client.model.TDSaveQueryRequest)

Example 2 with TDSavedQuery

use of com.treasuredata.client.model.TDSavedQuery 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());
}
Also used : TDSavedQueryUpdateRequest(com.treasuredata.client.model.TDSavedQueryUpdateRequest) TDSavedQuery(com.treasuredata.client.model.TDSavedQuery) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TDSaveQueryRequest(com.treasuredata.client.model.TDSaveQueryRequest) Test(org.junit.Test)

Example 3 with TDSavedQuery

use of com.treasuredata.client.model.TDSavedQuery in project td-client-java by treasure-data.

the class TDClient method updateSavedQuery.

@Override
public TDSavedQuery updateSavedQuery(String name, TDSavedQueryUpdateRequest request) {
    String json = request.toJson();
    TDSavedQuery result = doPost(buildUrl("/v3/schedule/update", name), ImmutableMap.<String, String>of(), Optional.of(json), TDSavedQuery.class);
    return result;
}
Also used : TDSavedQuery(com.treasuredata.client.model.TDSavedQuery)

Example 4 with TDSavedQuery

use of com.treasuredata.client.model.TDSavedQuery in project td-client-java by treasure-data.

the class TDClient method saveQuery.

@Override
public TDSavedQuery saveQuery(TDSaveQueryRequest request) {
    String json = toJson(request);
    TDSavedQuery result = doPost(buildUrl("/v3/schedule/create", request.getName()), ImmutableMap.<String, String>of(), Optional.of(json), TDSavedQuery.class);
    return result;
}
Also used : TDSavedQuery(com.treasuredata.client.model.TDSavedQuery)

Example 5 with TDSavedQuery

use of com.treasuredata.client.model.TDSavedQuery in project td-client-java by treasure-data.

the class TestTDClient method getSavedQueryHistory.

@Test
public void getSavedQueryHistory() {
    List<TDSavedQuery> allQueries = client.listSavedQueries();
    List<TDSavedQuery> queries = FluentIterable.from(allQueries).limit(10).toList();
    for (TDSavedQuery query : queries) {
        TDSavedQueryHistory firstPage = client.getSavedQueryHistory(query.getName());
        logger.info("count: {}, from: {}, to: {}, jobs: {}", firstPage.getCount(), firstPage.getFrom(), firstPage.getTo(), firstPage.getHistory().size());
        TDSavedQueryHistory secondPage = client.getSavedQueryHistory(query.getName(), firstPage.getTo().get(), firstPage.getTo().get() + 20L);
        logger.info("count: {}, from: {}, to: {}, jobs: {}", secondPage.getCount(), secondPage.getFrom(), secondPage.getTo(), secondPage.getHistory().size());
    }
}
Also used : TDSavedQueryHistory(com.treasuredata.client.model.TDSavedQueryHistory) TDSavedQuery(com.treasuredata.client.model.TDSavedQuery) Test(org.junit.Test)

Aggregations

TDSavedQuery (com.treasuredata.client.model.TDSavedQuery)5 TDSaveQueryRequest (com.treasuredata.client.model.TDSaveQueryRequest)2 TDSavedQueryHistory (com.treasuredata.client.model.TDSavedQueryHistory)2 TDSavedQueryUpdateRequest (com.treasuredata.client.model.TDSavedQueryUpdateRequest)2 Test (org.junit.Test)2 TDJob (com.treasuredata.client.model.TDJob)1 Date (java.util.Date)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1