use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method testDelete.
@Test
public void testDelete() throws Exception {
//first, INDEX a value
Map<String, String> map = createIndexedData();
sendBody("direct:index", map);
String indexId = template.requestBody("direct:index", map, String.class);
assertNotNull("indexId should be set", indexId);
//now, verify GET succeeded
GetResponse response = template.requestBody("direct:get", indexId, GetResponse.class);
assertNotNull("response should not be null", response);
assertNotNull("response source should not be null", response.getSource());
//now, perform DELETE
DeleteResponse deleteResponse = template.requestBody("direct:delete", indexId, DeleteResponse.class);
assertNotNull("response should not be null", deleteResponse);
//now, verify GET fails to find the indexed value
response = template.requestBody("direct:get", indexId, GetResponse.class);
assertNotNull("response should not be null", response);
assertNull("response source should be null", response.getSource());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method testDeleteWithHeaders.
@Test
public void testDeleteWithHeaders() throws Exception {
//first, INDEX a value
Map<String, String> map = createIndexedData();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX);
headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class);
//now, verify GET
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID);
GetResponse response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class);
assertNotNull("response should not be null", response);
assertNotNull("response source should not be null", response.getSource());
//now, perform DELETE
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_DELETE);
DeleteResponse deleteResponse = template.requestBodyAndHeaders("direct:start", indexId, headers, DeleteResponse.class);
assertNotNull("response should not be null", deleteResponse);
//now, verify GET fails to find the indexed value
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_GET_BY_ID);
response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class);
assertNotNull("response should not be null", response);
assertNull("response source should be null", response.getSource());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method getRequestBody.
@Test
public void getRequestBody() throws Exception {
String prefix = createPrefix();
// given
GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar");
// when
String documentId = template.requestBody("direct:index", new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
GetResponse response = template.requestBody("direct:get", request.id(documentId), GetResponse.class);
// then
assertThat(response, notNullValue());
assertThat(prefix + "hello", equalTo(response.getSourceAsMap().get(prefix + "content")));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project nifi by apache.
the class FetchElasticsearch5 method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
synchronized (esClient) {
if (esClient.get() == null) {
super.setup(context);
}
}
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final String index = context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue();
final String docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(flowFile).getValue();
final String docType = context.getProperty(TYPE).evaluateAttributeExpressions(flowFile).getValue();
final Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());
final ComponentLog logger = getLogger();
try {
logger.debug("Fetching {}/{}/{} from Elasticsearch", new Object[] { index, docType, docId });
GetRequestBuilder getRequestBuilder = esClient.get().prepareGet(index, docType, docId);
final GetResponse getResponse = getRequestBuilder.execute().actionGet();
if (getResponse == null || !getResponse.isExists()) {
logger.debug("Failed to read {}/{}/{} from Elasticsearch: Document not found", new Object[] { index, docType, docId });
// We couldn't find the document, so penalize it and send it to "not found"
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_NOT_FOUND);
} else {
flowFile = session.putAllAttributes(flowFile, new HashMap<String, String>() {
{
put("filename", docId);
put("es.index", index);
put("es.type", docType);
}
});
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
out.write(getResponse.getSourceAsString().getBytes(charset));
}
});
logger.debug("Elasticsearch document " + docId + " fetched, routing to success");
// The document is JSON, so update the MIME type of the flow file
flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json");
session.getProvenanceReporter().fetch(flowFile, getResponse.remoteAddress().getAddress());
session.transfer(flowFile, REL_SUCCESS);
}
} catch (NoNodeAvailableException | ElasticsearchTimeoutException | ReceiveTimeoutTransportException | NodeClosedException exceptionToRetry) {
logger.error("Failed to read into Elasticsearch due to {}, this may indicate an error in configuration " + "(hosts, username/password, etc.), or this issue may be transient. Routing to retry", new Object[] { exceptionToRetry.getLocalizedMessage() }, exceptionToRetry);
session.transfer(flowFile, REL_RETRY);
context.yield();
} catch (Exception e) {
logger.error("Failed to read {} from Elasticsearch due to {}", new Object[] { flowFile, e.getLocalizedMessage() }, e);
session.transfer(flowFile, REL_FAILURE);
context.yield();
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project nifi by apache.
the class FetchElasticsearch method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final String index = context.getProperty(INDEX).evaluateAttributeExpressions(flowFile).getValue();
final String docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(flowFile).getValue();
final String docType = context.getProperty(TYPE).evaluateAttributeExpressions(flowFile).getValue();
final Charset charset = Charset.forName(context.getProperty(CHARSET).evaluateAttributeExpressions(flowFile).getValue());
final ComponentLog logger = getLogger();
try {
logger.debug("Fetching {}/{}/{} from Elasticsearch", new Object[] { index, docType, docId });
final long startNanos = System.nanoTime();
GetRequestBuilder getRequestBuilder = esClient.get().prepareGet(index, docType, docId);
if (authToken != null) {
getRequestBuilder.putHeader("Authorization", authToken);
}
final GetResponse getResponse = getRequestBuilder.execute().actionGet();
if (getResponse == null || !getResponse.isExists()) {
logger.debug("Failed to read {}/{}/{} from Elasticsearch: Document not found", new Object[] { index, docType, docId });
// We couldn't find the document, so penalize it and send it to "not found"
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_NOT_FOUND);
} else {
flowFile = session.putAttribute(flowFile, "filename", docId);
flowFile = session.putAttribute(flowFile, "es.index", index);
flowFile = session.putAttribute(flowFile, "es.type", docType);
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
out.write(getResponse.getSourceAsString().getBytes(charset));
}
});
logger.debug("Elasticsearch document " + docId + " fetched, routing to success");
final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
final String uri = context.getProperty(HOSTS).evaluateAttributeExpressions().getValue() + "/" + index + "/" + docType + "/" + docId;
session.getProvenanceReporter().fetch(flowFile, uri, millis);
session.transfer(flowFile, REL_SUCCESS);
}
} catch (NoNodeAvailableException | ElasticsearchTimeoutException | ReceiveTimeoutTransportException | NodeClosedException exceptionToRetry) {
logger.error("Failed to read into Elasticsearch due to {}, this may indicate an error in configuration " + "(hosts, username/password, etc.). Routing to retry", new Object[] { exceptionToRetry.getLocalizedMessage() }, exceptionToRetry);
session.transfer(flowFile, REL_RETRY);
context.yield();
} catch (Exception e) {
logger.error("Failed to read {} from Elasticsearch due to {}", new Object[] { flowFile, e.getLocalizedMessage() }, e);
session.transfer(flowFile, REL_FAILURE);
context.yield();
}
}
Aggregations