Windows安装ElasticSearch与kibana
文章目录
- ElasticSearch安装
- kibana安装
- 示例程序
Elasticsearch的下载链接:https://www.elastic.co/downloads/elasticsearch
kibana的下载链接:https://www.elastic.co/cn/downloads/kibana
ElasticSearch安装
- 进入
conf\
目录下,修改如下两个配置项:xpack.security.enabled
与xpack.security.http.ssl
,都改为false
,最后效果如下:
- 解压后进入
bin\
目录,运行elasticsearch.bat
,等一段时间访问网站:http://localhost:9200/
,就可以看到成功的json:
kibana安装
kibanan安装包下载后解压,等ElasticSearch启动后,运行kibana的启动文件:bin\kibana.bat
等ElasticSearch启动后,启动kibana:http://localhost:5601/app/home#/
,就可以看到如下界面:
点击【Get started by adding integrations】中的【Try sample data】,在弹出页面后点开【Other sample data sets】,就可以下载官方提供的示例数据:
示例程序
下面是用python连接Elasticsearch并查询记录的程序,首先需要安装:pip install elasticsearch
,然后下载完上面的官方数据后,就可以拿其中一个数据集(kibana_sample_data_flights)测试代码:
from elasticsearch import Elasticsearch
# 连接到本地的 Elasticsearch 服务
es = Elasticsearch(hosts=["http://localhost:9200"])
# 检查连接是否成功
if not es.ping():
print("Elasticsearch无法连接")
# 查询并打印所有索引
response = es.cat.indices(format="json")
for _index_detail in response.body:
if not _index_detail['index'].startswith("."):
print("Elasticsearch索引: ", _index_detail['index'])
index_name = 'kibana_sample_data_flights'
response = es.search(index=index_name, size=10) # 查询10条
for _hit in response["hits"]["hits"]:
_value = _hit['_source']
print("FlightNum:", _value["FlightNum"], " DestCountry:", _value['DestCountry'])
最后得到如下结果:
Elasticsearch索引: kibana_sample_data_flights
Elasticsearch索引: kibana_sample_data_ecommerce
FlightNum: 9HY9SWR DestCountry: AU
FlightNum: X98CCZO DestCountry: IT
FlightNum: UFK2WIZ DestCountry: IT
FlightNum: EAYQW69 DestCountry: IT
FlightNum: 58U013N DestCountry: CN
FlightNum: XEJ78I2 DestCountry: IT
FlightNum: EVARI8I DestCountry: CH
FlightNum: 1IRBW25 DestCountry: CA
FlightNum: M05KE88 DestCountry: IN
FlightNum: SNI3M1Z DestCountry: IT