Search in sources :

Example 1 with MappingMetadata

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

the class SQLTransportIntegrationTest method getIndexMapping.

/**
     * Get all mappings from an index as JSON String
     *
     * @param index the name of the index
     * @return the index mapping as String
     * @throws IOException
     */
protected String getIndexMapping(String index) throws IOException {
    ClusterStateRequest request = Requests.clusterStateRequest().routingTable(false).nodes(false).metaData(true).indices(index);
    ClusterStateResponse response = client().admin().cluster().state(request).actionGet();
    MetaData metaData = response.getState().metaData();
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    IndexMetaData indexMetaData = metaData.iterator().next();
    for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) {
        builder.field(cursor.value.type());
        builder.map(cursor.value.sourceAsMap());
    }
    builder.endObject();
    return builder.string();
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) MetaData(org.elasticsearch.cluster.metadata.MetaData) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 2 with MappingMetadata

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

the class PartitionedTableIntegrationTest method testAlterPartitionedTableKeepsMetadata.

@Test
public void testAlterPartitionedTableKeepsMetadata() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double" + ") partitioned by (score) with (number_of_replicas=0, column_policy='dynamic')");
    ensureGreen();
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("refresh table dynamic_table");
    ensureGreen();
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Collections.singletonList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    Map<String, Object> metaMap = (Map) partitionMetaData.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
    execute("alter table dynamic_table set (column_policy= 'dynamic')");
    waitNoPendingTasksOnAll();
    partitionMetaData = clusterService().state().metaData().indices().get(new PartitionName("dynamic_table", Collections.singletonList(new BytesRef("10.0"))).asIndexName()).getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    metaMap = (Map) partitionMetaData.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
}
Also used : PartitionName(io.crate.metadata.PartitionName) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 3 with MappingMetadata

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

the class ColumnPolicyIntegrationTest method testResetColumnPolicyPartitioned.

@Test
public void testResetColumnPolicyPartitioned() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double" + ") partitioned by (score) with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("refresh table dynamic_table");
    execute("alter table dynamic_table set (column_policy = 'strict')");
    waitNoPendingTasksOnAll();
    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(ColumnPolicy.STRICT.value()));
    execute("alter table dynamic_table reset (column_policy)");
    waitNoPendingTasksOnAll();
    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())));
}
Also used : PartitionName(io.crate.metadata.PartitionName) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 4 with MappingMetadata

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

the class ColumnPolicyIntegrationTest method testResetColumnPolicy.

@Test
public void testResetColumnPolicy() throws Exception {
    execute("create table dynamic_table (" + "  id integer, " + "  score double" + ") with (number_of_replicas=0)");
    ensureYellow();
    execute("alter table dynamic_table set (column_policy = 'strict')");
    waitNoPendingTasksOnAll();
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices().get("dynamic_table").getMappings().get(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(String.valueOf(partitionMetaData.getSourceAsMap().get("dynamic")), is(ColumnPolicy.STRICT.value()));
    execute("alter table dynamic_table reset (column_policy)");
    waitNoPendingTasksOnAll();
    partitionMetaData = clusterService().state().metaData().indices().get("dynamic_table").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) Test(org.junit.Test)

Example 5 with MappingMetadata

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

the class ColumnPolicyIntegrationTest method testStrictPartitionedTableInsert.

@Test
public void testStrictPartitionedTableInsert() 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("insert into numbers (num, odd, prime, perfect) values (?, ?, ?, ?)", new Object[] { 28, true, false, 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)

Aggregations

MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)45 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)15 Map (java.util.Map)14 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)11 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)10 Settings (org.elasticsearch.common.settings.Settings)9 Test (org.junit.Test)9 IOException (java.io.IOException)8 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)7 PartitionName (io.crate.metadata.PartitionName)6 HashMap (java.util.HashMap)6 BytesRef (org.apache.lucene.util.BytesRef)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 List (java.util.List)4 Set (java.util.Set)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)4 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)3 ArrayList (java.util.ArrayList)3 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)3