使用TransportClient操作ElasticSearch5.2

1.官网地址

  https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-get.html

2.环境准备,添加Maven依赖

 


    org.elasticsearch.client
    transport
    5.2.2

2.简单连接测试

 

// on startup
 TransportClient client =newPreBuiltTransportClient(Settings.EMPTY)

.addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName("host1"),9300))

.addTransportAddress(newInetSocketTransportAddress(InetAddress.getByName("host2"),9300));

// on shutdown

client.close();

   测试结果失败,报错。

   Unsupported major.minor version 52.0

   解决方案,切换jdk版本为1.8

   持续报错

   Exception in thread "main" java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW

   引入maven依赖

    
        com.fasterxml.jackson.core 
         jackson-core 
          2.8.1 
  

   持续报错

   Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger

   这个错误很明显 log4j jar包缺失的错误

 
        org.apache.logging.log4j
        log4j-core
        2.6.2
       

       
        org.apache.logging.log4j
        log4j-api
        2.6.2
       

       
        org.apache.logging.log4j
        log4j-1.2-api
        2.6.2
  

   问题得到解决,测试通过!!!

4.GET查询(通过ID查询一条记录)

 

GetResponse response = client.prepareGet("twitter","tweet","1").setOperationThreaded(false).get();

 上边是官方API示例。如果想打印值,可以调用response.getSource()方法,得到一个Map

  

  

 

你可能感兴趣的:(elasticsearch,大数据,java)