Search in sources :

Example 46 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class ColumnPolicyIntegrationTest method testStrictPartitionedTableUpdate.

@Test
public void testStrictPartitionedTableUpdate() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='strict', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "numbers")).execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetadata template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.STRICT));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "numbers"), Arrays.asList("true")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.STRICT));
    assertThrowsMatches(() -> execute("update numbers set num=?, perfect=? where num=6", new Object[] { 28, true }), isSQLError(is("Column perfect unknown"), UNDEFINED_COLUMN, NOT_FOUND, 4043));
}
Also used : PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RelationName(io.crate.metadata.RelationName) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Example 47 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class ColumnPolicyIntegrationTest method testAlterColumnPolicyOnPartitionedTableWithExistingPartitions.

@Test
public void testAlterColumnPolicyOnPartitionedTableWithExistingPartitions() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double" + ") partitioned by (score) with (number_of_replicas=0, column_policy='strict')");
    ensureYellow();
    // create at least 2 partitions to test real multi partition logic (1 partition would behave similar to 1 normal table)
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("insert into dynamic_table (id, score) values (1, 20)");
    execute("refresh table dynamic_table");
    ensureYellow();
    execute("alter table dynamic_table set (column_policy = 'dynamic')");
    waitNoPendingTasksOnAll();
    // After changing the column_policy it's possible to add new columns to existing and new
    // partitions
    execute("insert into dynamic_table (id, score, comment) values (2, 10, 'this is a new column')");
    execute("insert into dynamic_table (id, score, new_comment) values (2, 5, 'this is a new column on a new partition')");
    execute("refresh table dynamic_table");
    ensureYellow();
    GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates(PartitionName.templateName(sqlExecutor.getCurrentSchema(), "dynamic_table")).execute().actionGet();
    assertThat(response.getIndexTemplates().size(), is(1));
    IndexTemplateMetadata template = response.getIndexTemplates().get(0);
    CompressedXContent mappingStr = template.mappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mappingStr, is(notNullValue()));
    Tuple<XContentType, Map<String, Object>> typeAndMap = XContentHelper.convertToMap(mappingStr.compressedReference(), false, XContentType.JSON);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(decodeMappingValue(mapping.get("dynamic")), is(ColumnPolicy.DYNAMIC));
    execute("insert into dynamic_table (id, score, new_col) values (?, ?, ?)", new Object[] { 6, 3, "hello" });
    execute("refresh table dynamic_table");
    ensureYellow();
    Map<String, Object> sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "dynamic_table"), Arrays.asList("10.0")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.DYNAMIC));
    sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "dynamic_table"), Arrays.asList("5.0")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.DYNAMIC));
    sourceMap = getSourceMap(new PartitionName(new RelationName("doc", "dynamic_table"), Arrays.asList("3.0")).asIndexName());
    assertThat(decodeMappingValue(sourceMap.get("dynamic")), is(ColumnPolicy.DYNAMIC));
}
Also used : PartitionName(io.crate.metadata.PartitionName) XContentType(org.elasticsearch.common.xcontent.XContentType) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RelationName(io.crate.metadata.RelationName) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) Test(org.junit.Test)

Example 48 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class PartitionedTableIntegrationTest method testAlterTableAddColumnOnPartitionedTable.

@Test
@UseRandomizedSchema(random = false)
public void testAlterTableAddColumnOnPartitionedTable() throws Exception {
    execute("create table t (id int primary key, date timestamp with time zone primary key) " + "partitioned by (date) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
    execute("insert into t (id, date) values (1, '2014-01-01')");
    execute("insert into t (id, date) values (10, '2015-01-01')");
    ensureYellow();
    refresh();
    execute("alter table t add name string");
    execute("select * from t");
    assertThat(Arrays.asList(response.cols()), Matchers.containsInAnyOrder("date", "id", "name"));
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().getTemplates(new GetIndexTemplatesRequest(".partitioned.t.")).actionGet();
    IndexTemplateMetadata metadata = templatesResponse.getIndexTemplates().get(0);
    String mappingSource = metadata.mappings().get(DEFAULT_MAPPING_TYPE).toString();
    Map mapping = (Map) XContentFactory.xContent(mappingSource).createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource).map().get(DEFAULT_MAPPING_TYPE);
    assertNotNull(((Map) mapping.get("properties")).get("name"));
    // template order must not be touched
    assertThat(metadata.order(), is(100));
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Example 49 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class PartitionedTableIntegrationTest method testAlterTableAddColumnOnPartitionedTableWithoutPartitions.

@Test
@UseRandomizedSchema(random = false)
public void testAlterTableAddColumnOnPartitionedTableWithoutPartitions() throws Exception {
    execute("create table t (id int primary key, date timestamp with time zone primary key) " + "partitioned by (date) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
    ensureYellow();
    execute("alter table t add column name string");
    execute("alter table t add column ft_name string index using fulltext");
    ensureYellow();
    execute("select * from t");
    assertThat(Arrays.asList(response.cols()), Matchers.containsInAnyOrder("date", "ft_name", "id", "name"));
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().getTemplates(new GetIndexTemplatesRequest(".partitioned.t.")).actionGet();
    IndexTemplateMetadata metadata = templatesResponse.getIndexTemplates().get(0);
    String mappingSource = metadata.mappings().get(DEFAULT_MAPPING_TYPE).toString();
    Map mapping = (Map) XContentFactory.xContent(mappingSource).createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource).map().get(DEFAULT_MAPPING_TYPE);
    assertNotNull(((Map) mapping.get("properties")).get("name"));
    assertNotNull(((Map) mapping.get("properties")).get("ft_name"));
}
Also used : GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) GetIndexTemplatesRequest(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Example 50 with IndexTemplateMetadata

use of org.elasticsearch.cluster.metadata.IndexTemplateMetadata in project crate by crate.

the class IndexTemplateUpgraderTest method testInvalidSettingIsRemovedForTemplateInCustomSchema.

@Test
public void testInvalidSettingIsRemovedForTemplateInCustomSchema() {
    Settings settings = Settings.builder().put("index.recovery.initial_shards", "quorum").build();
    String templateName = PartitionName.templateName("foobar", "t1");
    IndexTemplateMetadata template = IndexTemplateMetadata.builder(templateName).settings(settings).patterns(Collections.singletonList("*")).build();
    IndexTemplateUpgrader indexTemplateUpgrader = new IndexTemplateUpgrader();
    Map<String, IndexTemplateMetadata> result = indexTemplateUpgrader.apply(Collections.singletonMap(templateName, template));
    assertThat("Outdated setting `index.recovery.initial_shards` must be removed", result.get(templateName).settings().hasValue("index.recovery.initial_shards"), is(false));
}
Also used : IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Aggregations

IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)36 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)18 Test (org.junit.Test)16 Settings (org.elasticsearch.common.settings.Settings)14 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)13 Map (java.util.Map)12 Metadata (org.elasticsearch.cluster.metadata.Metadata)11 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)11 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)10 ClusterState (org.elasticsearch.cluster.ClusterState)10 PartitionName (io.crate.metadata.PartitionName)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Index (org.elasticsearch.index.Index)9 RelationName (io.crate.metadata.RelationName)8 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)7 AliasMetadata (org.elasticsearch.cluster.metadata.AliasMetadata)6 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)6 IOException (java.io.IOException)5 Set (java.util.Set)5