Search in sources :

Example 16 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project crate by crate.

the class TableAliasIntegrationTest method testPartitionedTableKeepsAliasAfterSchemaUpdate.

@Test
public void testPartitionedTableKeepsAliasAfterSchemaUpdate() throws Exception {
    execute("create table t (name string, p string) partitioned by (p) " + "clustered into 2 shards with (number_of_replicas = 0)");
    ensureYellow();
    execute("insert into t (name, p) values ('Arthur', 'a')");
    execute("insert into t (name, p) values ('Trillian', 'a')");
    execute("alter table t add column age integer");
    execute("insert into t (name, p) values ('Marvin', 'b')");
    waitNoPendingTasksOnAll();
    refresh();
    execute("select count(*) from t");
    assertThat(response.rowCount(), is(1L));
    assertThat((Long) response.rows()[0][0], is(3L));
    GetIndexTemplatesResponse indexTemplatesResponse = client().admin().indices().prepareGetTemplates(".partitioned.t.").execute().actionGet();
    IndexTemplateMetaData indexTemplateMetaData = indexTemplatesResponse.getIndexTemplates().get(0);
    AliasMetaData t = indexTemplateMetaData.aliases().get("t");
    assertThat(t.alias(), is("t"));
    execute("select partitioned_by from information_schema.tables where table_name = 't'");
    assertThat((String) ((Object[]) response.rows()[0][0])[0], is("p"));
}
Also used : AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) Test(org.junit.Test)

Example 17 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse 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(null, "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(String.valueOf(mapping.get("dynamic")), is(ColumnPolicy.STRICT.value()));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("numbers", Arrays.asList(new BytesRef("true"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(ColumnPolicy.STRICT.value()));
    expectedException.expect(SQLActionException.class);
    expectedException.expectMessage("Column perfect unknown");
    execute("update numbers set num=?, perfect=? where num=6", new Object[] { 28, true });
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) 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) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 18 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project crate by crate.

the class ColumnPolicyIntegrationTest method testDynamicPartitionedTable.

