Search in sources :

Example 66 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch-river-rabbitmq by elastic.

the class RabbitMQIntegrationTest method launchTest.

private void launchTest(XContentBuilder river, final int numMessages, final int numDocsPerMessage, InjectorHook injectorHook, boolean delete, boolean update) throws Exception {
    final String dbName = getDbName();
    logger.info(" --> create index [{}]", dbName);
    try {
        client().admin().indices().prepareDelete(dbName).get();
    } catch (IndexMissingException e) {
    // No worries.
    }
    try {
        createIndex(dbName);
    } catch (IndexMissingException e) {
    // No worries.
    }
    ensureGreen(dbName);
    logger.info("  -> Checking rabbitmq running");
    // We try to connect to RabbitMQ.
    // If it's not launched, we don't fail the test but only log it
    Channel channel = null;
    Connection connection = null;
    try {
        logger.info(" --> connecting to rabbitmq");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(AMQP.PROTOCOL.PORT);
        connection = factory.newConnection();
    } catch (ConnectException ce) {
        throw new Exception("RabbitMQ service is not launched on localhost:" + AMQP.PROTOCOL.PORT + ". Can not start Integration test. " + "Launch `rabbitmq-server`.", ce);
    }
    try {
        logger.info("  -> Creating [{}] channel", dbName);
        channel = connection.createChannel();
        logger.info("  -> Creating queue [{}]", dbName);
        channel.queueDeclare(getDbName(), true, false, false, null);
        // We purge the queue in case of something is remaining there
        logger.info("  -> Purging [{}] channel", dbName);
        channel.queuePurge(getDbName());
        logger.info("  -> Put [{}] messages with [{}] documents each = [{}] docs", numMessages, numDocsPerMessage, numMessages * numDocsPerMessage);
        final Set<String> removed = new HashSet<String>();
        int nbUpdated = 0;
        for (int i = 0; i < numMessages; i++) {
            StringBuffer message = new StringBuffer();
            for (int j = 0; j < numDocsPerMessage; j++) {
                if (logger.isTraceEnabled()) {
                    logger.trace("  -> Indexing document [{}] - [{}][{}]", i + "_" + j, i, j);
                }
                message.append("{ \"index\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + i + "_" + j + "\" } }\n");
                message.append("{ \"field\" : \"" + i + "_" + j + "\",\"numeric\" : " + i * j + " }\n");
                // Sometime we update a document
                if (update && rarely()) {
                    String id = between(0, i) + "_" + between(0, j);
                    // We can only update if it has not been removed :)
                    if (!removed.contains(id)) {
                        logger.debug("  -> Updating document [{}] - [{}][{}]", id, i, j);
                        message.append("{ \"update\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + id + "\" } }\n");
                        message.append("{ \"doc\": { \"foo\" : \"bar\", \"field2\" : \"" + i + "_" + j + "\" }}\n");
                        nbUpdated++;
                    }
                }
                // Sometime we delete a document
                if (delete && rarely()) {
                    String id = between(0, i) + "_" + between(0, j);
                    if (!removed.contains(id)) {
                        logger.debug("  -> Removing document [{}] - [{}][{}]", id, i, j);
                        message.append("{ \"delete\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + id + "\" } }\n");
                        removed.add(id);
                    }
                }
            }
            channel.basicPublish("", dbName, null, message.toString().getBytes(StandardCharsets.UTF_8));
        }
        logger.info("  -> We removed [{}] docs and updated [{}] docs", removed.size(), nbUpdated);
        if (injectorHook != null) {
            logger.info("  -> Injecting extra data");
            injectorHook.inject();
        }
        logger.info(" --> create river");
        IndexResponse indexResponse = index("_river", dbName, "_meta", river);
        assertTrue(indexResponse.isCreated());
        logger.info("-->  checking that river [{}] was created", dbName);
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
                return response.isExists();
            }
        }, 5, TimeUnit.SECONDS), equalTo(true));
        // Check that docs are still processed by the river
        logger.info(" --> waiting for expected number of docs: [{}]", numDocsPerMessage * numMessages - removed.size());
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                try {
                    refresh();
                    int expected = numDocsPerMessage * numMessages - removed.size();
                    CountResponse response = client().prepareCount(dbName).get();
                    logger.debug("  -> got {} docs, expected {}", response.getCount(), expected);
                    return response.getCount() == expected;
                } catch (IndexMissingException e) {
                    return false;
                }
            }
        }, 20, TimeUnit.SECONDS), equalTo(true));
    } finally {
        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
        // Deletes the river
        GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
        if (response.isExists()) {
            client().prepareDelete(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_meta").get();
            client().prepareDelete(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
        }
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
                return response.isExists();
            }
        }, 5, TimeUnit.SECONDS), equalTo(false));
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) CountResponse(org.elasticsearch.action.count.CountResponse) IndexMissingException(org.elasticsearch.indices.IndexMissingException) GetResponse(org.elasticsearch.action.get.GetResponse) ConnectException(java.net.ConnectException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IndexResponse(org.elasticsearch.action.index.IndexResponse) ConnectException(java.net.ConnectException) HashSet(java.util.HashSet)

Example 67 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project storm-elastic-search by hmsonline.

the class ElasticSearchFunctionTest method testFunction.

@Test
public void testFunction() {
    ElasticSearchStateUpdater function = new ElasticSearchStateUpdater(createMapper());
    ElasticSearchState state = new ElasticSearchState(getClient());
    function.updateState(state, getTuples(), Mockito.mock(TridentCollector.class));
    GetResponse response = getClient().prepareGet(indexName, indexType, indexKey).execute().actionGet();
    Assert.assertEquals(indexName, response.getIndex());
    Assert.assertEquals(indexType, response.getType());
    Assert.assertEquals(indexKey, response.getId());
    Assert.assertEquals("kimchy", response.getSource().get("user"));
    Assert.assertEquals("trying out Elastic Search", response.getSource().get("message"));
}
Also used : TridentCollector(storm.trident.operation.TridentCollector) GetResponse(org.elasticsearch.action.get.GetResponse) Test(org.junit.Test) StormElasticSearchAbstractTest(com.hmsonline.storm.elasticsearch.StormElasticSearchAbstractTest)

Example 68 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project rssriver by dadoonet.

the class RssRiverAllParametersTest method waitForChange.

private void waitForChange(final String riverName, final String lastupdate_id) throws InterruptedException {
    String date = "";
    GetResponse getResponse = client().prepareGet("_river", riverName, lastupdate_id).setFields("rss." + lastupdate_id).execute().actionGet();
    if (getResponse.isExists()) {
        date = getResponse.getField("rss." + lastupdate_id).getValue().toString();
    }
    final String finalDate = date;
    assertThat("Date should have changed " + date, awaitBusy(new Predicate<Object>() {

        @Override
        public boolean apply(Object o) {
            GetResponse getResponse = client().prepareGet("_river", riverName, lastupdate_id).setFields("rss." + lastupdate_id).execute().actionGet();
            String new_date = "";
            if (getResponse.isExists() && getResponse.getField("rss." + lastupdate_id) != null) {
                new_date = getResponse.getField("rss." + lastupdate_id).getValue().toString();
            }
            return !finalDate.equals(new_date);
        }
    }, 10, TimeUnit.SECONDS), equalTo(true));
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) Predicate(org.elasticsearch.common.base.Predicate)

Example 69 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project crate by crate.

the class InsertIntoIntegrationTest method testInsertWithPrimaryKeyMultiValues.

@Test
public void testInsertWithPrimaryKeyMultiValues() throws Exception {
    this.setup.createTestTableWithPrimaryKey();
    Object[] args = new Object[] { "1", "All the doors in this spaceship have a cheerful and sunny disposition.", "2", "I always thought something was fundamentally wrong with the universe" };
    execute("insert into test (pk_col, message) values (?, ?), (?, ?)", args);
    refresh();
    GetResponse response = client().prepareGet("test", "default", "1").execute().actionGet();
    assertTrue(response.getSourceAsMap().containsKey("message"));
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) Test(org.junit.Test)

Example 70 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project jstorm by alibaba.

the class EsQueryBolt method execute.

@Override
public void execute(Tuple tuple) {
    try {
        String index = mapper.getIndex(tuple);
        String type = mapper.getType(tuple);
        String id = mapper.getId(tuple);
        GetResponse response = client.prepareGet(index, type, id).execute().actionGet();
        collector.emit(esOutputDeclarer.getValues(response.getSource()));
        collector.ack(tuple);
    } catch (Exception e) {
        collector.fail(tuple);
    }
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)84 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)22 Test (org.junit.Test)18 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 HashMap (java.util.HashMap)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)10 Script (org.elasticsearch.script.Script)9 Settings (org.elasticsearch.common.settings.Settings)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 CompiledScript (org.elasticsearch.script.CompiledScript)7 ExecutableScript (org.elasticsearch.script.ExecutableScript)7 SearchScript (org.elasticsearch.script.SearchScript)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)6 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)5 Path (java.nio.file.Path)4