java连接elastic search

导入jar包:https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/_maven_repository.html

注意,使用的jar包版本尽量与所连的els版本一致

创建连接:https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/transport-client.html

查询:https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-search.html

发现两个错误:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

错误原因:java连接els用到了log4j2,所以需要log4j2-core的依赖

解决办法:添加log4j2-core依赖

https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/_log4j_2_logger.html

NoNodeAvailableException[None of the configured nodes are available: [{#

错误原因:创建连接时需要配置cluster_name。

elasticsearch服务器的cluster_name可以在#els_home#/config/elasticsearch.yml文件中配置。

然后在java客户端程序中将

TransportClient client=new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("139.199.32.101"),9300));

改为

Settings settings=Settings.builder().put("cluster.name","production").build();
TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("139.199.32.101"),9300));

JAVA客户端代码:

package com.tpot.DataDownload;

import java.net.InetAddress;
import java.net.UnknownHostException; //import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws UnknownHostException
{
System.out.println( "Hello World!" ); //连接+配置
Settings settings=Settings.builder().put("cluster.name","production").build();
TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("139.xxx.xxx.101"),9300)); //查询
SearchResponse response = client.prepareSearch().get();
System.out.println(response); //关闭连接
client.close();
}
}

pom.xml文件中添加的依赖:

  <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.3</version>
</dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>

java连接elastic search 9300的更多相关文章

  1. elastic search&logstash&kibana 学习历程(一)es基础环境的搭建

    elastic search 6.1.x 常用框架: 1.Lucene Apache下面的一个开源项目,高性能的.可扩展的工具库,提供搜索的基本架构: 如果开发人员需用使用的话,需用自己进行开发,成本 ...

  2. elastic search book [ ElasticSearch book es book]

    谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...

  3. elastic search远程测试

    elastic search远程测试 推荐:elastic官方教程:https://www.elastic.co/guide/en/elasticsearch/reference/6.2/index. ...

  4. elastic search安装与本地测试

    elastic search安装与本地测试 elastic search是一个全文搜索引擎 教程: 综合:http://www.ruanyifeng.com/blog/2017/08/elastics ...

  5. Elastic Search快速上手(2):将数据存入ES

    前言 在上手使用前,需要先了解一些基本的概念. 推荐 可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htm ...

  6. Elastic Search快速上手(1):简介及安装配置

    前言 最近开始尝试学习Elastic Search,因此决定做一些简单的整理,以供后续参考,快速上手使用ES. 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多 ...

  7. SpringMVC项目使用elastic search搜索

    项目需要,引入了elastic search(后续简称es),后面将介绍本地对es的安装,使用以及java连接es查询的整个过程. 1.es索引字段建立与修改,以curl新增一个索引字段示例 curl ...

  8. 教你用Elastic Search:运行第一条Hello World搜索命令

    摘要:Elastic Search可实时对数据库进行全文检索.处理同义词.从同样的数据中生成分析和聚合数据. 本文分享自华为云社区<Elastic Search入门(一): 简介,安装,运行第一 ...

  9. docker安装elastic search和kibana

    安装目标 使用docker安装elastic search和kibana,版本均为7.17.1 安装es 1. docker pull 去dockerhub看具体版本,这里用7.17.1 docker ...

随机推荐

  1. C语言第十一回合:预处理命令的集中营

    C语言第十一回合:预处理命令的集中营   [学习目标]   1.         宏定义 2.         文件包括"处理 3.         条件编译 预处理命令:能够改进程序设计的 ...

  2. 转-linux下配置socks5代理

    简介: 在Linux下有各种各样的代理程序可用,象最常用的Squid,是http/https代理,也能代理ftp请求,但它实际上 是个HTTP代理程序,不是ftp代理,但它能处理ftp代理请求,就象浏 ...

  3. flask学习笔记(-操作数据库)

    Python 数据库框架 大多数的数据库引擎都有对应的 Python 包,包括开源包和商业包.Flask 并不限制你使用何种类型的数据库包,因此可以根据自己的喜好选择使用 MySQL.Postgres ...

  4. Windows虚拟环境下安装mysql-python

    因为在虚拟环境下安装mysql-python走了许多弯路,特此记录,也希望以后的朋友避免像我一样,被环境配置问题搞的半死 直接使用pip安装mysql-python会报错 pip install My ...

  5. 凑成整数x----二进制枚举

    给n个数,给定x. 用这n个数求和凑成x,问有多少种凑法. #include<bits/stdc++.h> using namespace std; int main() { int n, ...

  6. STM32F10x_SPI(硬件接口 + 软件模拟)读写Flash(25Q16)

    推荐 分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来! http://www.captainbed.net/strongerhuang Ⅰ.写在前 ...

  7. PHP——做服务

    xml的写法和特点 <?xml version='1.0' encoding='utf-8'?><Info><code>c001</code><n ...

  8. cannot be translated into a null value due to being declared as a primitive type. Consid

    严重: Servlet.service() for servlet [note-portal] in context with path [] threw exception [Request pro ...

  9. php 合并数组的方法 非array_merge

    Array ( [0] => Array ( [max] => 50 [date] => 2016-01-07 ) [1] => Array ( [max] => 100 ...

  10. 【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3296 显然,每群能交流的群是个强联通块 然后求出scc的数量,答案就是scc-1 #include ...