@Test
public void testDynamicPartitionedTable() throws Exception {
    execute("create table numbers (" + "  num int, " + "  odd boolean," + "  prime boolean" + ") partitioned by (odd) with (column_policy='dynamic', number_of_replicas=0)");
    ensureYellow();
    GetIndexTemplatesResponse templateResponse = client().admin().indices().prepareGetTemplates(PartitionName.templateName(null, "numbers")).execute().actionGet();
    assertThat(templateResponse.getIndexTemplates().size(), is(1));
    IndexTemplateMetaData template = templateResponse.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(String.valueOf(mapping.get("dynamic")), is("true"));
    execute("insert into numbers (num, odd, prime) values (?, ?, ?)", new Object[] { 6, true, false });
    execute("refresh table numbers");
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("numbers", Collections.singletonList(new BytesRef("true"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is("true"));
    execute("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)", new Object[] { 28, true, false, true });
    ensureYellow();
    execute("refresh table numbers");
    waitForMappingUpdateOnAll("numbers", "perfect");
    execute("select * from numbers order by num");
    assertThat(response.rowCount(), is(2L));
    assertThat(response.cols(), arrayContaining("num", "odd", "perfect", "prime"));
    assertThat(TestingHelpers.printedTable(response.rows()), is("6| true| NULL| false\n" + "28| true| true| false\n"));
    execute("update numbers set prime=true, changed='2014-10-23T10:20', author='troll' where num=28");
    assertThat(response.rowCount(), is(1L));
    waitForMappingUpdateOnAll("numbers", "changed");
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) 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) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 19 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse 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(null, "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);
    @SuppressWarnings("unchecked") Map<String, Object> mapping = (Map<String, Object>) typeAndMap.v2().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(mapping.get("dynamic")), is(String.valueOf(ColumnPolicy.DYNAMIC.mappingValue())));
    execute("insert into dynamic_table (id, score, new_col) values (?, ?, ?)", new Object[] { 6, 3, "hello" });
    execute("refresh table dynamic_table");
    ensureYellow();
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(String.valueOf(ColumnPolicy.DYNAMIC.mappingValue())));
    partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("5.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(String.valueOf(ColumnPolicy.DYNAMIC.mappingValue())));
    partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Arrays.asList(new BytesRef("3.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(String.valueOf(ColumnPolicy.DYNAMIC.mappingValue())));
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) 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) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 20 with GetIndexTemplatesResponse

use of org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse in project crate by crate.

the class PartitionedTableIntegrationTest method testAlterNumberOfShards.

@Test
public void testAlterNumberOfShards() throws Exception {
    execute("create table quotes (" + "  id integer, " + "  quote string, " + "  date timestamp) " + "partitioned by(date) clustered into 3 shards with (number_of_replicas='0-all')");
    ensureYellow();
    String templateName = PartitionName.templateName(null, "quotes");
    GetIndexTemplatesResponse templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    Settings templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(3));
    execute("alter table quotes set (number_of_shards=6)");
    ensureGreen();
    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(6));
    execute("insert into quotes (id, quote, date) values (?, ?, ?)", new Object[] { 1, "Don't panic", 1395874800000L });
    assertThat(response.rowCount(), is(1L));
    refresh();
    assertTrue(clusterService().state().metaData().hasAlias("quotes"));
    execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
    assertEquals("0-all", response.rows()[0][0]);
    assertEquals(6, response.rows()[0][1]);
    execute("select number_of_shards from information_schema.table_partitions where table_name='quotes'");
    assertThat(response.rowCount(), is(1L));
    assertThat((Integer) response.rows()[0][0], is(6));
    execute("alter table quotes set (number_of_shards=2)");
    ensureYellow();
    execute("insert into quotes (id, quote, date) values (?, ?, ?)", new Object[] { 2, "Now panic", 1395961200000L });
    assertThat(response.rowCount(), is(1L));
    ensureYellow();
    refresh();
    execute("select number_of_replicas, number_of_shards from information_schema.tables where table_name = 'quotes'");
    assertEquals("0-all", response.rows()[0][0]);
    assertEquals(2, response.rows()[0][1]);
    execute("select number_of_shards from information_schema.table_partitions where table_name='quotes' order by number_of_shards ASC");
    assertThat(response.rowCount(), is(2L));
    assertThat((Integer) response.rows()[0][0], is(2));
    assertThat((Integer) response.rows()[1][0], is(6));
    templatesResponse = client().admin().indices().prepareGetTemplates(templateName).execute().actionGet();
    templateSettings = templatesResponse.getIndexTemplates().get(0).getSettings();
    assertThat(templateSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 0), is(2));
    List<String> partitions = ImmutableList.of(new PartitionName("quotes", Collections.singletonList(new BytesRef("1395874800000"))).asIndexName(), new PartitionName("quotes", Collections.singletonList(new BytesRef("1395961200000"))).asIndexName());
    Thread.sleep(1000);
    GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings(partitions.get(0), partitions.get(1)).execute().get();
    String index = partitions.get(0);
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_SHARDS), is("6"));
    index = partitions.get(1);
    assertThat(settingsResponse.getSetting(index, IndexMetaData.SETTING_NUMBER_OF_SHARDS), is("2"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) Settings(org.elasticsearch.common.settings.Settings) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Aggregations

GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)35 Test (org.junit.Test)18 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)11 PartitionName (io.crate.metadata.PartitionName)10 BytesRef (org.apache.lucene.util.BytesRef)10 GetSettingsResponse (org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 Map (java.util.Map)5 GetIndexTemplatesRequest (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest)5 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)5 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)5 HashMap (java.util.HashMap)4 Alias (org.elasticsearch.action.admin.indices.alias.Alias)4 XContentType (org.elasticsearch.common.xcontent.XContentType)4 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)3 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)3 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)3 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)2 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)2 DeleteIndexTemplateRequest (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2