@Override
public void process(String[] args, PrintStream output) throws Exception {
super.process(args, output);
String query = Joiner.on(" ").join(args);
QueryHandle queryHandle = queryClient.execute(query);
QueryStatus status = new QueryStatus(null, false);
long startTime = System.currentTimeMillis();
while (QueryStatus.OpStatus.RUNNING == status.getStatus() ||
QueryStatus.OpStatus.INITIALIZED == status.getStatus() ||
QueryStatus.OpStatus.PENDING == status.getStatus()) {
Thread.sleep(1000);
status = queryClient.getStatus(queryHandle);
}
if (status.hasResults()) {
List<ColumnDesc> schema = queryClient.getSchema(queryHandle);
String[] header = new String[schema.size()];
for (int i = 0; i < header.length; i++) {
ColumnDesc column = schema.get(i);
// Hive columns start at 1
int index = column.getPosition() - 1;
header[index] = column.getName() + ": " + column.getType();
}
List<QueryResult> results = queryClient.getResults(queryHandle, 20);
new AsciiTable<QueryResult>(header, results, new RowMaker<QueryResult>() {
@Override
public Object[] makeRow(QueryResult object) {
return object.getColumns().toArray(new Object[object.getColumns().size()]);
}
}).print(output);
queryClient.delete(queryHandle);
} else {
output.println("Couldn't obtain results after " + (System.currentTimeMillis() - startTime) + "ms. " +
"Try querying manually with handle " + queryHandle.getHandle());
}
}