Elasticsearch5和head最新插件学习,JavaAPI demo

来源:互联网 发布:php bin2hex写入文件 编辑:程序博客网 时间:2024/06/08 18:44

本文是在window10下使用

1.elasticsearch安装 使用

1.1.下载地址:https://www.elastic.co/downloads/elasticsearch

1.2. 选择zip文件,下载解压缩到 C:\elasticsearch-5.4.1

1.3. 修改配置文件

 为了允许elasticsearch-head运行时的跨域, 打开C:\elasticsearch-5.4.1\config中的elasticsearch.yml文件,文件末尾添加

# allow originhttp.cors.enabled: truehttp.cors.allow-origin: "*"

cluster.name 可修改集群名称

node.name 节点名称

network.host ES的监听地址,修改为0.0.0.0 别的机器可访问

http.port: 端口号

1.4.启动elastic,打开cmd   ——》 cd C:\elasticsearch-5.4.1\bin ——》elasticsearch.bat

1.5. 浏览器打开 http://localhost:9200/ 即可看到成功运行的JSON页面


更多配置参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html

2.head可视化插件安装

1. Node.js 下载地址 https://nodejs.org/en/

 傻瓜式安装,cmd 测试下是否安装成功 CMD ——》 node -v 

C:\Users\zhangmy>node -vv8.1.2
2. 安装grunt 

grunt是一个很方便的构建工具,可以进行打包压缩、测试、执行等等的工作,5.0里的head插件就是通过grunt启动的。因此需要安装grunt:

路径切换到nodejs的安装目录下

C:\Users\zhangmy>cd C:\Program Files\nodejs
C:\Program Files\nodejs>npm install -g grunt-cliC:\Users\zhangmy\AppData\Roaming\npm\grunt -> C:\Users\zhangmy\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt+ grunt-cli@1.2.0added 16 packages in 5.931s

npm install -g grunt-cli

-g代表全局安装。安装路径为C:\Users\yourname\AppData\Roaming\npm,并且自动加入PATH变量。安装完成后检查一下:

C:\Program Files\nodejs>grunt -versiongrunt-cli v1.2.0
3. 下载head

官网github地址: https://github.com/mobz/elasticsearch-head

git clone https://github.com/mobz/elasticsearch-head 到本地

进入head下载目录,执行npm install 下载的包(如果失败,可重新运行一次),最后启动nodejs服务 grunt server 
C:\Users\zhangmy>cd H:\work\project\elasticsearch-headH:\work\project\elasticsearch-head>npm installnpm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expressionup to date in 1.51sH:\work\project\elasticsearch-head>grunt serverRunning "connect:server" (connect) taskWaiting forever...Started connect web server on http://localhost:9100
访问http://localhost:9100就可以访问head插件了:



3. head插件学习

http://mobz.github.io/elasticsearch-head/

1.概览

这个页面可以看到基本的分片的信息,比如主分片、副本分片等等,以及多少分片可用。

上方elasticsearch是集群的名称,颜色表示集群的健康状态:

绿色表示主分片和副本分片都可用;
黄色表示只有主分片可用,没有副本分片;
红色表示主分片中的部分索引不可用,但是不耽误某些索引的访问。


2.索引

可创建索引,设置分片数量,副本数量等


3.数据浏览

可以每个索引有哪些字段,存储了哪些数据


4.基本查询(类似CMS系统的简单查询)


5.复合查询

这里可使用restful风格对数据进行增删改查操作,具体操作可参考 http://blog.csdn.net/wuyzhen_csdn/article/details/51586082

GET格式:index/_type/_id


4. JDk8 +  elasticsearch5.4.1 完成新增索引

非常详细的官方JAVA API文档

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

gradle引入jar包

    compile('org.elasticsearch:elasticsearch:5.4.1')    compile('org.elasticsearch.client:transport:5.4.1')    compile('org.apache.logging.log4j:log4j-api:2.8.2')    compile('org.apache.logging.log4j:log4j-core:2.8.2')


package com.wdletu.travel.admin;import org.elasticsearch.action.index.IndexResponse;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.rest.RestStatus;import org.elasticsearch.transport.client.PreBuiltTransportClient;import java.net.InetAddress;import java.util.ArrayList;import java.util.List;/** * Created by zhangmy on 2017/6/20. */public class Test {    public static void main(String[] args) throws Exception {        // on startup        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));        List<String> jsondata = getInitJsonData();        for (int i = 0; i < jsondata.size(); i++) {            IndexResponse response = client.prepareIndex("blog", "article").setSource(jsondata.get(i)).get();            if (response.status()== RestStatus.CREATED) {                System.out.println("创建成功!");            }        }        client.close();    }    // Java实体对象转json对象    public static String model2Json(Blog blog) throws Exception {        String jsonData = null;        XContentBuilder builder = XContentFactory.jsonBuilder()                .startObject()                .field("id", blog.getId())                .field("title", blog.getTitle())                .field("posttime", blog.getPosttime())                .field("content", blog.getContent())                .endObject();        jsonData = builder.string();        return jsonData;    }    public static List<String> getInitJsonData() throws Exception {        List<String> list = new ArrayList<String>();        String data1 = model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));        String data2 = model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));        String data3 = model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));        String data4 = model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));        String data5 = model2Json(new Blog(5, "Shell基本知识", "2016-06-19", "Shell是什么..."));        list.add(data1);        list.add(data2);        list.add(data3);        list.add(data4);        list.add(data5);        return list;    }}class Blog {    private Integer id;    private String title;    private String posttime;    private String content;    public Blog() {    }    public Blog(Integer id, String title, String posttime, String content) {        this.id = id;        this.title = title;        this.posttime = posttime;        this.content = content;    }    //setter and getter    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getPosttime() {        return posttime;    }    public void setPosttime(String posttime) {        this.posttime = posttime;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }}


感谢分享,参考网址http://blog.csdn.net/yx1214442120/article/details/55102298

http://www.cnblogs.com/xing901022/p/5469338.html

原创粉丝点击