存档在 ‘Elasticsearch’ 分类

ElasticSearch同步MySql

2016年8月19日

ElasticSearch同步MySQL的插件选择了elasticsearch-jdbc.

一、下载

下载地址:https://github.com/jprante/elasticsearch-jdbc
下载后解压,里面有bin、lib2个目录.

二、mysql配置

确保mysql能用,在mysql中新建一个test数据库

mysql>create database test;

新建一张user表

mysql> create table user(id int(10) Not null,name char(10));

插入几条数据.

mysql> insert into test values("1","zhangsan");
mysql> insert into user values("2","LiSi");
mysql> insert into user values("3","WangWu");
mysql> insert into user values("4","MaLiu");

查看所有数据:

mysql> select * from user;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | LiSi     |
|  3 | WangWu   |
|  4 | MaLiu    |
+----+----------+
4 rows in set (0.00 sec)

这样mysql中的数据就准备好了.

三、导入数据

新建一个odbc_es文件夹,新建mysql_import_es.sh脚本,脚本内容:

bin=/Users/yaopan/Documents/bropen/elasticsearch-jdbc-2.3.2.0/bin
lib=/Users/yaopan/Documents/bropen/elasticsearch-jdbc-2.3.2.0/lib
echo '{
    "type" : "jdbc",
    "jdbc" : {
        "elasticsearch.autodiscover":true,
        "elasticsearch.cluster":"bropen",
        "url" : "jdbc:mysql://localhost:3306/test",
        "user" : "root",
        "useSSL":"true",
        "password" : "123456",
        "sql" : "select *, id as _id from user",
        "elasticsearch" : {
             "host" : "127.0.0.1",
             "port" : 9300
        },
        "index" : "test", 
        "type" : "user"
    }
}' | java \
       -cp "${lib}/*" \
       -Dlog4j.configurationFile=${bin}/log4j2.xml \
       org.xbib.tools.Runner \
       org.xbib.tools.JDBCImporter

其中bin和lib用了绝对路径.
添加可执行权限:

chmod a+x mysql_import_es.sh 

执行脚本:

./mysql_import_es.sh

报了一个SSL连接的警告,没有错误.如果出现Error: Could not find or load main class org.xbib.tools.Runner之类的错误,很有可能是bin和lib路径出现问题.

查看导入结果 or head插件中查看.

http://localhost:9200/test/user/_search?pretty

参考文章

ElasticSearch同步Mysql
elasticsearch-jdbc实现MySQL同步到ElasticSearch深入详解

ElasticSearch Java Api(四) -删除索引

2016年8月18日

删除可以是删除整个索引库,也可以根据文档id删除索引库下的文档,还可以通过query查询条件删除所有符合条件的数据。

一、删除整个索引库

下面的例子会删除indexName索引:

DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName)
                        .execute().actionGet();

可以根据DeleteIndexResponse对象的isAcknowledged()方法判断删除是否成功,返回值为boolean类型.
如果传人的indexName不存在会出现异常.可以先判断索引是否存在:

IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);

IndicesExistsResponse inExistsResponse = client.admin().indices()
                    .exists(inExistsRequest).actionGet();

根据IndicesExistsResponse对象的isExists()方法的boolean返回值可以判断索引库是否存在.

二、通过ID删除

下面的例子是删除索引名为blog,类型为article,id为1的文档:

DeleteResponse dResponse = client.prepareDelete("blog", "article", "1").execute().actionGet();

通过DeleteResponse对象的isFound()方法,可以得到删除是否成功,返回值为boolean类型.

三、通过Query删除

elasticsearch-2.3 中和旧版本api不太一样,安装插件:

sudo bin/plugin install delete-by-query

集群有多个节点的情况下,每个节点都需要安装并重启.
如果想要移除插件,可以执行以下命令:

sudo bin/plugin remove delete-by-query

删除索引名为twitter,类型为tweet,user字段中含有kimchy的所有文档:

DELETE /twitter/tweet/_query?q=user:kimchy

四、java demo

package cn.com.bropen.es;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;

public class ElasticSearchCreate {

    private static String ServerIP = "127.0.0.1";// ElasticSearch server ip
    private static int ServerPort = 9300;// port
    private Client client;

    public static void main(String[] args) {

        try {
            Client client = TransportClient.builder().build().addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            DeleteResponse dResponse = client.prepareDelete("blog", "article", "11").execute()
                    .actionGet();

            if (dResponse.isFound()) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }

            QueryBuilder qb1 = termQuery("title", "hibernate");


        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

         deleteIndex("test");//删除名为test的索引库
    }

    // 删除索引库

    public static void deleteIndex(String indexName) {

        try {
            if (!isIndexExists(indexName)) {
                System.out.println(indexName + " not exists");
            } else {
                Client client = TransportClient.builder().build().addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName(ServerIP),
                                ServerPort));

                DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName)
                        .execute().actionGet();
                if (dResponse.isAcknowledged()) {
                    System.out.println("delete index "+indexName+"  successfully!");
                }else{
                    System.out.println("Fail to delete index "+indexName);
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    // 创建索引库
    public static void createIndex(String indexName) {
        try {
            Client client = TransportClient.builder().build().addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort));

            // 创建索引库

            if (isIndexExists("indexName")) {
                System.out.println("Index  " + indexName + " already exits!");
            } else {
                CreateIndexRequest cIndexRequest = new CreateIndexRequest("indexName");
                CreateIndexResponse cIndexResponse = client.admin().indices().create(cIndexRequest)
                        .actionGet();
                if (cIndexResponse.isAcknowledged()) {
                    System.out.println("create index successfully!");
                } else {
                    System.out.println("Fail to create index!");
                }

            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

    // 判断索引是否存在 传入参数为索引库名称
    public static boolean isIndexExists(String indexName) {
        boolean flag = false;
        try {
            Client client = TransportClient.builder().build().addTransportAddress(
                    new InetSocketTransportAddress(InetAddress.getByName(ServerIP), ServerPort));

            IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);

            IndicesExistsResponse inExistsResponse = client.admin().indices()
                    .exists(inExistsRequest).actionGet();

            if (inExistsResponse.isExists()) {
                flag = true;
            } else {
                flag = false;
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return flag;
    }

}

ElasticSearch Java Api(三) -更新索引库

2016年8月18日

官网文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update.html

一、UpdateRequest

创建一个UpdateRequest,然后将其发送给client.

UpdateRequest uRequest = new UpdateRequest();
            uRequest.index("blog");
            uRequest.type("article");
            uRequest.id("2");
            uRequest.doc(jsonBuilder().startObject().field("content", "学习目标 掌握java泛型的产生意义ssss").endObject());
            client.update(uRequest).get();

二、prepareUpdate()

2.1使用脚本方式

首先打开elasticsearch-2.3.3/config/elasticsearch.yml,新增一行:

script.engine.groovy.inline.update: on

之后重启elasticsearch.

client.prepareUpdate("blog", "article", "1")
                .setScript(new Script("ctx._source.title = \"git入门\"", ScriptService.ScriptType.INLINE, null, null))
                .get();

2.2使用doc方式

client.prepareUpdate("blog", "article", "1")
                    .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get();

三、updateRequest

UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1")
                    .doc(jsonBuilder().startObject().field("commet", "0").endObject());
            client.update(updateRequest).get();

这种方式可以新增字段。

四、upsert

如果文档不存在则创建新的索引.

IndexRequest indexRequest = new IndexRequest("blog", "article", "10").source(jsonBuilder().startObject()
                    .field("title", "Git安装10").field("content", "学习目标 git。。。10").endObject());

UpdateRequest uRequest2 = new UpdateRequest("blog", "article", "10").doc(
                    jsonBuilder().startObject().field("title", "Git安装").field("content", "学习目标 git。。。").endObject())
                    .upsert(indexRequest);
            client.update(uRequest2).get();

这个例子中,如果blog/article/10存在,那么根据UpdateRequest更新索引;如果不存在,新建indexRequest索引.

五、java demo

package cn.com.bropen.es;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;

import static org.elasticsearch.common.xcontent.XContentFactory.*;

public class ElasticSearchUpdate {

    private static Client client;

    public static void main(String[] args) {

        try {
            // client startup
            client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        upMethod1();

    }

    public static void upMethod1() {
        try {
            // 方法一:创建一个UpdateRequest,然后将其发送给client.
            UpdateRequest uRequest = new UpdateRequest();
            uRequest.index("blog");
            uRequest.type("article");
            uRequest.id("22");
            uRequest.doc(jsonBuilder().startObject().field("content", "学习目标 掌握java泛型的产生意义ssss").endObject());
            client.update(uRequest).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    public static void upMethod2() {
        // 方法二:prepareUpdate() 使用脚本更新索引
        client.prepareUpdate("blog", "article", "1")
                .setScript(new Script("ctx._source.title = \"git入门\"", ScriptService.ScriptType.INLINE, null, null))
                .get();
    }

    public static void upMethod3() {
        // 方法三:prepareUpdate() 使用doc更新索引
        try {
            client.prepareUpdate("blog", "article", "1")
                    .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void upMethod4() {
        // 方法四: 增加新的字段

        try {
            UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1")
                    .doc(jsonBuilder().startObject().field("commet", "0").endObject());
            client.update(updateRequest).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public static void upMethod5() {
        // 方法五:upsert 如果文档不存在则创建新的索引
        try {
            IndexRequest indexRequest = new IndexRequest("blog", "article", "10").source(jsonBuilder().startObject()
                    .field("title", "Git安装10").field("content", "学习目标 git。。。10").endObject());

            UpdateRequest uRequest2 = new UpdateRequest("blog", "article", "10").doc(
                    jsonBuilder().startObject().field("title", "Git安装").field("content", "学习目标 git。。。").endObject())
                    .upsert(indexRequest);
            client.update(uRequest2).get();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

ElasticSearch Java Api(二) -检索索引库

2016年8月18日

一、准备数据

 String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));
    String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));
    String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));
    String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));
    String data5 = JsonUtil.model2Json(new Blog(5, "Git基本知识git", "2016-06-19", "Shell是什么..."));
    String data6 = JsonUtil.model2Json(new Blog(6, "C++基本知识", "2016-06-19", "Shell是什么..."));
    String data7 = JsonUtil.model2Json(new Blog(7, "Mysql基本知识", "2016-06-19", "git是什么..."));

二、查询

一次查询可分为下面四个步骤:

    • 1.创建连接ElasticSearch服务的client.
      索引在ElasticSearch服务器上,进行索引的查询首先要和服务器创建连接,这是第一步。
Client client = TransportClient.builder().build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    • 2.创建QueryBuilder.
      QueryBuilder可以设置单个字段的查询,也可以设置多个字段的查询.
      e.g.1: 查询title字段中包含hibernate关键字的文档:

      QueryBuilder qb1 = termQuery("title", "hibernate");

      e.g.2: 查询title字段或content字段中包含Git关键字的文档:

QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");
    • 3.执行查询
      通过client设置查询的index、type、query.返回一个SearchResponse对象:
SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
            .actionGet();
    • 4.处理查询结果
      SearchResponse对象的getHits()方法获取查询结果,返回一个SearchHits的集合,遍历集合获取查询的文档信息:
SearchHits hits = response.getHits();

三、java代码实现

package cn.com.bropen.es;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import static org.elasticsearch.index.query.QueryBuilders.*;

public class ElasticSearchGet {

    public static void main(String[] args) {
        // client startup
        try {
            Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            QueryBuilder qb1 = termQuery("title", "hibernate");
            QueryBuilder qb2= QueryBuilders.multiMatchQuery("git", "title","content");


            SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(qb2).execute()
                    .actionGet();

            SearchHits hits = response.getHits();
            if (hits.totalHits() > 0) {
                for (SearchHit hit : hits) {
                    System.out.println("score:"+hit.getScore()+":\t"+hit.getSource());// .get("title")
                }
            } else {
                System.out.println("搜到0条结果");
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

}

查询结果:

log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
score:0.5:  {posttime=2016-06-19, id=1, title=git简介, content=SVN与Git最主要的区别...}
score:0.17673586:   {posttime=2016-06-19, id=7, title=Mysql基本知识, content=git是什么...}
score:0.049935166:  {posttime=2016-06-19, id=5, title=Git基本知识git, content=Shell是什么...}

参考资料:

 

ElasticSearch Java Api(一) -创建索引

2016年8月18日

ElasticSearch Java API官网文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html

一、生成JSON

创建索引的第一步是要把对象转换为JSON字符串.官网给出了四种创建JSON文档的方法:

1.1手写方式生成

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

手写方式很简单,但是要注意日期格式:Date Formate

1.2使用集合

集合是key:value数据类型,可以代表json结构.

Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");

1.3使用JACKSON序列化

ElasticSearch已经使用了jackson,可以直接使用它把javabean转为json.

// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse

// generate json
byte[] json = mapper.writeValueAsBytes(yourbeaninstance);

1.4使用ElasticSearch 帮助类

import static org.elasticsearch.common.xcontent.XContentFactory.*;

XContentBuilder builder = jsonBuilder()
    .startObject()
        .field("user", "kimchy")
        .field("postDate", new Date())
        .field("message", "trying out Elasticsearch")
    .endObject()

 String json = builder.string();

二、创建索引

下面的例子把json文档写入所以,索引库名为twitter、类型为tweet,id为1:

import static org.elasticsearch.common.xcontent.XContentFactory.*;

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        .get();

也可以直接传人JSON字符串:

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

IndexResponse response = client.prepareIndex("twitter", "tweet")
        .setSource(json)
        .get();

可以调用response对象的方法获取返回信息:

// 索引名称
String _index = response.getIndex();
// 类型名称
String _type = response.getType();
// 文档id
String _id = response.getId();
// 版本(if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
// 是否被创建is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();

更简单的可以直接System.out.println(response)查看返回信息.

三、java实现

新建一个java项目,导入elasticsearch-2.3.3/lib目录下的jar文件.新建一个Blog类:

public 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  
}

创建java实体类转json工具类:

import java.io.IOException;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

public class JsonUtil {

    // Java实体对象转json对象
    public static String model2Json(Blog blog) {
        String jsonData = null;
        try {
            XContentBuilder jsonBuild = XContentFactory.jsonBuilder();
            jsonBuild.startObject().field("id", blog.getId()).field("title", blog.getTitle())
                    .field("posttime", blog.getPosttime()).field("content",blog.getContent()).endObject();

            jsonData = jsonBuild.string();
            //System.out.println(jsonData);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonData;
    }

}

添加数据,返回一个list:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DataFactory {
    public static DataFactory dataFactory = new DataFactory();

    private DataFactory() {
    }

    public DataFactory getInstance() {
        return dataFactory;
    }

    public static List<String> getInitJsonData() {
        List<String> list = new ArrayList<String>();
        String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别..."));
        String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-06-19", "学习目标 掌握泛型的产生意义..."));
        String data3 = JsonUtil.model2Json(new Blog(3, "SQL基本操作", "2016-06-19", "基本操作:CRUD ..."));
        String data4 = JsonUtil.model2Json(new Blog(4, "Hibernate框架基础", "2016-06-19", "Hibernate框架基础..."));
        String data5 = JsonUtil.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;
    }

}

创建索引、添加数据:

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

import cn.com.bropen.entity.DataFactory;
import static org.elasticsearch.common.xcontent.XContentFactory.*;

public class ElasticSearchHandler {
    public static void main(String[] args) {
        try {
            /* 创建客户端 */
            // client startup
            Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            XContentBuilder builder = jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date())
                    .field("message", "trying out Elasticsearch").endObject();


            List<String> jsonData = DataFactory.getInitJsonData();

            for (int i = 0; i < jsonData.size(); i++) {
                IndexResponse response = client.prepareIndex("blog", "article").setSource(jsonData.get(i)).get();
                if (response.isCreated()) {
                   System.out.println("创建成功!");
                }
            }
            client.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

可以通过head插件查看插入的数据。

Installing elasticsearch & Marvel

2016年8月18日
  • 1.在此处https://www.elastic.co/downloads/elasticsearch下载elasticsearch
  • 2.解压后,运行
    sudo bin/elasticsearch
  • 3.在此处https://www.elastic.co/downloads/kibana下载kibana
  • 4.在elasticsearch/下执行
    sudo bin/plugin install license
    sudo bin/plugin install marvel-agent
  • 5.在kibana目录下执行
    bin/kibana plugin --install elasticsearch/marvel/2.3.5
  • 6.如果以上三条本地安装,则可以先下载到本地,然后
    bin/plugin install file:///path/to/file/license-2.3.5.zip
    bin/plugin install file:///path/to/file/marvel-agent-2.3.5.zip
    bin/kibana plugin --install marvel --url file:///path/to/file/marvel-2.3.5.tar.gz
    
  • 7.执行bin/kibana
  • 8.通过:http://localhost:9200查看elasticsearch;通过http://localhost:5601/查看marvel
  • 9200端口结果:

    {
      "name" : "Bloodstorm",
      "cluster_name" : "elasticsearch",
      "version" : {
        "number" : "2.3.5",
        "build_hash" : "90f439ff60a3c0f497f91663701e64ccd01edbb4",
        "build_timestamp" : "2016-07-27T10:36:52Z",
        "build_snapshot" : false,
        "lucene_version" : "5.5.0"
      },
      "tagline" : "You Know, for Search"
    }

    5601端口:
    屏幕快照 2016-08-18 上午10.03.04
    屏幕快照 2016-08-18 上午10.04.26

Installing Marvel

2016年8月18日

To use Marvel, you need to install two components:

  • An Elasticsearch plugin that collects data from each node in your cluster. This plugin must be installed on every node.
  • A Kibana app that provides the Marvel monitoring UI.

By default, the Marvel plugin stores data in the same Elasticsearch cluster where it is installed. If you are monitoring a production cluster we recommend that you store the Marvel data in a separate monitoring cluster. Sending the Marvel data to a monitoring cluster helps ensure that you can continue to monitor your production cluster if it’s in an unhealthy state.

For basic installation instructions, see Getting Started with Marvel. For information about storing Marvel data in a separate monitoring cluster, see Setting up a Separate Monitoring Cluster.

https://www.elastic.co/guide/en/marvel/current/installing-marvel.html#marvel-prequisites

在Mac OS X使用Elasticsearch

2016年8月18日

要在OS X上使用Elasticsearch,其实很简单,在这里:https://www.elastic.co/downloads/elasticsearch 下载Zip压缩包,并在合适的目录下解压,然后终端下定位到解压后目录下,运行:

sudo bin/elasticsearch 

其实就可以启动Elasticsearch了,可以再安装一下Marvel,这个Elasticsearch的可视化管理和监控工具:

sudo bin/plugin install elasticsearch/marvel/latest

重新启动Elasticsearch:

sudo bin/elasticsearch restart

可以在浏览器下访问:http://localhost:9200/,可以看到返回一个类似于JSON式的结果集。Marvel可以访问这里:http://localhost:9200/_plugin/marvel/,可以看到这个界面做得还挺漂亮的。

最后总结一下Elasticsearch的API,其实可以用Restful形式的API,也可以使用Java API,其Java API的文档在这里:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html。

中文文献:http://es.xiaoleilu.com/010_Intro/10_Installing_ES.html
英文文献:https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html

 

 

http://localhost:9200/_plugin/head/