Search in sources :

Example 1 with TDJobSummary

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

the class TestTDClient method invalidJobStatus.

@Test
public void invalidJobStatus() {
    try {
        TDJobSummary invalidJob = client.jobStatus("xxxxxx");
        logger.debug("invalid job: " + invalidJob);
        fail("should not reach here");
    } catch (TDClientException e) {
        assertEquals(TDClientException.ErrorType.TARGET_NOT_FOUND, e.getErrorType());
    }
}
Also used : TDJobSummary(com.treasuredata.client.model.TDJobSummary) Test(org.junit.Test)

Example 2 with TDJobSummary

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

the class TestTDClient method submitExportJob.

@Test
public void submitExportJob() throws Exception {
    TDExportJobRequest jobRequest = new TDExportJobRequest(SAMPLE_DB, "sample_output", new Date(0L), new Date(1456522300L * 1000), TDExportFileFormatType.JSONL_GZ, "access key id", "secret access key", "bucket", "prefix/", Optional.<String>absent());
    client.createDatabaseIfNotExists(SAMPLE_DB);
    client.createTableIfNotExists(SAMPLE_DB, "sample_output");
    String jobId = client.submitExportJob(jobRequest);
    TDJobSummary tdJob = waitJobCompletion(jobId);
// this job will do nothing because sample_output table is empty
}
Also used : TDExportJobRequest(com.treasuredata.client.model.TDExportJobRequest) 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 3 with TDJobSummary

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

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

the class TestTDClient method submitJobWithResultOutput.

@Test
public void submitJobWithResultOutput() throws Exception {
    client.deleteTableIfExists(SAMPLE_DB, "sample_output");
    String resultOutput = String.format("td://%s@/%s/sample_output?mode=replace", client.config.apiKey.get(), SAMPLE_DB);
    String jobId = client.submit(TDJobRequest.newPrestoQuery("sample_datasets", "-- td-client-java test\nselect count(*) from nasdaq", resultOutput));
    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)

Example 5 with TDJobSummary

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

the class TestTDClient method submitPrestoJobWithInvalidPoolName.

@Test
public void submitPrestoJobWithInvalidPoolName() throws Exception {
    exception.expect(TDClientHttpException.class);
    exception.expectMessage("Presto resource pool with name 'no_such_pool' does not exist");
    client.deleteTableIfExists(SAMPLE_DB, "sample_output");
    String poolName = "no_such_pool";
    String jobId = client.submit(TDJobRequest.newPrestoQuery("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