3

如何在Linux安装Elasticsearch

 2 years ago
source link: https://www.myfreax.com/how-to-install-elasticsearch-on-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

更新于 2021/12/14 |  创建于 2021/12/14

如何在Linux安装Elasticsearch

Elasticsearch是当今最流行的日志分析平台——ELK Stack(Elasticsearch、Logstash和Kibana)的核心。Elasticsearch的角色非常重要,以至于它已成为ELK 本身名称的同义词。Elasticsearch主要用于搜索和日志分析,是当今最流行的数据库系统之一。

在本Elasticsearch教程将为新用户说明如何使用Elasticsearch的必备知识和工具。它包括安装说明、初始索引和数据处理的说明。

Elasticsearch(有时称为 ES)最初于 2010 年发布,是一种基于Apache Lucene的现代搜索和分析引擎。Elasticsearch完全开源并使用Java构建,是一个NoSQL数据库。这意味着它以非结构化的方式存储数据,并且您不能使用 SQL 来查询它。

这个Elasticsearch教程也可以被认为是一个NoSQL教程。然而,与大多数NoSQL数据库不同,Elasticsearch非常注重搜索功能和特性,从 ES 获取数据的最简单方法是使用Elasticsearch API进行搜索。

在数据分析的上下文中,Elasticsearch与ELK Stack、Logstash和Kibana中的其他组件一起使用,起到数据索引和存储的作用。

安装Elasticsearch

Elasticsearch的要求很简单:此外,您需要确保您的操作系统是否支持Elastic,否则您可能会遇到奇怪和不可预测的问题。完成后,您可以从安装 Elasticsearch开始。

您可以将 Elasticsearch作为独立发行版下载或使用aptyum存储库进行安装。我们将上一个Ubuntu 20.04机器上使用apt安装Elasticsearch。

首先,您需要添加Elastic的签名密钥,以便您可以验证下载的软件包(如果您已经安装了Elastic的软件包,请跳过此步骤):

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

对于Debian,我们需要安装apt-transport-https软件包:

sudo apt-get install apt-transport-https

下一步是将存储库定义添加到您的系统中:

echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

当前最新版本是7.16,这个版本号7.x可能会随着时间发生变化,建议安装最后发行版本。

接下来要做的是安装:

sudo apt-get update
sudo apt-get install elasticsearch

配置Elasticsearch

Elasticsearch配置是使用配置文件完成的,该文件的位置取决于您的操作系统。在此文件中,您可以配置常规设置(例如节点名称)以及网络设置(例如主机和端口)、数据存储位置、内存、日志文件等。

对于开发和测试目的,默认设置就足够了,但建议您在投入生产之前对应该手动定义的设置进行一些研究。

例如,尤其是在云上安装Elasticsearch时,将Elasticsearch绑定到私有IP或本地主机是一个很好的最佳实践,使用你喜欢的编辑器,这里推荐使用Vim编辑器

sudo vim /etc/elasticsearch/elasticsearch.yml
network.host: "localhost"
http.port:9200
/etc/elasticsearch/elasticsearch.yml

运行Elasticsearch

Elasticsearch安装后不会自动运行,您需要手动启动它。如何运行Elasticsearch将取决于您的系统。在大多数基于Linux和Unix的系统上,您可以使用以下命令:

sudo systemctl start elasticsearch

如果一切正常,可以使用curl或您的浏览器打开http://localhost:9200,您应该会看到如下输出:

