use of com.treasuredata.client.model.TDJob in project td-client-java by treasure-data.
the class Example method saveQueryExample.
public static void saveQueryExample() {
TDClient client = TDClient.newClient();
// Register a new scheduled query
TDSaveQueryRequest query = TDSavedQuery.newBuilder("my_saved_query", TDJob.Type.PRESTO, "testdb", "select 1", "Asia/Tokyo").setCron("40 * * * *").setResult("mysql://testuser:pass@somemysql.address/somedb/sometable").build();
client.saveQuery(query);
// List saved queries
List<TDSavedQuery> savedQueries = client.listSavedQueries();
// Run a saved query
Date scheduledTime = new Date(System.currentTimeMillis());
client.startSavedQuery(query.getName(), scheduledTime);
// Get saved query job history (first page)
TDSavedQueryHistory firstPage = client.getSavedQueryHistory(query.getName());
// Get second page
long from = firstPage.getTo().get();
long to = from + 20;
TDSavedQueryHistory secondPage = client.getSavedQueryHistory(query.getName(), from, to);
// Get result of last job
TDJob lastJob = firstPage.getHistory().get(0);
System.out.println("Last job:" + lastJob);
// Update a saved query
TDSavedQueryUpdateRequest updateRequest = TDSavedQuery.newUpdateRequestBuilder().setQuery("select 2").setDelay(3600).build();
client.updateSavedQuery("my_saved_query", updateRequest);
// Delete a saved query
client.deleteSavedQuery(query.getName());
}
use of com.treasuredata.client.model.TDJob 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);
}
}
});
}
use of com.treasuredata.client.model.TDJob 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();
}
}
use of com.treasuredata.client.model.TDJob in project td-client-java by treasure-data.
the class TestTDClient method listJobs.
@Test
public void listJobs() throws Exception {
TDJobList jobs = client.listJobs();
logger.debug("job list: " + jobs);
TDJobList jobsInAnIDRange = client.listJobs(34022478, 34022600);
logger.debug("job list: " + jobsInAnIDRange);
assertTrue(jobsInAnIDRange.getJobs().size() > 0);
// Check getters
Iterable<Method> getters = FluentIterable.from(TDJob.class.getDeclaredMethods()).filter(new Predicate<Method>() {
@Override
public boolean apply(Method input) {
return test(input);
}
@Override
public boolean test(Method input) {
return input.getName().startsWith("get");
}
});
// Call getters
for (TDJob job : jobs.getJobs()) {
for (Method m : getters) {
m.invoke(job);
}
}
}
Aggregations