All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mengxianun.elasticsearch.ElasticsearchDataContextFactory Maven / Gradle / Ivy

package com.github.mengxianun.elasticsearch;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.mengxianun.core.DataContextFactory;
import com.github.mengxianun.elasticsearch.attributes.ElasticsearchDatasourceConfig;
import com.google.auto.service.AutoService;
import com.google.gson.JsonObject;

@AutoService(DataContextFactory.class)
public class ElasticsearchDataContextFactory implements DataContextFactory {

	private static final Logger logger = LoggerFactory.getLogger(ElasticsearchDataContextFactory.class);

	@Override
	public String getType() {
		return "elasticsearch";
	}

	@Override
	public ElasticsearchDataContext create(JsonObject dataSourceJsonObject) {
		String multiUrl = dataSourceJsonObject.get(ElasticsearchDatasourceConfig.URL).getAsString();
		List httpHosts = createHttpHost(multiUrl);
		return new ElasticsearchDataContext(httpHosts.stream().toArray(HttpHost[]::new));
	}

	private List createHttpHost(String multiUrl) {
		List httpHosts = new ArrayList<>();
		String[] urls = multiUrl.split(",");
		for (String urlString : urls) {
			URL url;
			try {
				url = new URL(urlString);
				httpHosts.add(new HttpHost(url.getHost(), url.getPort()));
			} catch (MalformedURLException e) {
				logger.error("URL parsing failed", e);
			}
		}
		return httpHosts;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy