本文整理汇总了Java中org.elasticsearch.transport.client.PreBuiltTransportClient类的典型用法代码示例。如果您正苦于以下问题:Java PreBuiltTransportClient类的具体用法?Java PreBuiltTransportClient怎么用?Java PreBuiltTransportClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PreBuiltTransportClient类属于org.elasticsearch.transport.client包,在下文中一共展示了PreBuiltTransportClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getElasticClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
private TransportClient getElasticClient() {
try {
// un-command this, if you have multiple node
// TransportClient client1 = new PreBuiltTransportClient(Settings.EMPTY)
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300));
Settings setting = Settings.builder()
.put("cluster.name", elasticPro.getProperty("cluster"))
.put("client.transport.sniff", Boolean.valueOf(elasticPro.getProperty("transport.sniff"))).build();
client = new PreBuiltTransportClient(setting)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticPro.getProperty("host")), Integer.valueOf(elasticPro.getProperty("port"))));
} catch (UnknownHostException ex) {
log.error("Exception occurred while getting Client : " + ex, ex);
}
return client;
}
示例2: createClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
private TransportClient createClient() {
Config cfg = getTypeCfg();
Settings settings = Settings.builder().put("cluster.name", cfg.getString("elastic.cluster-name")).build();
TransportClient client = new PreBuiltTransportClient(settings);
List<String> servers = cfg.getStringList("elastic.servers");
logger.debug(marker, "Elastic Servers: {}", servers);
for (String addr : servers) {
try {
String[] a = addr.split(":");
String host = a[0];
int port = Integer.parseInt(a[1]);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
} catch (Exception e) {
logger.error(marker, "Transport client creation failed for '{}'", addr, e);
}
}
return client;
}
示例3: ElasticsearchClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
/**
* create a elasticsearch transport client (remote elasticsearch)
* @param addresses an array of host:port addresses
* @param clusterName
*/
public ElasticsearchClient(final String[] addresses, final String clusterName) {
// create default settings and add cluster name
Settings.Builder settings = Settings.builder()
.put("cluster.routing.allocation.enable", "all")
.put("cluster.routing.allocation.allow_rebalance", "always");
if (clusterName != null) settings.put("cluster.name", clusterName);
// create a client
TransportClient tc = new PreBuiltTransportClient(settings.build());
for (String address: addresses) {
String a = address.trim();
int p = a.indexOf(':');
if (p >= 0) try {
InetAddress i = InetAddress.getByName(a.substring(0, p));
int port = Integer.parseInt(a.substring(p + 1));
tc.addTransportAddress(new InetSocketTransportAddress(i, port));
} catch (UnknownHostException e) {
Data.logger.warn("", e);
}
}
this.elasticsearchClient = tc;
}
示例4: get
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
/**
* TransportClient provider.
* @return TransportClient
*/
public TransportClient get() {
final String hostCsv = configuration.getString(CONFIG_ES_CLUSTER_HOST);
final List<String> hosts = Splitter.on(",").splitToList(hostCsv);
Preconditions.checkState(!hosts.isEmpty());
final TransportClient transportClient = new PreBuiltTransportClient(esSettings());
final Integer esTransportPort = configuration.getInteger(CONFIG_ES_CLUSTER_PORT, 9300);
log.info("connect to elastic search {} on port {} ", hostCsv, esTransportPort);
hosts.forEach(
host -> transportClient.addTransportAddress(
new InetSocketTransportAddress(new InetSocketAddress(host, esTransportPort))
)
);
return transportClient;
}
示例5: connect
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
@Override
public TransportClient connect() {
Settings settings = Settings.builder()
.put("cluster.name", "elasticsearch")
.put("client.transport.sniff", true).build();
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("event-apptst01.as.it.ubc.ca"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("event-apptst02.as.it.ubc.ca"), 9300));
} catch (UnknownHostException uhe) {
logger.error(uhe.toString());
}
return client;
}
示例6: ElasticSearchConnection
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
public ElasticSearchConnection(String jdbcUrl) {
Settings settings = Settings.builder().put("client.transport.ignore_cluster_name", true).build();
try {
TransportClient transportClient = new PreBuiltTransportClient(settings);
String hostAndPortArrayStr = jdbcUrl.split("/")[2];
String[] hostAndPortArray = hostAndPortArrayStr.split(",");
for (String hostAndPort : hostAndPortArray) {
String host = hostAndPort.split(":")[0];
String port = hostAndPort.split(":")[1];
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), Integer.parseInt(port)));
}
client = transportClient;
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
示例7: createClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
@Override
public Client createClient(Map<String, String> clientConfig) {
Settings settings = Settings.builder().put(clientConfig)
.put(NetworkModule.HTTP_TYPE_KEY, Netty3Plugin.NETTY_HTTP_TRANSPORT_NAME)
.put(NetworkModule.TRANSPORT_TYPE_KEY, Netty3Plugin.NETTY_TRANSPORT_NAME)
.build();
TransportClient transportClient = new PreBuiltTransportClient(settings);
for (TransportAddress transport : ElasticsearchUtils.convertInetSocketAddresses(transportAddresses)) {
transportClient.addTransportAddress(transport);
}
// verify that we actually are connected to a cluster
if (transportClient.connectedNodes().isEmpty()) {
throw new RuntimeException("Elasticsearch client is not connected to any Elasticsearch nodes!");
}
if (LOG.isInfoEnabled()) {
LOG.info("Created Elasticsearch TransportClient with connected nodes {}", transportClient.connectedNodes());
}
return transportClient;
}
示例8: build
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
/**
* Builds the {@link MaprElasticSearchService} according to the specified properties.
*
* @return instence of {@link MaprElasticSearchService}, which can be started via {@link MaprElasticSearchService#start()}.
* @throws IllegalStateException in case when some of the required properties are missed.
*/
public MaprElasticSearchService build() {
ensureFieldNonNull("port", this.port);
ensureFieldNonNull("hostname", this.inetAddress);
ensureFieldNonNull("indexName", this.indexName);
ensureFieldNonNull("typeName", this.typeName);
ensureFieldNonNull("changelog", this.changelog);
ensureFieldNonNull("fields", this.fields);
return () -> {
// Create ES Client
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(inetAddress, port));
// Create CDC Listener
ChangelogListener listener = ChangelogListenerImpl.forChangelog(changelog);
// Set 'onInsert' callback
listener.onInsert(new SaveIndexCDCCallback(client));
// Set 'onUpdate' callback
listener.onUpdate(new SaveIndexCDCCallback(client));
// Define and set 'onDelete' callback
listener.onDelete((id) -> client.prepareDelete(indexName, typeName, id).get());
listener.listen();
};
}
示例9: createTransportClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
private static TransportClient createTransportClient(ElasticsearchSearchIndexConfiguration config) {
Settings settings = tryReadSettingsFromFile(config);
if (settings == null) {
Settings.Builder settingsBuilder = Settings.builder();
if (config.getClusterName() != null) {
settingsBuilder.put("cluster.name", config.getClusterName());
}
for (Map.Entry<String, String> esSetting : config.getEsSettings().entrySet()) {
settingsBuilder.put(esSetting.getKey(), esSetting.getValue());
}
settings = settingsBuilder.build();
}
TransportClient transportClient = new PreBuiltTransportClient(settings);
for (String esLocation : config.getEsLocations()) {
String[] locationSocket = esLocation.split(":");
String hostname;
int port;
if (locationSocket.length == 2) {
hostname = locationSocket[0];
port = Integer.parseInt(locationSocket[1]);
} else if (locationSocket.length == 1) {
hostname = locationSocket[0];
port = config.getPort();
} else {
throw new MemgraphException("Invalid elastic search location: " + esLocation);
}
InetAddress host;
try {
host = InetAddress.getByName(hostname);
} catch (UnknownHostException ex) {
throw new MemgraphException("Could not resolve host: " + hostname, ex);
}
transportClient.addTransportAddress(new InetSocketTransportAddress(host, port));
}
return transportClient;
}
示例10: initESClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
@Bean
public TransportClient initESClient() throws NumberFormatException, UnknownHostException{
String ip = env.getProperty("spring.es.ip");
String port = env.getProperty("spring.es.port");
String clusterName = env.getProperty("spring.es.cluster_name");
Builder builder = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", true);
Settings esSettings = builder.build();
TransportClient client = new PreBuiltTransportClient(esSettings);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), Integer.parseInt(port)));
logger.info("ES Client 初始化成功, ip : {}, port : {}, cluster_name : {}", ip, port, clusterName);
return client;
}
示例11: startClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
private static Client startClient(Path tempDir, TransportAddress... transportAddresses) {
Settings.Builder builder = Settings.builder()
.put("node.name", "qa_smoke_client_" + counter.getAndIncrement())
.put("client.transport.ignore_cluster_name", true)
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir);
final Collection<Class<? extends Plugin>> plugins;
if (random().nextBoolean()) {
builder.put(NetworkModule.TRANSPORT_TYPE_KEY, MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME);
plugins = Collections.singleton(MockTcpTransportPlugin.class);
} else {
plugins = Collections.emptyList();
}
TransportClient client = new PreBuiltTransportClient(builder.build(), plugins).addTransportAddresses(transportAddresses);
logger.info("--> Elasticsearch Java TransportClient started");
Exception clientException = null;
try {
ClusterHealthResponse health = client.admin().cluster().prepareHealth().get();
logger.info("--> connected to [{}] cluster which is running [{}] node(s).",
health.getClusterName(), health.getNumberOfNodes());
} catch (Exception e) {
clientException = e;
}
assumeNoException("Sounds like your cluster is not running at " + clusterAddresses, clientException);
return client;
}
示例12: transportClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
@Bean
TransportClient transportClient() throws UnknownHostException {
return new PreBuiltTransportClient(
Settings.builder()
.put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "slacklistener")
.build()
)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(System.getProperty("elasticsearch.host")), 9300));
}
示例13: ElasticsearchServiceImpl
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
public ElasticsearchServiceImpl(EsConf esConf) throws UnknownHostException {
String clusterName = esConf.getClusterName();
String address = esConf.getAddress();
String[] hostPort = address.split(":");
logger.info("Connect to elasticsearch {}:{}", clusterName, address);
Settings settings = Settings.builder().put("cluster.name", clusterName)
.put("client.transport.sniff", true)
.build();
transportClient = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.valueOf(hostPort[1])));
logger.info("Complete the connection to elasticsearch");
}
示例14: getClient
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
public Client getClient() {
if (!staticValue.isNeedEs()) {
LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
return null;
}
if (client != null) return client;
try {
LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
Settings settings = Settings.builder()
.put("cluster.name", staticValue.getEsClusterName()).build();
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), 9300));
final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
.setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
if (healthResponse.isTimedOut()) {
LOG.error("ES客户端初始化失败");
} else {
LOG.info("ES客户端初始化成功");
}
} catch (IOException e) {
LOG.fatal("构建ElasticSearch客户端失败!");
}
return client;
}
示例15: main
import org.elasticsearch.transport.client.PreBuiltTransportClient; //导入依赖的package包/类
@SuppressWarnings({"unchecked", "resource"})
public static void main(String[] args) throws Exception {
// 先构建client,两个参数分别是:cluster.name 固定参数代表后面参数的含义,集群名称
// client.transport.sniff 表示设置自动探查集群的集群节点
Settings settings = Settings.builder()
.put("cluster.name", "youmeek-cluster")
.put("client.transport.sniff", true)
.build();
//单个节点的写法
TransportClient transportClient = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.127"), 9300));
//batchCreate(transportClient);
query(transportClient);
transportClient.close();
}