Apache Zeppelin 的 Elasticsearch 解释器

概述

Elasticsearch 是一个高度可扩展的开源全文搜索和分析引擎。 它允许您快速且近乎实时地存储、搜索和分析大量数据。 它通常用作支持具有复杂搜索功能和需求的应用程序的底层引擎/技术。

配置

属性 默认值 描述
elasticsearch.cluster.name elasticsearch 集群名称
elasticsearch.host localhost 集群中某个节点的主机
elasticsearch.port 9300 连接端口 (重要:取决于客户端类型,transport 或 http)
elasticsearch.client.type transport Elasticsearch 的客户端类型(transport 或 http)(重要:端口取决于此值)
elasticsearch.basicauth.username 基本认证的用户名 (http)
elasticsearch.basicauth.password 基本认证的密码 (http)
elasticsearch.result.size 10 搜索查询结果集的大小

解释器配置

注意 #1: 您可以添加更多属性来配置 Elasticsearch 客户端。

注意 #2: 如果您使用Shield,可以添加一个名为shield.user的属性,其值包含用户名和密码(格式:username:password)。有关Shield配置的更多详细信息,请参阅Shield参考指南。不要忘记将shield客户端jar复制到解释器目录(ZEPPELIN_HOME/interpreters/elasticsearch)。

启用Elasticsearch解释器

在笔记本中,要启用Elasticsearch解释器,请点击齿轮图标并选择Elasticsearch

使用Elasticsearch解释器

在一个段落中,使用%elasticsearch来选择Elasticsearch解释器,然后输入所有命令。 要获取可用命令的列表,请使用help

%elasticsearch
help

Elasticsearch interpreter:
General format: <command> /<indices>/<types>/<id> <option> <JSON>
  - indices: list of indices separated by commas (depends on the command)
  - types: list of document types separated by commas (depends on the command)
Commands:
  - search /indices/types <query>
    . indices and types can be omitted (at least, you have to provide '/')
    . a query is either a JSON-formatted query, nor a lucene query
  - size <value>
    . defines the size of the result set (default value is in the config)
    . if used, this command must be declared before a search command
  - count /indices/types <query>
    . same comments as for the search
  - get /index/type/id
  - delete /index/type/id
  - index /index/type/id <json-formatted document>
    . the id can be omitted, elasticsearch will generate one

提示: 使用 ( Ctrl + . ) 进行自动补全。

获取

使用get命令,您可以通过id查找文档。结果是一个JSON文档。

%elasticsearch
get /index/type/id

示例: Elasticsearch - Get

搜索

使用search命令,您可以向Elasticsearch发送搜索查询。查询有两种格式:

  • 你可以提供一个JSON格式的查询,这正是你在使用Elasticsearch的REST API时所提供的。
  • 你也可以提供query_string的内容。
    • 这是类似以下查询的快捷方式:{ "query": { "query_string": { "query": "__HERE YOUR QUERY__", "analyze_wildcard": true } } }
    • 有关此类查询内容的更多详细信息,请参见Elasticsearch查询字符串语法
%elasticsearch
search /index1,index2,.../type1,type2,...  <JSON document containing the query or query_string elements>

如果你想修改结果集的大小,你可以在搜索命令之前添加一行设置大小的代码。

%elasticsearch
size 50
search /index1,index2,.../type1,type2,...  <JSON document containing the query or query_string elements>

搜索查询也可以包含聚合。如果至少有一个聚合,则显示第一个聚合的结果,否则,您将获得搜索结果。

示例:

  • 使用JSON查询:

    %elasticsearch
    search / { "query": { "match_all": { } } }
    
    %elasticsearch
    search /logs { "query": { "query_string": { "query": "request.method:GET AND status:200" } } }
    
    %elasticsearch
    search /logs { "aggs": {
    "content_length_stats": {
      "extended_stats": {
        "field": "content_length"
      }
    }
    } }
    
  • 使用 query_string 元素:

    %elasticsearch
    search /logs request.method:GET AND status:200
    
    %elasticsearch
    search /logs (404 AND (POST OR DELETE))
    

重要:Elasticsearch 中的文档是一个 JSON 文档,因此它是分层的,而不是像 SQL 表中的一行那样扁平。 对于 Elastic 解释器,搜索查询的结果是扁平的。

假设我们有一个JSON文档:

{
  "date": "2015-12-08T21:03:13.588Z",
  "request": {
    "method": "GET",
    "url": "/zeppelin/4cd001cd-c517-4fa9-b8e5-a06b8f4056c4",
    "headers": [ "Accept: *.*", "Host: apache.org"]
  },
  "status": "403",
  "content_length": 1234
}

数据将被展平如下:

内容长度 日期 请求头[0] 请求头[1] 请求方法 请求URL 状态
1234 2015-12-08T21:03:13.588Z 接受: *.* 主机: apache.org GET /zeppelin/4cd001cd-c517-4fa9-b8e5-a06b8f4056c4 403

示例:

  • 使用包含结果的表格: Elasticsearch - Search - table

  • 你也可以使用预定义的图表: Elasticsearch - Search - diagram

  • 使用JSON查询: Elasticsearch - 使用查询搜索

  • 使用包含fields参数的JSON查询(用于过滤响应中的字段):在这种情况下,响应中的所有字段值都是数组,因此,在展平结果后,所有字段名称的格式为field_name[x] Elasticsearch - 使用查询和fields参数进行搜索

  • 使用查询字符串: Elasticsearch - 使用查询字符串搜索

  • 使用包含多值度量聚合的查询: Elasticsearch - 使用聚合进行搜索(多值度量)

  • 使用包含多桶聚合的查询: Elasticsearch - 使用聚合进行搜索(多桶)

计数

使用count命令,您可以计算某些索引和类型中可用的文档数量。您还可以提供一个查询。

%elasticsearch
count /index1,index2,.../type1,type2,... <JSON document containing the query OR a query string>

示例:

  • 无查询: Elasticsearch - 计数

  • 使用查询: Elasticsearch - 带查询的计数

索引

使用index命令,您可以在Elasticsearch中插入/更新文档。

%elasticsearch
index /index/type/id <JSON document>

%elasticsearch
index /index/type <JSON document>

删除

使用delete命令,您可以删除一个文档。

%elasticsearch
delete /index/type/id

应用Zeppelin动态表单

您可以在查询中利用Zeppelin动态表单。您可以使用文本输入选择表单参数化功能。

%elasticsearch
size ${limit=10}
search /index/type { "query": { "match_all": { } } }