Search in sources :

Example 1 with TDDatabase

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

the class TestTDClient method listDatabases.

@Test
public void listDatabases() throws Exception {
    List<String> dbList = client.listDatabaseNames();
    assertTrue("should contain sample_datasets", dbList.contains("sample_datasets"));
    String dbListStr = Joiner.on(", ").join(dbList);
    logger.debug(dbListStr);
    List<TDDatabase> detailedDBList = client.listDatabases();
    Iterable<String> dbStr = Iterables.transform(detailedDBList, new Function<TDDatabase, String>() {

        @Override
        public String apply(TDDatabase input) {
            String summary = String.format("name:%s, count:%s, createdAt:%s, updatedAt:%s, organization:%s, permission:%s", input.getName(), input.getCount(), input.getCreatedAt(), input.getUpdatedAt(), input.getOrganization(), input.getPermission());
            return summary;
        }
    });
    String detailedDbListStr = Joiner.on(", ").join(dbStr);
    logger.trace(detailedDbListStr);
}
Also used : TDDatabase(com.treasuredata.client.model.TDDatabase) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with TDDatabase

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

TDDatabase (com.treasuredata.client.model.TDDatabase)2 TDJob (com.treasuredata.client.model.TDJob)1 TDJobSummary (com.treasuredata.client.model.TDJobSummary)1 TDTable (com.treasuredata.client.model.TDTable)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 Test (org.junit.Test)1 MessageUnpacker (org.msgpack.core.MessageUnpacker)1 ArrayValue (org.msgpack.value.ArrayValue)1