Search in sources :

Example 1 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project storm-elastic-search by hmsonline.

the class ElasticSearchState method createIndices.

public void createIndices(TridentElasticSearchMapper mapper, List<TridentTuple> tuples) {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    Set<String> existingIndex = new HashSet<String>();
    for (TridentTuple tuple : tuples) {
        String indexName = mapper.mapToIndex(tuple);
        String type = mapper.mapToType(tuple);
        String key = mapper.mapToKey(tuple);
        Map<String, Object> data = mapper.mapToData(tuple);
        String parentId = mapper.mapToParentId(tuple);
        if (!existingIndex.contains(indexName) && !client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet().isExists()) {
            createIndex(bulkRequest, indexName, mapper.mapToIndexSettings(tuple));
            createMapping(bulkRequest, indexName, type, mapper.mapToMappingSettings(tuple));
            existingIndex.add(indexName);
        }
        if (StringUtils.isBlank(parentId)) {
            bulkRequest.add(client.prepareIndex(indexName, type, key).setSource(data));
        } else {
            LOGGER.debug("parent: " + parentId);
            bulkRequest.add(client.prepareIndex(indexName, type, key).setSource(data).setParent(parentId));
        }
    }
    try {
        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            // Index failed. Retry!
            throw new FailedException("Cannot create index via ES: " + bulkResponse.buildFailureMessage());
        }
    } catch (ElasticSearchException e) {
        StormElasticSearchUtils.handleElasticSearchException(getClass(), e);
    }
}
Also used : FailedException(backtype.storm.topology.FailedException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) ElasticSearchException(org.elasticsearch.ElasticSearchException) HashSet(java.util.HashSet) TridentTuple(storm.trident.tuple.TridentTuple)

Example 2 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project crate by crate.

the class TransportExecutorDDLTest method testCreateTableWithOrphanedAlias.

@Test
public void testCreateTableWithOrphanedAlias() throws Exception {
    String partitionName = new PartitionName("test", Collections.singletonList(new BytesRef("foo"))).asIndexName();
    client().admin().indices().prepareCreate(partitionName).addMapping(Constants.DEFAULT_MAPPING_TYPE, TEST_PARTITIONED_MAPPING).setSettings(TEST_SETTINGS).addAlias(new Alias("test")).execute().actionGet();
    ensureGreen();
    execute("create table test (id integer, name string, names string) " + "clustered into 2 shards " + "partitioned by (id) with (number_of_replicas=0)");
    assertThat(response.rowCount(), is(1L));
    ensureGreen();
    execute("select * from information_schema.tables where table_name = 'test'");
    assertThat(response.rowCount(), is(1L));
    execute("select count(*) from information_schema.columns where table_name = 'test'");
    assertThat((Long) response.rows()[0][0], is(3L));
    // check that orphaned alias has been deleted
    assertThat(client().admin().cluster().prepareState().execute().actionGet().getState().metaData().hasAlias("test"), is(false));
    // check that orphaned partition has been deleted
    assertThat(client().admin().indices().exists(new IndicesExistsRequest(partitionName)).actionGet().isExists(), is(false));
}
Also used : PartitionName(io.crate.metadata.PartitionName) Alias(org.elasticsearch.action.admin.indices.alias.Alias) BytesRef(org.apache.lucene.util.BytesRef) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 3 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project crate by crate.

the class TransportExecutorDDLTest method testCreateTableWithOrphanedPartitions.

@Test
public void testCreateTableWithOrphanedPartitions() throws Exception {
    String partitionName = new PartitionName("test", Collections.singletonList(new BytesRef("foo"))).asIndexName();
    client().admin().indices().prepareCreate(partitionName).addMapping(Constants.DEFAULT_MAPPING_TYPE, TEST_PARTITIONED_MAPPING).setSettings(TEST_SETTINGS).execute().actionGet();
    ensureGreen();
    execute("create table test (id integer, name string, names string) partitioned by (id)");
    ensureYellow();
    execute("select * from information_schema.tables where table_name = 'test'");
    assertThat(response.rowCount(), is(1L));
    execute("select count(*) from information_schema.columns where table_name = 'test'");
    assertThat((Long) response.rows()[0][0], is(3L));
    // check that orphaned partition has been deleted
    assertThat(client().admin().indices().exists(new IndicesExistsRequest(partitionName)).actionGet().isExists(), is(false));
}
Also used : PartitionName(io.crate.metadata.PartitionName) BytesRef(org.apache.lucene.util.BytesRef) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest) Test(org.junit.Test)

