ElasticSearch Java API 简单使用

版本为:
elasticsearch-5.2.2
jdk 1.8
相关jar包下面我会都贴上


一、首先创建一个maven项目(当然不一定非要maven,但是依赖的jar包比较多,简单粗暴最好是用maven)下面是pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>

</project>
二、具体实现
package test.test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class Test_ES {
    private static String elasticSearchIP = "10.255.0.167"; //ip
    private static String elasticSearchPort = "9300"; //端口

    private static Client client;

    /**
     * 保存一个数据
     */
    public static void main(String[] args) {
        try {
            // 设置集群名称
            Settings settings = Settings.builder().put("cluster.name", "native_ess").build();
            // 创建client
            client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(
                    InetAddress.getByName(elasticSearchIP), Integer.parseInt(elasticSearchPort)));

            BulkRequestBuilder bulkRequest = client.prepareBulk();
            //第一个为索引值  第二个为type表 第三个为id
            IndexRequestBuilder indexRequest = client
                    .prepareIndex("test_reg", "logs","AVyul_cYVGgMlwaX8gnD").setSource(createJson4());
            bulkRequest.add(indexRequest);
            try {
                BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                if (bulkResponse.hasFailures()) {
                    System.out.println(bulkResponse.buildFailureMessage());
                }
                }catch(Exception e) {
                    e.printStackTrace();
                    }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

     /**
      * 按照索引和表名和id进行单条数据的查询
      * @param args
      */
     public static void main1(String[] args) {
         //设置集群名称
         Settings settings = Settings.builder().put("cluster.name", "native_ess").build();
         //创建client
         TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(settings)
                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticSearchIP), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
         //搜索数据 第一个为索引值  第二个为type表 第三个为id
         GetResponse response = client.prepareGet("test_reg", "logs", "AVyul_cYVGgMlwaX8gnD").execute().actionGet();
         //输出结果
         System.out.println(response.getSourceAsString());
         //关闭client
         client.close();
     }

    /**
     * 这是一个帮助类,里面是添加的字段信息
     */
    public static XContentBuilder createJson4() throws Exception {
        // 创建json对象, 其中一个创建json的方式
        XContentBuilder source = XContentFactory.jsonBuilder()
            .startObject()
                .field("user", "kimchy")
                .field("postDate", new Date())
                .field("message", "trying to out ElasticSearch")
            .endObject();
        return source;
    }
}
三、依赖的jar包(直接使用maven命令到出来的)
/test/target/dependency/commons-codec-1.10.jar
/test/target/dependency/commons-logging-1.1.3.jar
/test/target/dependency/compiler-0.9.3.jar
/test/target/dependency/elasticsearch-5.2.2.jar
/test/target/dependency/HdrHistogram-2.1.6.jar
/test/target/dependency/hppc-0.7.1.jar
/test/target/dependency/httpasyncclient-4.1.2.jar
/test/target/dependency/httpclient-4.5.2.jar
/test/target/dependency/httpcore-4.4.5.jar
/test/target/dependency/httpcore-nio-4.4.5.jar
/test/target/dependency/jackson-core-2.8.6.jar
/test/target/dependency/jackson-dataformat-cbor-2.8.6.jar
/test/target/dependency/jackson-dataformat-smile-2.8.6.jar
/test/target/dependency/jackson-dataformat-yaml-2.8.6.jar
/test/target/dependency/jna-4.2.2.jar
/test/target/dependency/joda-time-2.9.5.jar
/test/target/dependency/jopt-simple-5.0.2.jar
/test/target/dependency/junit-3.8.1.jar
/test/target/dependency/lang-mustache-client-5.2.2.jar
/test/target/dependency/log4j-api-2.7.jar
/test/target/dependency/log4j-core-2.7.jar
/test/target/dependency/lucene-analyzers-common-6.4.1.jar
/test/target/dependency/lucene-backward-codecs-6.4.1.jar
/test/target/dependency/lucene-core-6.4.1.jar
/test/target/dependency/lucene-grouping-6.4.1.jar
/test/target/dependency/lucene-highlighter-6.4.1.jar
/test/target/dependency/lucene-join-6.4.1.jar
/test/target/dependency/lucene-memory-6.4.1.jar
/test/target/dependency/lucene-misc-6.4.1.jar
/test/target/dependency/lucene-queries-6.4.1.jar
/test/target/dependency/lucene-queryparser-6.4.1.jar
/test/target/dependency/lucene-sandbox-6.4.1.jar
/test/target/dependency/lucene-spatial-6.4.1.jar
/test/target/dependency/lucene-spatial-extras-6.4.1.jar
/test/target/dependency/lucene-spatial3d-6.4.1.jar
/test/target/dependency/lucene-suggest-6.4.1.jar
/test/target/dependency/netty-3.10.6.Final.jar
/test/target/dependency/netty-buffer-4.1.7.Final.jar
/test/target/dependency/netty-codec-4.1.7.Final.jar
/test/target/dependency/netty-codec-http-4.1.7.Final.jar
/test/target/dependency/netty-common-4.1.7.Final.jar
/test/target/dependency/netty-handler-4.1.7.Final.jar
/test/target/dependency/netty-resolver-4.1.7.Final.jar
/test/target/dependency/netty-transport-4.1.7.Final.jar
/test/target/dependency/percolator-client-5.2.2.jar
/test/target/dependency/reindex-client-5.2.2.jar
/test/target/dependency/rest-5.2.2.jar
/test/target/dependency/securesm-1.1.jar
/test/target/dependency/snakeyaml-1.15.jar
/test/target/dependency/t-digest-3.0.jar
/test/target/dependency/transport-5.2.2.jar
/test/target/dependency/transport-netty3-client-5.2.2.jar
/test/target/dependency/transport-netty4-client-5.2.2.jar


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值