Search in sources :

Example 1 with TDTable

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

the class TestProxyAccess method proxyApiAccess.

@Test
public void proxyApiAccess() {
    TDClient client = TDClient.newBuilder().setProxy(proxyBaseConfig()).build();
    try {
        client.serverStatus();
        List<TDTable> tableList = client.listTables("sample_datasets");
        assertTrue(tableList.size() >= 2);
        TDJobList jobList = client.listJobs();
        assertTrue(jobList.getJobs().size() > 0);
    } finally {
        logger.debug("proxy access count: {}", proxyAccessCount);
        assertEquals(1, proxyAccessCount.get());
    }
}
Also used : TDTable(com.treasuredata.client.model.TDTable) TDJobList(com.treasuredata.client.model.TDJobList) Test(org.junit.Test)

Example 2 with TDTable

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

the class TestSSLProxyAccess method proxyApiAccess.

@Test
public void proxyApiAccess() {
    ProxyConfig.ProxyConfigBuilder proxy = new ProxyConfig.ProxyConfigBuilder();
    proxy.useSSL(true);
    proxy.setHost("localhost");
    proxy.setPort(proxyPort);
    proxy.setUser(PROXY_USER);
    proxy.setPassword(PROXY_PASS);
    TDClient client = TDClient.newBuilder().setProxy(proxy.createProxyConfig()).build();
    try {
        client.serverStatus();
        List<TDTable> tableList = client.listTables("sample_datasets");
        assertTrue(tableList.size() >= 2);
        TDJobList jobList = client.listJobs();
        assertTrue(jobList.getJobs().size() > 0);
    } finally {
        logger.debug("proxy access count: {}", proxyAccessCount);
        assertEquals(1, proxyAccessCount.get());
    }
}
Also used : TDTable(com.treasuredata.client.model.TDTable) TDJobList(com.treasuredata.client.model.TDJobList) Test(org.junit.Test)

Example 3 with TDTable

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

the class TestTDClient method showTables.

@Test
public void showTables() throws Exception {
    TDTable table;
    // nasdaq
    table = client.showTable("sample_datasets", "nasdaq");
    assertTrue(table.getColumns().size() == 6);
    // www_access
    table = client.showTable("sample_datasets", "www_access");
    assertTrue(table.getColumns().size() == 8);
}
Also used : TDTable(com.treasuredata.client.model.TDTable) Test(org.junit.Test)

Example 4 with TDTable

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

the class TestTDClient method authenticate.

@Test
public void authenticate() throws Exception {
    // authenticate() method should retrieve apikey, and set it to the TDClient
    // [NOTE] To pass this you need to add password config to ~/.td/td.conf
    Properties p = TDClientConfig.readTDConf();
    // Set no API key
    TDClient client = new TDClientBuilder(false).build();
    String user = firstNonNull(p.getProperty("user"), System.getenv("TD_USER"));
    String password = firstNonNull(p.getProperty("password"), System.getenv("TD_PASS"));
    TDClient newClient = client.authenticate(user, password);
    List<TDTable> tableList = newClient.listTables("sample_datasets");
    assertTrue(tableList.size() >= 2);
}
Also used : Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Properties(java.util.Properties) TDTable(com.treasuredata.client.model.TDTable) Test(org.junit.Test)

Example 5 with TDTable

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

TDTable (com.treasuredata.client.model.TDTable)8 Test (org.junit.Test)7 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 TDColumn (com.treasuredata.client.model.TDColumn)2 TDJobList (com.treasuredata.client.model.TDJobList)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 MessageUnpacker (org.msgpack.core.MessageUnpacker)2 ArrayValue (org.msgpack.value.ArrayValue)2 TDBulkImportSession (com.treasuredata.client.model.TDBulkImportSession)1 TDDatabase (com.treasuredata.client.model.TDDatabase)1 TDJob (com.treasuredata.client.model.TDJob)1 TDJobSummary (com.treasuredata.client.model.TDJobSummary)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1