博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用java如何操作elasticsearch?简单示例。
阅读量:4693 次
发布时间:2019-06-09

本文共 1760 字,大约阅读时间需要 5 分钟。

在线API:

https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/transport-client.html
教程:
http://blog.java1234.com/blog/articles/345.html
注意:
不同版本的ES API差别较大,引入jar包版本一定要和生产保持一致。工具类及使用方法可以参考备件系统项目:源码见GitHub
工具类及使用方法可以参考备件系统项目:源码见GitHub

 

引入jar包:

<dependency>

<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
</dependency>

写测试类:

 

package com.sxt.es.test;import java.net.InetAddress;import java.util.Date;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentFactory;public class Testes {        private static String host="192.168.136.131"; // 服务器地址    private static int port=9300; // 端口        public static void main(String[] args) throws Exception {                TransportClient client = TransportClient.builder().build()                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Testes.host), Testes.port));        IndexResponse response =client.prepareIndex("twitter", "tweet", "1")                .setSource(XContentFactory.jsonBuilder()                        .startObject()                        .field("user", "kimchy")                        .field("postDate", new Date())                        .field("message", "trying out Elasticsearch")                    .endObject()                        )                .get();            System.out.println("索引名称:"+response.getIndex());            System.out.println("类型:"+response.getType());            System.out.println("文档ID:"+response.getId()); // 第一次使用是1        client.close();    }}

 

转载于:https://www.cnblogs.com/xyhero/p/9339189.html

你可能感兴趣的文章
VBA trouble
查看>>
HUD Is It A Tree?!!!!!)
查看>>
电梯调度算法(-)
查看>>
Two Sum III - Data Structure Design
查看>>
java web----jsp自定义标签
查看>>
SQL知识
查看>>
krpano之字幕添加
查看>>
面向接口编程 --对象的三种依赖
查看>>
另一种图片上传 jquery.fileupload.js
查看>>
ibatis 中isNull, isNotNull与isEmpty, isNotEmpty区别
查看>>
div+css命名参考
查看>>
常用工具集合
查看>>
第二章 开发环境配置
查看>>
java中函数传值和传地址的问题
查看>>
Debian下载地址
查看>>
VideoView播放视频
查看>>
获取gcc和clang的内置宏定义
查看>>
CF1027D Mouse Hunt题解
查看>>
php 1-10,1.10 PHP异常处理
查看>>
php 循环赋值,thinkphp如何在js中循环赋值
查看>>