Search in sources :

Example 1 with ResultsReaderJson

use of com.splunk.ResultsReaderJson in project camel by apache.

the class SplunkDataReader method extractData.

private List<SplunkEvent> extractData(Job job, boolean realtime, SplunkResultProcessor callback) throws Exception {
    List<SplunkEvent> result = new ArrayList<SplunkEvent>();
    HashMap<String, String> data;
    SplunkEvent splunkData;
    ResultsReader resultsReader = null;
    int total = 0;
    if (realtime) {
        total = job.getResultPreviewCount();
    } else {
        total = job.getResultCount();
    }
    if (getCount() == 0 || total < getCount()) {
        InputStream stream = null;
        JobResultsArgs outputArgs = new JobResultsArgs();
        outputArgs.setOutputMode(OutputMode.JSON);
        if (realtime) {
            if (getCount() > 0) {
                outputArgs.setCount(getCount());
            }
            stream = job.getResultsPreview(outputArgs);
        } else {
            stream = job.getResults(outputArgs);
        }
        resultsReader = new ResultsReaderJson(stream);
        while ((data = resultsReader.getNextEvent()) != null) {
            splunkData = new SplunkEvent(data);
            if (callback != null) {
                callback.process(splunkData);
            } else {
                result.add(splunkData);
            }
        }
        IOHelper.close(stream);
    } else {
        int offset = 0;
        while (offset < total) {
            InputStream stream;
            JobResultsArgs outputArgs = new JobResultsArgs();
            outputArgs.setOutputMode(OutputMode.JSON);
            outputArgs.setCount(getCount());
            outputArgs.setOffset(offset);
            if (realtime) {
                stream = job.getResultsPreview(outputArgs);
            } else {
                stream = job.getResults(outputArgs);
            }
            resultsReader = new ResultsReaderJson(stream);
            while ((data = resultsReader.getNextEvent()) != null) {
                splunkData = new SplunkEvent(data);
                if (callback != null) {
                    callback.process(splunkData);
                } else {
                    result.add(splunkData);
                }
            }
            offset += getCount();
            IOHelper.close(stream);
        }
    }
    if (resultsReader != null) {
        resultsReader.close();
    }
    job.cancel();
    return result;
}
Also used : SplunkEvent(org.apache.camel.component.splunk.event.SplunkEvent) ResultsReader(com.splunk.ResultsReader) JobResultsArgs(com.splunk.JobResultsArgs) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SplunkEndpoint(org.apache.camel.component.splunk.SplunkEndpoint) ResultsReaderJson(com.splunk.ResultsReaderJson)

Aggregations

JobResultsArgs (com.splunk.JobResultsArgs)1 ResultsReader (com.splunk.ResultsReader)1 ResultsReaderJson (com.splunk.ResultsReaderJson)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 SplunkEndpoint (org.apache.camel.component.splunk.SplunkEndpoint)1 SplunkEvent (org.apache.camel.component.splunk.event.SplunkEvent)1