org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example #2
Source File: IngestIndexCreationTest.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@Test
public void testIngestCreation() throws Exception {
    Settings settingsForIndex = Settings.settingsBuilder()
            .put("index.number_of_shards", 1)
            .build();
    Map<String,String> mappings = new HashMap<>();
    mappings.put("typename","{\"properties\":{\"message\":{\"type\":\"string\"}}}");
    final IngestTransportClient ingest = ClientBuilder.builder()
            .put(getSettings())
            .setMetric(new LongAdderIngestMetric())
            .toIngestTransportClient();
    try {
        ingest.newIndex("test", settingsForIndex, mappings);
        GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices("test");
        GetMappingsResponse getMappingsResponse =
                ingest.client().execute(GetMappingsAction.INSTANCE, getMappingsRequest).actionGet();
        MappingMetaData md = getMappingsResponse.getMappings().get("test").get("typename");
        assertEquals("{properties={message={type=string}}}", md.getSourceAsMap().toString());
    } finally {
        ingest.shutdown();
    }
}
 
Example #3
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example #4
Source File: ElasticsearchUtil.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Generated
public Set<String> removeFieldsHavingExistingMapping(Set<String> fields, String indexName, String docType) {
    GetMappingsRequest request = new GetMappingsRequest();
    request.indices(indexName);

    try {
        GetMappingsResponse mappingsResponse = legacyElasticSearchClient.indices().getMapping(request, RequestOptions.DEFAULT);
        Map<String, String> mapProperties = ((Map<String, String>) mappingsResponse.getMappings()
                .get(indexName)
                .get(docType)
                .sourceAsMap().get("properties"));

        Set<String> mappedFields = new HashSet<>(mapProperties.keySet());
        fields.removeAll(mappedFields);
        return fields;
    } catch (IOException e) {
        log.error("Error finding mappings", e);
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: Elasticsearch5SearchIndex.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) {
    indexRefreshTracker.refresh(client, indexName);
    GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get();
    for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) {
        ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value);
        for (ObjectCursor<String> typeName : typeMappings.keys()) {
            MappingMetaData typeMapping = typeMappings.get(typeName.value);
            Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping);
            if (properties == null) {
                continue;
            }

            for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) {
                String rawPropertyName = propertyEntry.getKey().replace(FIELDNAME_DOT_REPLACEMENT, ".");
                loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName);
            }
        }
    }
}
 
Example #6
Source File: Elasticsearch7SearchIndex.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) {
    indexRefreshTracker.refresh(client, indexName);
    GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get();
    for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) {
        ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value);
        for (ObjectCursor<String> typeName : typeMappings.keys()) {
            MappingMetaData typeMapping = typeMappings.get(typeName.value);
            Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping);
            if (properties == null) {
                continue;
            }

            for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) {
                String rawPropertyName = propertyEntry.getKey().replace(FIELDNAME_DOT_REPLACEMENT, ".");
                loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName);
            }
        }
    }
}
 
Example #7
Source File: TestElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private boolean doesMappingExist(final String index, final String mappingName) {
	GetMappingsRequest request = new GetMappingsRequest()
			.indices(index);
	try {
		GetMappingsResponse response = elasticSearchClient.admin()
				.indices()
				.getMappings(request)
				.get();

		return response.getMappings()
				.get(index)
				.containsKey(mappingName);
	} catch (InterruptedException | ExecutionException e) {
		throw new RuntimeException(e);
	}
}
 
Example #8
Source File: TestElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
private boolean doesMappingExist(final String index, final String mappingName) {
    GetMappingsRequest request = new GetMappingsRequest()
            .indices(index);
    try {
        GetMappingsResponse response = elasticSearchClient.admin()
                .indices()
                .getMappings(request)
                .get();

        return response.getMappings()
                .get(index)
                .containsKey(mappingName);
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
}
 
Example #9
Source File: RestHighLevelClientExt.java    From canal with Apache License 2.0 5 votes vote down vote up
public static GetMappingsResponse getMapping(RestHighLevelClient restHighLevelClient,
                                             GetMappingsRequest getMappingsRequest,
                                             RequestOptions options) throws IOException {
    return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
        RequestConvertersExt::getMappings,
        options,
        GetMappingsResponse::fromXContent,
        emptySet());

}
 
