Search in sources :

Example 1 with TDJob

use of com.treasuredata.client.model.TDJob 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 TDJob

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

the class TestTDClient method submitJob.

@Test
public void submitJob() throws Exception {
    String jobId = client.submit(TDJobRequest.newPrestoQuery("sample_datasets", "-- td-client-java test\nselect count(*) cnt from nasdaq"));
    logger.debug("job id: " + jobId);
    int retryCount = 0;
    TDJobSummary tdJob = waitJobCompletion(jobId);
    TDJob jobInfo = client.jobInfo(jobId);
    logger.debug("job show result: " + tdJob);
    logger.debug("job info: " + jobInfo);
    Optional<String> schema = jobInfo.getResultSchema();
    assertTrue(schema.isPresent());
    assertEquals("[[\"cnt\", \"bigint\"]]", schema.get());
    JSONArray array = client.jobResult(jobId, TDResultFormat.JSON, new Function<InputStream, JSONArray>() {

        @Override
        public JSONArray apply(InputStream input) {
            try {
                String result = new String(ByteStreams.toByteArray(input), StandardCharsets.UTF_8);
                logger.info("result:\n" + result);
                return new JSONArray(result);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    });
    assertEquals(1, array.length());
    assertEquals(8807278, array.getLong(0));
    // test msgpack.gz format
    client.jobResult(jobId, TDResultFormat.MESSAGE_PACK_GZ, new Function<InputStream, Object>() {

        @Override
        public Object apply(InputStream input) {
            try {
                logger.debug("Reading job result in msgpack.gz");
                MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new GZIPInputStream(input));
                int rowCount = 0;
                while (unpacker.hasNext()) {
                    ArrayValue array = unpacker.unpackValue().asArrayValue();
                    assertEquals(1, array.size());
                    int numColumns = array.get(0).asIntegerValue().toInt();
                    assertEquals(8807278, numColumns);
                    rowCount++;
                }
                assertEquals(rowCount, 1);
                return null;
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }
    });
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) JSONException(org.json.JSONException) ExpectedException(org.junit.rules.ExpectedException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) TDJob(com.treasuredata.client.model.TDJob) MessageUnpacker(org.msgpack.core.MessageUnpacker) JSONObject(org.json.JSONObject) TDJobSummary(com.treasuredata.client.model.TDJobSummary) ArrayValue(org.msgpack.value.ArrayValue) Test(org.junit.Test)

Example 3 with TDJob

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

the class Example method main.

public static void main(String[] args) {
    TDClient client = TDClient.newClient();
    try {
        // Retrieve database and table names
        List<TDDatabase> databases = client.listDatabases();
        TDDatabase db = databases.get(0);
        System.out.println("database: " + db.getName());
        for (TDTable table : client.listTables(db.getName())) {
            System.out.println(" table: " + table);
        }
        // Submit a new Presto query
        String jobId = client.submit(TDJobRequest.newPrestoQuery("sample_datasets", "select count(1) cnt from www_access"));
        // Wait until the query finishes
        ExponentialBackOff backOff = new ExponentialBackOff();
        TDJobSummary job = client.jobStatus(jobId);
        while (!job.getStatus().isFinished()) {
            Thread.sleep(backOff.nextWaitTimeMillis());
            job = client.jobStatus(jobId);
        }
        // Read the detailed job information
        TDJob jobInfo = client.jobInfo(jobId);
        System.out.println("log:\n" + jobInfo.getCmdOut());
        System.out.println("error log:\n" + jobInfo.getStdErr());
        // Read the job results in msgpack.gz format
        client.jobResult(jobId, TDResultFormat.MESSAGE_PACK_GZ, new Function<InputStream, Integer>() {

            @Override
            public Integer apply(InputStream input) {
                int count = 0;
                try {
                    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new GZIPInputStream(input));
                    while (unpacker.hasNext()) {
                        // Each row of the query result is array type value (e.g., [1, "name", ...])
                        ArrayValue array = unpacker.unpackValue().asArrayValue();
                        System.out.println(array);
                        count++;
                    }
                    unpacker.close();
                } catch (Exception e) {
                    throw Throwables.propagate(e);
                }
                return count;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        client.close();
    }
}
Also used : TDDatabase(com.treasuredata.client.model.TDDatabase) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) GZIPInputStream(java.util.zip.GZIPInputStream) TDJob(com.treasuredata.client.model.TDJob) MessageUnpacker(org.msgpack.core.MessageUnpacker) TDJobSummary(com.treasuredata.client.model.TDJobSummary) TDTable(com.treasuredata.client.model.TDTable) ArrayValue(org.msgpack.value.ArrayValue)

Example 4 with TDJob

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

the class TestTDClient method listJobs.

@Test
public void listJobs() throws Exception {
    TDJobList jobs = client.listJobs();
    logger.debug("job list: " + jobs);
    TDJobList jobsInAnIDRange = client.listJobs(34022478, 34022600);
    logger.debug("job list: " + jobsInAnIDRange);
    assertTrue(jobsInAnIDRange.getJobs().size() > 0);
    // Check getters
    Iterable<Method> getters = FluentIterable.from(TDJob.class.getDeclaredMethods()).filter(new Predicate<Method>() {

        @Override
        public boolean apply(Method input) {
            return test(input);
        }

        @Override
        public boolean test(Method input) {
            return input.getName().startsWith("get");
        }
    });
    // Call getters
    for (TDJob job : jobs.getJobs()) {
        for (Method m : getters) {
            m.invoke(job);
        }
    }
}
Also used : TDJob(com.treasuredata.client.model.TDJob) Method(java.lang.reflect.Method) TDJobList(com.treasuredata.client.model.TDJobList) Test(org.junit.Test)

Aggregations

TDJob (com.treasuredata.client.model.TDJob)4 TDJobSummary (com.treasuredata.client.model.TDJobSummary)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Test (org.junit.Test)2 MessageUnpacker (org.msgpack.core.MessageUnpacker)2 ArrayValue (org.msgpack.value.ArrayValue)2 TDDatabase (com.treasuredata.client.model.TDDatabase)1 TDJobList (com.treasuredata.client.model.TDJobList)1 TDSaveQueryRequest (com.treasuredata.client.model.TDSaveQueryRequest)1 TDSavedQuery (com.treasuredata.client.model.TDSavedQuery)1 TDSavedQueryHistory (com.treasuredata.client.model.TDSavedQueryHistory)1 TDSavedQueryUpdateRequest (com.treasuredata.client.model.TDSavedQueryUpdateRequest)1 TDTable (com.treasuredata.client.model.TDTable)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 Date (java.util.Date)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1