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. 2015&#183;Fool&#39;s Day&#183;NND

    本博文没有主旨,仅仅是记录. ============================ Date:2015/4/1 - April Fool's Day! Addr:ZhongHai ======== ...

  2. WebView中input file的解决方法

    public class MyWb extends Activity { /** Called when the activity is first created. */ WebView web; ...

  3. WEB前端的性能优化

    转自:http://www.2cto.com/kf/201604/498725.html 网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发 ...

  4. ORACLE修改列名与列类型

    --修改列名 alter table 表名 rename column 旧列名 to 新列名; --修改列类型 )); 删除表的一列: alter table 表名 drop column 列名 给表 ...

  5. 在Windows下使用nmake+Makefile+编译ZThread库(附例子)

    ----------2015/01/09/23:21更新----------------------------------- 关于保留DEBUG信息的一个简单例子,见这篇随笔 ----------2 ...

  6. JVM Specification 9th Edition (2) Chapter 1. Introduction

    Chapter 1. Introduction 翻译太累了,我就这样的看英文吧. 内容列表 1.1. A Bit of History 1.2. The Java Virtual Machine 1. ...

  7. 解决linux下/etc/rc.local开机器不执行的原因

    前不久因项目需要写了开机启动其他程序的shell脚本,因工作忙,调试完给了技术支持人员,也没去注意过. 到后来,有几台服务器突然被重启了,这时候领导问,怎么开机启动的脚本没起作用,还被批了一顿,哎,做 ...

  8. SQL宝典

    SQL Server 数据库的高级操作 (1) 批处理 (2) 变量 (3) 逻辑控制 (4) 函数 (5) 高级查询 */ (1)批处理 将多条SQL语句作为一个整体去编译,生成一个执行计划,然后, ...

  9. PAT001 一元多项式求导

    题目: 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格 ...

  10. URL编码,空格和+

    下表中列出了一些URL特殊符号及编码 + URL 中+号表示空格 %2B 空格 URL中的空格可以用+号或者编码 %20 / 分隔目录和子目录 %2F ? 分隔实际的URL和参数 %3F % 指定特殊 ...