{
  "name" : "33QdmXw",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "mTkBe_AlSZGbX-vDIe_vZQ",
  "version" : {
    "number" : "6.1.2",
    "build_hash" : "5b1fea5",
    "build_date" : "2018-01-10T02:35:59.208Z",
    "build_snapshot" : false,
    "lucene_version" : "7.1.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

要调试Elasticsearch,请查看Elasticsearch日志文件/var/log/elasticsearch/

Elasticsearch自动启动

sudo systemctl enable elasticsearch

创建Elasticsearch索引

创建索引是将数据添加到Elasticsearch的过程。这是因为当您将数据提供给 Elasticsearch时,数据会被放入Apache Lucene索引中。这是有原因的,因为 Elasticsearch使用Lucene索引来存储和检索其数据。尽管您不需要对Lucene有很多了解,但当您开始认真使用Elasticsearch时,了解它的工作原理确实很有帮助。

Elasticsearch的行为类似于REST API,因此您可以使用POSTPUT方法向Elasticsearch添加数据。您可以使用PUT,当你知道或想指定id的数据,或者如果你想Elasticsearch自动生成id ,请使用POST 。当在命令行中创建Elasticsearch的数据时,我们将会使用CURL发起请求

curl -X POST 'localhost:9200/logs/my_app' -H 'Content-Type: application/json' -d'
{
	"timestamp": "2018-01-24 12:34:56",
	"message": "User logged in",
	"user_id": 4,
	"admin": false
}'
post请求
curl -X PUT 'localhost:9200/app/users/4' -H 'Content-Type: application/json' -d '
{
  "id": 4,
  "username": "john",
  "last_login": "2018-01-25 12:34:56"
}
put请求
{"_index":"logs","_type":"my_app","_id":"ZsWdJ2EBir6MIbMWSMyF","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}{"_index":"app","_type":"users","_id":"4","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
curl返回结果

文档的数据作为JSON对象发送。您可能想知道我们如何在不定义数据结构的情况下索引数据。好吧,使用 Elasticsearch,就像使用任何其他NoSQL数据库一样,无需事先定义数据的结构。不过,为了确保最佳性能,您可以根据数据类型定义 Elasticsearch映射。稍后会详细介绍。

如果您使用Filebeat,Metricbeat或Logstash的这些软件它将会自动创建索引。

要查看您的 Elasticsearch索引列表,请使用:

curl -X GET 'localhost:9200/_cat/indices?v&pretty'
health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   logstash-2018.01.23 y_-PguqyQ02qOqKiO6mkfA   5   1      17279            0      9.9mb          9.9mb
yellow open   app                 GhzBirb-TKSUFLCZTCy-xg   5   1          1            0      5.2kb          5.2kb
yellow open   .kibana             Vne6TTWgTVeAHCSgSboa7Q   1   1          2            0      8.8kb          8.8kb
yellow open   logs                T9E6EdbMSxa8S_B7SDabTA   5   1          1            0      5.7kb          5.7kb
结果

本例中的列表包括我们上面创建的索引、一个Kibana索引和一个由Logstash管道创建的索引。

Elasticsearch搜索查询

将数据索引到Elasticsearch后,您就可以开始搜索或者分析它了。您可以执行的最简单查询是获取单个项目。

我们使用GET 请求方法调用Elasticsearch REST API:

curl -X GET 'localhost:9200/app/users/4?pretty'
{ 
  “_index”:“app”,
  “ 
  _type”:“users” 
  ,“_id”:“4”,“_version”:1,
  “found”:true,
  “ _source ”:{ 
    “id”:4,
    “用户名” " : "john", 
    "last_login" : "2018-01-25 12:34:56" 
  } 
}
结果

下划线开头的字段都是结果的元字段。_source对象是已建立索引的原始文档。

使用搜索API搜索数据

我们还可以使用GET请求调用_search Api来进行搜索:

curl -XGET 'localhost:9200/_search?q=logged' 
{"took":173,"timed_out":false,"_shards":{"total":16,"successful":16,"skiped":0, "failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"logs","_type":"my_app","_id": "ZsWdJ2EBir6MIbMWSMyF","_score":0.2876821,"_source": 
{ 
    "timestamp": " 2018-01-24 12:34:56 ", 
    "message": "用户登录", 
    "user_id": 4, 
    "admin ": false 
} 
}]}}

删除Elasticsearch数据

从Elasticsearch中删除文档就像在Elasticsearch中输入数据一样简单。这次使用的HTTP方法是DELETE

curl -X DELETE 'localhost:9200/app/users/4?pretty'
{
  "_index" : "app",
  "_type" : "users",
  "_id" : "4",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
结果

本教程帮助初学者使用Elasticsearch,因此仅提供Elasticsearch中CRUD操作的基本步骤。Elasticsearch是一个搜索引擎,因此要了解其搜索功能具有极大的难度。你也可以浏览官方Elasticsearch文档,深入了解其魅力。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK