Search in sources :

Example 1 with ArrayValue

use of org.msgpack.value.ArrayValue 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 2 with ArrayValue

use of org.msgpack.value.ArrayValue 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)

Aggregations

TDJob (com.treasuredata.client.model.TDJob)2 TDJobSummary (com.treasuredata.client.model.TDJobSummary)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 MessageUnpacker (org.msgpack.core.MessageUnpacker)2 ArrayValue (org.msgpack.value.ArrayValue)2 TDDatabase (com.treasuredata.client.model.TDDatabase)1 TDTable (com.treasuredata.client.model.TDTable)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1