response.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
response.append("<response>\n");
try {
HBaseConnectionVariable hbVar = HBaseConnection.getConnection(connection);
// Check connection variable
if (hbVar == null) {
makeError(result, "There is no connection with name '" + connection + "'", false);
} else {
// Get HTable from pool
HTableInterface hTable = hbVar.getTable(table);
Scan scan = new Scan();
FilterList mainFilterList = new FilterList(Operator.MUST_PASS_ALL);
try {
if (filter != null && !"".equals(filter.trim())) {
request.append("\nfilters:[").append(filter).append("]\n\n");
Node node = FilterParser.parse(filter);
Filter filters = HBaseFilterParser.parse(node);
if (filters != null) {
mainFilterList.addFilter(filters);
}
}
} catch (Exception e) {
makeError(result, "Invalid filter syntax ".concat(e.getMessage()), true);
}
if (limit != null && !"".equals(limit.trim())) {
Long l = Long.valueOf(limit);
PageFilter pageFilter = new PageFilter(l);
mainFilterList.addFilter(pageFilter);
}
scan.setFilter(mainFilterList);
if (startKey != null && !"".equals(startKey.trim())) {
scan.setStartRow(Bytes.toBytes(startKey));
}
if (endKey != null && !"".equals(endKey.trim())) {
scan.setStopRow(Bytes.toBytes(endKey));
}
// Get data and convert to return SampleResult
// Data is in format :
// family:column_#=value - where # is column index
//
// Additional variable named by Hbase Scan name (getName()) is
// created with number of columns returned, e.g. 'HBase Scan
// Sampler_#'
ResultScanner resultScanner = hTable.getScanner(scan);
Iterator<Result> iter = resultScanner.iterator();
Integer i = 0;
while (iter.hasNext()) {
Result res = iter.next();
if (getLatestTimestampRows() == Boolean.TRUE)
{
response.append(Row2XML.row2xmlStringLatest(res, getOmitVars() ? vars : null, i + 1));
i++;
}
else
{
response.append(Row2XML.row2xmlstring(res, getOmitVars() ? vars : null, i + 1));
i++;
}
}
response.append("</response>\n");
// Set sampler data, type, success, ...
result.setResponseData(response.toString(), "UTF-8");
result.setDataType(SampleResult.TEXT);
result.setSuccessful(true);
result.setSamplerData(request.toString());
result.sampleEnd();
// Back to pool
hbVar.putTable(hTable);
}
} catch (Exception e) {
makeError(result, "Failed to access database : host:[" + connection + "] table:[" + table + "]\n" + e.getMessage(), false);
}