Example #10
Source File: ElasticSearchEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getDynamicFieldNames() throws SearchEngineException {
	List<String> fieldNames = new LinkedList<>();

	try {
		GetMappingsRequest req =
				new GetMappingsRequestBuilder(client, GetMappingsAction.INSTANCE, configuration.getIndexName())
						.setTypes(configuration.getDocType())
						.request();
		GetMappingsResponse response = client.admin().indices().getMappings(req).actionGet();
		MappingMetaData metaData = response.getMappings()
				.get(configuration.getIndexName())
				.get(configuration.getDocType());
		Map<String, Object> sourceMap = metaData.getSourceAsMap();
		Object annotationField = ((Map)sourceMap.get("properties")).get(configuration.getAnnotationField());
		Map<String, Object> annotationProperties = (Map<String, Object>)((Map)annotationField).get("properties");

		if (annotationProperties != null) {
			for (String field : annotationProperties.keySet()) {
				if (field.matches(DYNAMIC_LABEL_FIELD_REGEX)) {
					fieldNames.add(field);
				}
			}
		}
	} catch (IOException e) {
		LOGGER.error("Caught IOException retrieving field source: {}", e.getMessage());
		throw new SearchEngineException(e);
	}

	return fieldNames;
}
 
Example #11
Source File: ESIndex.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public Set<String> listAllFields() throws Exception{
    GetMappingsResponse response = client.admin().indices().prepareGetMappings(this.indexName).
            execute().actionGet();
    MappingMetaData mappingMetaData = response.getMappings().get(this.indexName).get(this.documentType);
    Map map = (Map)mappingMetaData.getSourceAsMap().get("properties");
    Set<String> fields = new HashSet<>();
    for (Object field: map.keySet()){
        fields.add(field.toString());
    }
    return fields;
}
 
Example #12
Source File: AbstractWriter.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
public void open() {
    final IndicesExistsResponse existsResponse = client.admin().indices()
            .prepareExists(index).execute().actionGet();
    if (!existsResponse.isExists()) {
        final CreateIndexResponse createIndexResponse = client.admin()
                .indices().prepareCreate(index).execute().actionGet();
        if (!createIndexResponse.isAcknowledged()) {
            throw new TasteException("Failed to create " + index
                    + " index.");
        }
    }

    if (mappingBuilder != null) {
        final GetMappingsResponse response = client.admin().indices()
                .prepareGetMappings(index).setTypes(type).execute()
                .actionGet();
        if (response.mappings().isEmpty()) {
            final PutMappingResponse putMappingResponse = client.admin()
                    .indices().preparePutMapping(index).setType(type)
                    .setSource(mappingBuilder).execute().actionGet();
            if (!putMappingResponse.isAcknowledged()) {
                throw new TasteException("Failed to create a mapping of"
                        + index + "/" + type);
            }
        }
    }
}
 
Example #13
Source File: ElasticSearch.java    From javabase with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    Map<String, String> map = new HashMap();
    //基础名称
    map.put("cluster.name", "my-application-A");
    Settings.Builder settings = Settings.builder().put(map);
    try {
        transportClient = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        IndicesAdminClient indicesAdminClient = transportClient.admin().indices();
        //查看索引是否存在,不存在就创建索引
        if(!checkExistsIndex(indicesAdminClient,INDEX_NAME)){
            indicesAdminClient.prepareCreate(INDEX_NAME).setSettings().execute().actionGet();
        }
        //查询mapping是否存在,已存在就不创建了
        GetMappingsResponse getMappingsResponse = indicesAdminClient.getMappings(new GetMappingsRequest().indices(INDEX_NAME)).actionGet();
        ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexToMappings = getMappingsResponse.getMappings();
       if(indexToMappings.get(INDEX_NAME).get(TIEABA_CONTENT_TYPE)==null) {
           //创建zk分词mapping
           PutMappingRequest mapping = Requests.putMappingRequest(INDEX_NAME).type(TIEABA_CONTENT_TYPE).source(createIKMapping(TIEABA_CONTENT_TYPE, TIEABA_CONTENT_FIELD).string());
           mapping.updateAllTypes(true);
           indicesAdminClient.putMapping(mapping).actionGet();
       }
    } catch (Exception e) {
       log.error("初始化 elasticsearch cliet error"+e.getLocalizedMessage());
    }
}
 
Example #14
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 5 votes vote down vote up
private void addTypeMapping(String indexName, String type, String sourcePath) {
    GetMappingsResponse getMappingsResponse = elasticSearchClient.admin().indices().prepareGetMappings(indexName).addTypes(type).execute().actionGet();
    if (getMappingsResponse.mappings().isEmpty()) {
        LOGGER.info("Adding the {} type mappings", indexName);
        try {
            String source = loadTypeMappingSource(sourcePath);
            elasticSearchClient.admin().indices().preparePutMapping(indexName).setType(type).setSource(source, XContentType.JSON).execute().actionGet();
        } catch (Exception e) {
            LOGGER.error("Failed to init index " + indexName + " mappings", e);
        }
    }
}
 
Example #15
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
private void addMappingToIndex(String indexName, String mappingType, String mappingFilename)
    throws IOException {
    GetMappingsResponse getMappingsResponse = elasticSearchClient.admin()
        .indices()
        .prepareGetMappings(indexName)
        .addTypes(mappingType)
        .execute()
        .actionGet();

    if (getMappingsResponse.mappings().isEmpty()) {
        logger.info("Adding the mappings for type: {}", mappingType);
        InputStream stream = ElasticSearchDAOV5.class.getResourceAsStream(mappingFilename);
        byte[] bytes = IOUtils.toByteArray(stream);
        String source = new String(bytes);
        try {
            elasticSearchClient.admin()
                .indices()
                .preparePutMapping(indexName)
                .setType(mappingType)
                .setSource(source, XContentFactory.xContentType(source))
                .execute()
                .actionGet();
        } catch (Exception e) {
            logger.error("Failed to init index mappings for type: {}", mappingType, e);
        }
    }
}
 
Example #16
Source File: ModelsCatalog.java    From swagger-for-elasticsearch with Apache License 2.0 4 votes vote down vote up
protected IndexModelsMap getIndexTypeModelsMap() {
    return cache.getOrResolve("getIndexTypeModelsMap",
        new Callable<IndexModelsMap>() {
            @Override
            public IndexModelsMap call() throws Exception {
                IndexModelsMap result = new IndexModelsMap();

                GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings().get();

                ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexTypeMappings = getMappingsResponse.getMappings();
                for (ObjectCursor<String> indexCursor : indexTypeMappings.keys()) {
                    String indexName = indexCursor.value;
                    List<Model> typeModels = new ArrayList<>();
                    result.put(indexName, typeModels);

                    ImmutableOpenMap<String, MappingMetaData> typeMappings = indexTypeMappings.get(indexName);
                    for (ObjectCursor<String> typeCursor : typeMappings.keys()) {
                        String typeName = typeCursor.value;

                        Map mappingProperties;
                        try {
                            mappingProperties = (Map) typeMappings.get(typeCursor.value).getSourceAsMap().get("properties");
                        } catch (IOException e) {
                            mappingProperties = Collections.emptyMap();
                            e.printStackTrace();
                        }

                        List<Property> properties = new ArrayList<>();
                        for (Object propertyNameObj : mappingProperties.keySet()) {
                            String propertyName = propertyNameObj.toString();
                            if (mappingProperties.containsKey(propertyName)) {
                                Map fieldMapping = (Map) mappingProperties.get(propertyName);
                                String mappingType = fieldMapping.containsKey("type") ? fieldMapping.get("type").toString() : "string";
                                properties.add(
                                    Property.builder()
                                        .name(propertyName)
                                        .model(mappingType != null ? mappingTypeToModel(mappingType) : null).build()
                                );
                            }
                        }

                        typeModels.add(
                            Model.builder()
                                .id(getTypeModelId(indexName, typeName))
                                .name(typeName)
                                .properties(properties)
                                .build()
                        );
                    }
                }
                return result;
            }
        }
    );
}
 
