往ElasticSearch中导入.csv数据
很多网上提供的数据集往往是csv文件格式的数据,为了能够直接向ElasticSearch中添加数据集,除了支持json文件意外以外,ES也可以支持csv文件格式。
注意: logstash的版本应当与ElasticSearch一致。
在logstash安装目录下创建xxx.conf文件(名字随意)
input
{
file{
path => ["/文件的绝对路径/Reviews.csv"]
start_position => "beginning"
}
}
filter{
csv{
separator => ","
columns => ["Id","ProductId","UserId","ProfileName","HelpfulnessNumerator","HelpfulnessDenominator","Score","Time","Summary","Text"]
}
mutate{
convert => {
"Id" => "integer"
"ProductId" => "string"
"ProfileName" => "string"
"HelpfulnessNumerator" => "integer"
"HelpfulnessDenominator" => "integer"
"score" => "integer"
"Time" => "integer"
"Summary" => "string"
"Text" => "string"
}
}
}
output{
elasticsearch{
hosts => ["localhost:9200"]
index => "food_reviews"
}
}
input模块
file的意思是采用文件输入的方式。
path就直接填写文件路径就好了,这个没什么好说的。
start_position="beginning"的意思是从文件的头部开始扫描。
关于input-file模块还有很多设置,具体可以参考官网文档:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html
filter模块
csv部分主要是对csv文件做一些基本的设定。
separator是指csv文件数据之间的分隔符,默认为“,”,可以改变。
columns设置的是每一列数据的名称(按照csv文件中的顺序来),如果不设置的话,默认为设置成column1、column2的形式。
同样,官方文档还有更多的设置形式:https://www.elastic.co/guide/en/logstash/7.0/plugins-filters-csv.html
mutate部分,顾名思义,就是对数据进行一定程度上的转变。
convert的目的是可以对原本csv文件中的数据类型进行改变,7.0版本支持的有integer、interger_eu、float、float_eu、string、boolean。
注意:
千万不要写错了!!!
我就是这里写错了自查N多遍都没有发现!
以及,报错了不要马上去百度,
一定要看看自己的原因是什么,
大多数情况下都是能看得懂的。
output模块
这一部分就比较简单了,有接触过怎么使用logstash的应该或多或少都知道一些。好了,还是官方文档送上:https://www.elastic.co/guide/en/logstash/7.0/plugins-outputs-elasticsearch.html
运行程序,导入数据
首先,ElasticSearch和Kibana都打开,确保两者已经成功运行,如果不知道的话建议先从头学起。
./bin/logstash -f xxx.conf
如果是这种情况的话,恭喜你成功了。至于需要花多长时间,取决于你的数据大小,反正500MB以下速度是很快的了。
网页打开 http://localhost:9200/_cat/indices?v ,如果能找到你的索引那就是上传完毕了。之后Kibana新建一个index pattern就可以可视化操作啦。