Search in sources :

Example 1 with ResultsReaderXml

use of com.splunk.ResultsReaderXml in project hop by apache.

the class SplunkInput method processRow.

@Override
public boolean processRow() throws HopException {
    if (first) {
        first = false;
        // get the output fields...
        // 
        data.outputRowMeta = new RowMeta();
        meta.getFields(data.outputRowMeta, getTransformName(), null, getTransformMeta(), this, metadataProvider);
        // Run a one shot search in blocking mode
        // 
        JobResultsArgs args = new JobResultsArgs();
        args.setCount(0);
        args.setOutputMode(JobResultsArgs.OutputMode.XML);
        data.eventsStream = data.service.oneshotSearch(resolve(meta.getQuery()), args);
    }
    try {
        ResultsReaderXml resultsReader = new ResultsReaderXml(data.eventsStream);
        HashMap<String, String> event;
        while ((event = resultsReader.getNextEvent()) != null) {
            Object[] outputRow = RowDataUtil.allocateRowData(data.outputRowMeta.size());
            for (int i = 0; i < meta.getReturnValues().size(); i++) {
                ReturnValue returnValue = meta.getReturnValues().get(i);
                String value = event.get(returnValue.getSplunkName());
                outputRow[i] = value;
            }
            incrementLinesInput();
            putRow(data.outputRowMeta, outputRow);
        }
    } catch (Exception e) {
        throw new HopException("Error reading from Splunk events stream", e);
    } finally {
        try {
            data.eventsStream.close();
        } catch (IOException e) {
            throw new HopException("Unable to close events stream", e);
        }
    }
    // Nothing more
    // 
    setOutputDone();
    return false;
}
Also used : JobResultsArgs(com.splunk.JobResultsArgs) RowMeta(org.apache.hop.core.row.RowMeta) HopException(org.apache.hop.core.exception.HopException) ResultsReaderXml(com.splunk.ResultsReaderXml) IOException(java.io.IOException) HopException(org.apache.hop.core.exception.HopException) IOException(java.io.IOException)

Example 2 with ResultsReaderXml

use of com.splunk.ResultsReaderXml in project hop by apache.

the class SplunkInputDialog method getReturnValues.

private void getReturnValues() {
    try {
        IHopMetadataSerializer<SplunkConnection> serializer = metadataProvider.getSerializer(SplunkConnection.class);
        SplunkConnection splunkConnection = serializer.load(variables.resolve(wConnection.getText()));
        Service service = Service.connect(splunkConnection.getServiceArgs(variables));
        Args args = new Args();
        args.put("connection_mode", JobArgs.ExecutionMode.BLOCKING.name());
        InputStream eventsStream = service.oneshotSearch(variables.resolve(wQuery.getText()), args);
        Set<String> detectedKeys = new HashSet<>();
        try {
            ResultsReaderXml resultsReader = new ResultsReaderXml(eventsStream);
            HashMap<String, String> event;
            int nrScanned = 0;
            while ((event = resultsReader.getNextEvent()) != null) {
                for (String key : event.keySet()) {
                    detectedKeys.add(key);
                }
                nrScanned++;
                if (nrScanned > 10) {
                    break;
                }
            }
        } finally {
            eventsStream.close();
        }
        for (String detectedKey : detectedKeys) {
            TableItem item = new TableItem(wReturns.table, SWT.NONE);
            item.setText(1, detectedKey);
            item.setText(2, detectedKey);
            item.setText(3, "String");
        }
        wReturns.removeEmptyRows();
        wReturns.setRowNums();
        wReturns.optWidth(true);
    } catch (Exception e) {
        new ErrorDialog(shell, "Error", "Error getting fields from Splunk query", e);
    }
}
Also used : Args(com.splunk.Args) JobArgs(com.splunk.JobArgs) InputStream(java.io.InputStream) Service(com.splunk.Service) ResultsReaderXml(com.splunk.ResultsReaderXml) SplunkConnection(org.apache.hop.splunk.SplunkConnection)

Aggregations

ResultsReaderXml (com.splunk.ResultsReaderXml)2 Args (com.splunk.Args)1 JobArgs (com.splunk.JobArgs)1 JobResultsArgs (com.splunk.JobResultsArgs)1 Service (com.splunk.Service)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HopException (org.apache.hop.core.exception.HopException)1 RowMeta (org.apache.hop.core.row.RowMeta)1 SplunkConnection (org.apache.hop.splunk.SplunkConnection)1