Example #17
Source File: Elasticsearch5SearchIndex.java    From vertexium with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void updateMetadata(Graph graph, IndexInfo indexInfo) {
    try {
        indexRefreshTracker.refresh(client, indexInfo.getIndexName());
        XContentBuilder mapping = XContentFactory.jsonBuilder()
            .startObject()
            .startObject(getIdStrategy().getType());
        GetMappingsResponse existingMapping = getClient()
            .admin()
            .indices()
            .prepareGetMappings(indexInfo.getIndexName())
            .execute()
            .actionGet();

        Map<String, Object> existingElementData = existingMapping.mappings()
            .get(indexInfo.getIndexName())
            .get(getIdStrategy().getType())
            .getSourceAsMap();

        mapping = mapping.startObject("_meta")
            .startObject("vertexium");
        Map<String, Object> properties = (Map<String, Object>) existingElementData.get("properties");
        for (String propertyName : properties.keySet()) {
            ElasticsearchPropertyNameInfo p = ElasticsearchPropertyNameInfo.parse(graph, propertyNameVisibilitiesStore, propertyName);
            if (p == null || p.getPropertyVisibility() == null) {
                continue;
            }
            mapping.field(replaceFieldnameDots(propertyName), p.getPropertyVisibility());
        }
        mapping.endObject()
            .endObject()
            .endObject()
            .endObject();
        getClient()
            .admin()
            .indices()
            .preparePutMapping(indexInfo.getIndexName())
            .setType(getIdStrategy().getType())
            .setSource(mapping)
            .execute()
            .actionGet();
    } catch (IOException ex) {
        throw new VertexiumException("Could not update mapping", ex);
    }
}
 
Example #18
Source File: RestGetMappingAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
    GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
    getMappingsRequest.indices(indices).types(types);
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
            if (mappingsByIndex.isEmpty()) {
                if (indices.length != 0 && types.length != 0) {
                    return new BytesRestResponse(OK, builder.endObject());
                } else if (indices.length != 0) {
                    return new BytesRestResponse(channel, new IndexNotFoundException(indices[0]));
                } else if (types.length != 0) {
                    return new BytesRestResponse(channel, new TypeMissingException(new Index("_all"), types[0]));
                } else {
                    return new BytesRestResponse(OK, builder.endObject());
                }
            }

            for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
                if (indexEntry.value.isEmpty()) {
                    continue;
                }
                builder.startObject(indexEntry.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(Fields.MAPPINGS);
                for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
                    builder.field(typeEntry.key);
                    Map<String, Object> mapping = typeEntry.value.sourceAsMap();
                    if (mapping.containsKey("_meta")) {
                        ((Map<String, Object>)mapping.get("_meta")).put(MappingMetaData.MAPPING_VERSION, typeEntry.value.mappingVersion());
                    } else {
                        HashMap<String, Object> mappingMeta = new HashMap<>();
                        mappingMeta.put(MappingMetaData.MAPPING_VERSION, typeEntry.value.mappingVersion());
                        mapping.put("_meta", mappingMeta);
                    }
                    builder.map(mapping);
                }
                builder.endObject();
                builder.endObject();
            }

            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #19
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
private TableFieldMapping getTableFieldMapping(String table) {
    ElasticsearchMappingParser mappingParser = new ElasticsearchMappingParser(mapper);
    final String indices = ElasticsearchUtils.getIndices(table);
    logger.info("Selected indices: {}", indices);
    GetMappingsResponse mappingsResponse = elasticsearchConnection.getClient()
            .admin()
            .indices()
            .prepareGetMappings(indices)
            .execute()
            .actionGet();
    Set<String> indicesName = Sets.newHashSet();
    for(ObjectCursor<String> index : mappingsResponse.getMappings()
            .keys()) {
        indicesName.add(index.value);
    }
    List<FieldMetadata> fieldMetadata = indicesName.stream()
            .filter(x -> !CollectionUtils.isNullOrEmpty(x))
            .sorted((lhs, rhs) -> {
                Date lhsDate = ElasticsearchUtils.parseIndexDate(lhs, table)
                        .toDate();
                Date rhsDate = ElasticsearchUtils.parseIndexDate(rhs, table)
                        .toDate();
                return 0 - lhsDate.compareTo(rhsDate);
            })
            .map(index -> mappingsResponse.mappings()
                    .get(index)
                    .get(ElasticsearchUtils.DOCUMENT_TYPE_NAME))
            .flatMap(mappingData -> {
                try {
                    return mappingParser.getFieldMappings(mappingData)
                            .stream();
                } catch (Exception e) {
                    logger.error("Could not read mapping from " + mappingData, e);
                    return Stream.empty();
                }
            })
            .collect(Collectors.toList());
    final TreeSet<FieldMetadata> fieldMetadataTreeSet = new TreeSet<>(new FieldMetadataComparator());
    fieldMetadataTreeSet.addAll(fieldMetadata);
    return new TableFieldMapping(table, fieldMetadataTreeSet);
}
 
