Search in sources :

Example 6 with TDJobSummary

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

the class TestTDClient method waitJobCompletion.

private TDJobSummary waitJobCompletion(String jobId) throws InterruptedException {
    int retryCount = 0;
    ExponentialBackOff backoff = new ExponentialBackOff();
    long deadline = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10);
    TDJobSummary tdJob = null;
    do {
        if (System.currentTimeMillis() > deadline) {
            throw new IllegalStateException(String.format("waiting job %s has timed out", jobId));
        }
        int nextWait = backoff.nextWaitTimeMillis();
        logger.debug(String.format("Run job status check in %.2f sec.", nextWait / 1000.0));
        Thread.sleep(nextWait);
        tdJob = client.jobStatus(jobId);
        logger.debug("job status: " + tdJob);
        retryCount++;
    } while (retryCount < 10 && !tdJob.getStatus().isFinished());
    return tdJob;
}
Also used : TDJobSummary(com.treasuredata.client.model.TDJobSummary)

Example 7 with TDJobSummary

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

the class TestTDClient method startSavedQuery.

@Test
public void startSavedQuery() throws Exception {
    Date scheduledTime = new Date(1457046000 * 1000L);
    try {
        String jobId = client.startSavedQuery("no method to save a schedule yet", scheduledTime);
        TDJobSummary tdJob = waitJobCompletion(jobId);
        fail();
    } catch (TDClientHttpNotFoundException e) {
    // OK
    }
}
Also used : Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TDJobSummary(com.treasuredata.client.model.TDJobSummary) Date(java.util.Date) Test(org.junit.Test)

Example 8 with TDJobSummary

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

the class TestTDClient method killJob.

@Test
public void killJob() throws Exception {
    String jobId = client.submit(TDJobRequest.newPrestoQuery("sample_datasets", "-- td-client-java job kill test\n select time from nasdaq"));
    client.killJob(jobId);
    waitJobCompletion(jobId);
    TDJobSummary summary = client.jobStatus(jobId);
    assertEquals(TDJob.Status.KILLED, summary.getStatus());
}
Also used : Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TDJobSummary(com.treasuredata.client.model.TDJobSummary) Test(org.junit.Test)

Example 9 with TDJobSummary

use of com.treasuredata.client.model.TDJobSummary 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 10 with TDJobSummary

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

the class TestTDClient method submitJobWithPoolName.

@Test
public void submitJobWithPoolName() throws Exception {
    client.deleteTableIfExists(SAMPLE_DB, "sample_output");
    String poolName = "hadoop2";
    String jobId = client.submit(TDJobRequest.newHiveQuery("sample_datasets", "-- td-client-java test\nselect count(*) from nasdaq", null, poolName));
    TDJobSummary tdJob = waitJobCompletion(jobId);
    client.existsTable(SAMPLE_DB, "sample_output");
}
Also used : Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) TDJobSummary(com.treasuredata.client.model.TDJobSummary) Test(org.junit.Test)

Aggregations

TDJobSummary (com.treasuredata.client.model.TDJobSummary)12 Test (org.junit.Test)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)9 TDJob (com.treasuredata.client.model.TDJob)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 MessageUnpacker (org.msgpack.core.MessageUnpacker)2 ArrayValue (org.msgpack.value.ArrayValue)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TDDatabase (com.treasuredata.client.model.TDDatabase)1 TDExportJobRequest (com.treasuredata.client.model.TDExportJobRequest)1 TDJobRequest (com.treasuredata.client.model.TDJobRequest)1 TDJobRequestBuilder (com.treasuredata.client.model.TDJobRequestBuilder)1 TDTable (com.treasuredata.client.model.TDTable)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1