Example 4 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project crate by crate.

the class DDLIntegrationTest method testCreateTableWithRefreshIntervalDisableRefresh.

@Test
public void testCreateTableWithRefreshIntervalDisableRefresh() throws Exception {
    execute("create table test (id int primary key, content string) " + "clustered into 5 shards " + "with (refresh_interval=0, number_of_replicas = 0)");
    ensureYellow();
    assertTrue(client().admin().indices().exists(new IndicesExistsRequest("test")).actionGet().isExists());
    String expectedSettings = "{\"test\":{" + "\"settings\":{" + "\"index.number_of_replicas\":\"0\"," + "\"index.number_of_shards\":\"5\"," + "\"index.refresh_interval\":\"0ms\"," + "\"index.version.created\":\"" + Version.CURRENT.esVersion.id + "\"" + "}}}";
    JSONAssert.assertEquals(expectedSettings, getIndexSettings("test"), false);
    execute("ALTER TABLE test SET (refresh_interval = 5000)");
    String expectedSetSettings = "{\"test\":{" + "\"settings\":{" + "\"index.number_of_replicas\":\"0\"," + "\"index.number_of_shards\":\"5\"," + "\"index.refresh_interval\":\"5000ms\"," + "\"index.version.created\":\"" + Version.CURRENT.esVersion.id + "\"" + "}}}";
    JSONAssert.assertEquals(expectedSetSettings, getIndexSettings("test"), false);
    execute("ALTER TABLE test RESET (refresh_interval)");
    String expectedResetSettings = "{\"test\":{" + "\"settings\":{" + "\"index.number_of_replicas\":\"0\"," + "\"index.number_of_shards\":\"5\"," + "\"index.refresh_interval\":\"1000ms\"," + "\"index.version.created\":\"" + Version.CURRENT.esVersion.id + "\"" + "}}}";
    JSONAssert.assertEquals(expectedResetSettings, getIndexSettings("test"), false);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) Test(org.junit.Test)

Example 5 with IndicesExistsRequest

use of org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest in project crate by crate.

the class DDLIntegrationTest method testCreateTable.

@Test
public void testCreateTable() throws Exception {
    execute("create table test (col1 integer primary key, col2 string) " + "clustered into 5 shards with (number_of_replicas = 1)");
    ensureYellow();
    assertTrue(client().admin().indices().exists(new IndicesExistsRequest("test")).actionGet().isExists());
    String expectedMapping = "{\"default\":{" + "\"dynamic\":\"true\",\"_meta\":{\"routing_hash_function\":\"org.elasticsearch.cluster.routing.Murmur3HashFunction\",\"primary_keys\":[\"col1\"]," + "\"version\":{\"created\":{\"elasticsearch\":" + Version.CURRENT.esVersion.id + ",\"cratedb\":" + Version.CURRENT.id + "}}}," + "\"_all\":{\"enabled\":false}," + "\"dynamic_templates\":[{\"strings\":{\"mapping\":{\"index\":\"not_analyzed\",\"store\":false,\"type\":\"string\",\"doc_values\":true},\"match_mapping_type\":\"string\"}}]," + "\"properties\":{" + "\"col1\":{\"type\":\"integer\"},\"col2\":{\"type\":\"string\",\"index\":\"not_analyzed\"}}}}";
    String expectedSettings = "{\"test\":{" + "\"settings\":{" + "\"index.number_of_replicas\":\"1\"," + "\"index.number_of_shards\":\"5\"," + "\"index.version.created\":\"" + Version.CURRENT.esVersion.id + "\"" + "}}}";
    assertEquals(expectedMapping, getIndexMapping("test"));
    JSONAssert.assertEquals(expectedSettings, getIndexSettings("test"), false);
    // test index usage
    execute("insert into test (col1, col2) values (1, 'foo')");
    assertEquals(1, response.rowCount());
    refresh();
    execute("SELECT * FROM test");
    assertEquals(1L, response.rowCount());
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) Test(org.junit.Test)

Aggregations

IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)32 Test (org.junit.Test)13 IOException (java.io.IOException)10 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)10 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)10 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)10 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)10 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)2 PartitionName (io.crate.metadata.PartitionName)2 BytesRef (org.apache.lucene.util.BytesRef)2 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)2 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)2 FailedException (backtype.storm.topology.FailedException)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1