Example #20
Source File: ElasticsearchTable.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public RelDataType[] getRowTypeFromEs(RelDataTypeFactory typeFactory) throws IOException {
    RelDataType[] relDataTypes = new RelDataType[2];
    RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
    RelDataTypeFactory.FieldInfoBuilder builder1 = typeFactory.builder();
    GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings(esIndexName).get();
    MappingMetaData typeMapping = getMappingsResponse.getMappings().get(esIndexName).get(esTypeName);
    //{"person":{"properties":{"age":{"type":"integer"},"name":{"type":"text","fields":{"raw":{"type":"keyword"}}}}}}
    String json = typeMapping.source().string();
    Map<String, Object> map = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>() {
    });
    Map<String, Object> properties = ((Map<String, Map<String, Object>>) map.get(esTypeName)).get("properties");
    int index = 0;
    int index1 = 0;
    //(base-field-name, field-map)
    Stack<Pair<String, Map<String, Object>>> mapStack = new Stack<>();
    mapStack.push(Pair.of(null, properties));
    while (!mapStack.isEmpty()) {
        Pair<String, Map<String, Object>> pair = mapStack.pop();
        String baseFieldName = pair.left;
        for (Map.Entry<String, Object> entry : pair.right.entrySet()) {
            String name = entry.getKey().toUpperCase();
            if (baseFieldName != null) name = baseFieldName + "." + name;

            Map<String, Object> fieldMap = (Map<String, Object>) entry.getValue();
            String type = fieldMap.get("type") != null ? fieldMap.get("type").toString() : null;
            if (type == null)
                throw new IllegalStateException(String.format("type of elasticsearch field '%s' is null", name));
            builder.add(new RelDataTypeFieldImpl(name, index++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));

            if(!name.endsWith(".KEYWORD")){
                builder1.add(new RelDataTypeFieldImpl(name, index1++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));
            }
            //multi-field, that means containing 'fields' attribute
            Map<String, Object> moreFields = fieldMap.get("fields") != null ? (Map<String, Object>) fieldMap.get("fields") : null;
            if (moreFields != null) mapStack.push(Pair.of(name, moreFields));
        }
    }
    relDataTypes[0] = builder.build();
    relDataTypes[1] = builder1.build();
    return relDataTypes;
}
 
Example #21
Source File: DkesService.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public JSONObject getIndexAndTypeName(String es_url, String esCluster_name) {
    JSONObject result = new JSONObject();
    ArrayList<JSONObject> indices = new ArrayList<>();
    if (es_url.contains(":") && StringUtils.split(es_url, ":").length == 2) {
        String[] esU = StringUtils.split(es_url, ":");
        String esIp = esU[0];
        int esPort = Integer.valueOf(esU[1]);
        Settings settings = Settings.builder().put("cluster.name", esCluster_name)
                .put("client.transport.sniff", true)  // 开启嗅探,自动搜寻新加入集群IP
                .build();
        TransportClient transportClient = null;
        try {
            transportClient = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esIp), esPort));
            ActionFuture<GetMappingsResponse> mappings = transportClient.admin().indices().getMappings(new GetMappingsRequest());
            Iterator<ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>>> iterator = mappings.actionGet().getMappings().iterator();
            while (iterator.hasNext()) {
                ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> next = iterator.next();
                String key = next.key;
                ImmutableOpenMap<String, MappingMetaData> value = next.value;
                Iterator<String> stringIterator = value.keysIt();
                while (stringIterator.hasNext()) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("index_name", key);
                    jsonObject.put("index_type", stringIterator.next());
                    indices.add(jsonObject);
                }
            }
            result.put("succeed", true);
            result.put("indices", indices);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } finally {
            if (transportClient != null) {
                transportClient.close();
            }
        }
    }else {
        result.put("succeed", false);
        result.put("msg", "连接es的地址输入格式有误!");
        return result;
    }

    /*if (es_url.contains(":") && StringUtils.split(es_url, ":").length == 2) {
        String[] esU = StringUtils.split(es_url, ":");
        String esIp = esU[0];
        ArrayList<JSONObject> indices = new ArrayList<>();
        JSONObject body = restTemplate.getForEntity("http://" + esIp + ":" + esHttpPort + "/_mappings", JSONObject.class).getBody();
        if (!body.isEmpty()) {
            for (String indexName : body.keySet()) {
                JSONObject o = body.getJSONObject(indexName);
                JSONObject mappings = o.getJSONObject("mappings");

                for (String esType : mappings.keySet()) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("index_name", indexName);
                    jsonObject.put("index_type", esType);
                    indices.add(jsonObject);
                }
            }
            result.put("succeed", true);
            result.put("indices", indices);
        } else {
            result.put("succeed", false);
            result.put("errmsg", "未发现集群中的index!");
        }
    } else {
        result.put("succeed", false);
        result.put("msg", "连接es的地址输入格式有误!");
        return result;
    }*/
    return result;
}
 
Example #22
Source File: ElasticsearchTable.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public RelDataType[] getRowTypeFromEs(RelDataTypeFactory typeFactory) throws IOException {
    RelDataType[] relDataTypes = new RelDataType[2];
    RelDataTypeFactory.FieldInfoBuilder builder = typeFactory.builder();
    RelDataTypeFactory.FieldInfoBuilder builder1 = typeFactory.builder();
    GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings(esIndexName).get();
    MappingMetaData typeMapping = getMappingsResponse.getMappings().get(esIndexName).get(esTypeName);
    //{"person":{"properties":{"age":{"type":"integer"},"name":{"type":"text","fields":{"raw":{"type":"keyword"}}}}}}
    String json = typeMapping.source().string();
    Map<String, Object> map = new ObjectMapper().readValue(json, new TypeReference<Map<String, Object>>() {
    });
    Map<String, Object> properties = ((Map<String, Map<String, Object>>) map.get(esTypeName)).get("properties");
    int index = 0;
    int index1 = 0;
    //(base-field-name, field-map)
    Stack<Pair<String, Map<String, Object>>> mapStack = new Stack<>();
    mapStack.push(Pair.of(null, properties));
    while (!mapStack.isEmpty()) {
        Pair<String, Map<String, Object>> pair = mapStack.pop();
        String baseFieldName = pair.left;
        for (Map.Entry<String, Object> entry : pair.right.entrySet()) {
            String name = entry.getKey().toUpperCase();
            if (baseFieldName != null) name = baseFieldName + "." + name;

            Map<String, Object> fieldMap = (Map<String, Object>) entry.getValue();
            String type = fieldMap.get("type") != null ? fieldMap.get("type").toString() : null;
            if (type == null)
                throw new IllegalStateException(String.format("type of elasticsearch field '%s' is null", name));
            builder.add(new RelDataTypeFieldImpl(name, index++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));

            if(!name.endsWith(".KEYWORD")){
                builder1.add(new RelDataTypeFieldImpl(name, index1++, typeFactory.createJavaType(ElasticSearchFieldType.of(type).getClazz())));
            }
            //multi-field, that means containing 'fields' attribute
            Map<String, Object> moreFields = fieldMap.get("fields") != null ? (Map<String, Object>) fieldMap.get("fields") : null;
            if (moreFields != null) mapStack.push(Pair.of(name, moreFields));
        }
    }
    relDataTypes[0] = builder.build();
    relDataTypes[1] = builder1.build();
    return relDataTypes;
}
 
Example #23
Source File: ElasticSearchService.java    From Mahuta with Apache License 2.0 3 votes vote down vote up
private Map<String, String> getMapping(String indexName) {
    Map<String, String> mapping = new HashMap<>();

    GetMappingsResponse response = this.client.admin().indices().prepareGetMappings(indexName).get();
    log.debug("GetMappingsResponse={}", response);
   
    MappingMetaData mappingMetaData = response.getMappings().get(indexName).get(DEFAULT_TYPE).get();
    LinkedHashMap<String, LinkedHashMap> map = (LinkedHashMap<String, LinkedHashMap>) mappingMetaData.getSourceAsMap().get("properties");
    
    map.forEach((key, value) -> mapping.put(key, (String) value.get("type")));
    
    return mapping;
}
 
Example #24
Source File: EsIndex.java    From AsuraFramework with Apache License 2.0 2 votes vote down vote up
/**
 * 获取mapping
 *
 * @param indexName
 * @param indexType
 *
 * @return
 */
public ActionResponse getMapping(String indexName, String indexType) {
    TransportClient client = esClientFactory.getClient();
    GetMappingsResponse response = client.admin().indices().prepareGetMappings(esClientFactory.getIndexs(indexName)).setTypes(indexType).get();
    return response;
}