一、数据从文件导入elasticsearch

1、数据准备:

1、数据文件:test.json
2、索引名称:index
3、数据类型:doc
4、批量操作API:bulk
{"index":{"_index":"index2","_type":"type2","_id":0}}
{"age":10,"name":"jim"}
{"index":{"_index":"index2","_type":"type2","_id":1}}
{"age":16,"name":"tom"}

2、_bulk API导入ES的JSON文件需要满足一定的格式,每条记录之前,需要有文档ID且每一行\n结束

curl -H 'Content-Type: application/x-ndjson'  -s -XPOST localhost:9200/_bulk --data-binary @test.json

如果是在test.json文件中没有指定index名、type、id时:

curl -H 'Content-Type: application/x-ndjson'  -s -XPOST localhost:9200/index2/type2/_bulk --data-binary @test.json
{ "index" : { } }
{"age":16,"name":"tom"}

但是id会自动生成

3、对于普通json文件的导入,可以logstash进行导入:

logstash的安装准备详细过程请查阅:

https://www.cnblogs.com/yfb918/p/10763292.html

json数据准备

[root@master mnt]# cat data.json
{"age":16,"name":"tom"}
{"age":11,"name":"tsd"}

创建配置文件:

[root@master bin]# cat json.conf
input{
file{
path=>"/mnt/data.json"
start_position=>"beginning"
sincedb_path=>"/dev/null"
codec=>json{
charset=>"ISO-8859-1"
}
}
}
output{
elasticsearch{
hosts=>"http://192.168.200.100:9200"
index=>"jsontestlogstash"
document_type=>"doc"
}
stdout{}
}

执行结果:

[root@master bin]# ./logstash -f json.conf
[2019-04-25T10:59:14,803][INFO ][logstash.agent ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[2019-04-25T10:59:16,084][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
{
"name" => "tom",
"age" => 16,
"path" => "/mnt/data.json",
"@timestamp" => 2019-04-25T02:59:16.009Z,
"host" => "master",
"@version" => "1"
}
{
"name" => "tsd",
"age" => 11,
"path" => "/mnt/data.json",
"@timestamp" => 2019-04-25T02:59:16.096Z,
"host" => "master",
"@version" => "1"
}

从结果中可以看到:默认增加了几个字段。那么我们想要这几个默认生成的字段我们应该怎么么办呢,可以如下解决:

在配置文件中使用filter进行过滤:

[root@master bin]# cat json.conf
input{
file{
path=>"/mnt/data.json"
start_position=>"beginning"
sincedb_path=>"/dev/null"
codec=>json{
charset=>"ISO-8859-1"
}
}
}
filter{
mutate {
remove_field => "@timestamp"
remove_field => "@version"
remove_field => "host"
remove_field => "path"
}
}
output{
elasticsearch{
hosts=>"http://192.168.200.100:9200"
index=>"jsontestlogstash"
document_type=>"doc"
}
stdout{}
}

过滤之后的结果:

Logstash学习之路(二)Elasticsearch导入json数据文件的更多相关文章

  1. Solr学习笔记——导入JSON数据

    1.导入JSON数据的方式有两种,一种是在web管理界面中导入,另一种是使用curl命令来导入 curl http://localhost:8983/solr/baikeperson/update/j ...

  2. 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  3. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  4. Qt 学习之路 2(35):文件

    Qt 学习之路 2(35):文件 豆子 2013年1月5日 Qt 学习之路 2 12条评论 文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了 ...

  5. Qt 学习之路 2(17):文件对话框

    Home / Qt 学习之路 2 / Qt 学习之路 2(17):文件对话框 Qt 学习之路 2(17):文件对话框  豆子  2012年9月24日  Qt 学习之路 2  85条评论 在前面的章节中 ...

  6. sqlcmd导入大数据文件

    sqlcmd导入大数据文件 SQLCMD 允许在Windows命令窗中通过命令行提示符运行脚本. 语法如下: sqlcmd  [  { { -U <login id> [ -P <p ...

  7. 阿里云Mysql导入大数据文件

    1.查询数据保存为CSV文件 select * from account into outfile '/root/account.csv' fields terminated by ',' enclo ...

  8. IOS学习之路二十(程序json转换数据的中文字符问题解决)

    ios请求web中的json数据的时候经常出现乱码问题: 例如请求结果可能如下:"\U00e5\U00a5\U00bd\U00e8\U00ae\U00a4" 在网上查到的解决方法是 ...

  9. Redis——学习之路二(初识redis服务器命令)

    上一章我们已经知道了如果启动redis服务器,现在我们来学习一下,以及如何用客户端连接服务器.接下来我们来学习一下查看操作服务器的命令. 服务器命令: 1.info——当前redis服务器信息   s ...

随机推荐

  1. HTTP慢速拒绝服务攻击(Slow HTTP Dos)

    HTTP慢速拒绝服务攻击简介 HTTP慢速攻击是利用HTTP合法机制,以极低的速度往服务器发送HTTP请求,尽量长时间保持连接,不释放,若是达到了Web Server对于并发连接数的上限,同时恶意占用 ...

  2. python——pandas技巧(处理dataframe每个元素,不用for,而用apply)

    用apply处理pandas比用for循环,快了无数倍,测试如下: 我们有一个pandas加载的dataframe如下,features是0和1特征的组合,可惜都是str形式(字符串形式),我们要将其 ...

  3. 优化 Webpack 的构建速度

    1.使用高版本的 Webpack 和 Node.js 2.多进程/多实例构建:HappyPack(不维护了).thread-loader 3.压缩代码 webpack-paralle-uglify-p ...

  4. AcWing&#160;127.&#160;任务

    题目链接 参考y神的思路QWQ 算法:贪心 对于每一个任务: \(y\) 的差异最多能使利润\(w\)浮动\(2 * 100 = 200\)元. \(x\) 差\(1\),则会使利润\(w\)浮动\( ...

  5. VirtualBox5.2.2 安装 CentOS 7

    转自百度经验:https://jingyan.baidu.com/article/4dc4084868a1e4c8d946f133.html?tdsourcetag=s_pctim_aiomsg&am ...

  6. C++异常之二 基本语法

    2. 异常处理的基本语法 下面是一个基本的代码例子,说明 throw.try.catch的基本用法,与 catch 的类型自动匹配: 1 #include <iostream> 2 #in ...

  7. Python最会变魔术的魔术方法,我觉得是它!

    在上篇文章中,我有一个核心的发现:Python 内置类型的特殊方法(含魔术方法与其它方法)由 C 语言独立实现,在 Python 层面不存在调用关系. 但是,文中也提到了一个例外:一个非常神秘的魔术方 ...

  8. mysql单机多实例配置

    Windows上配置多个mysql实例,主要改下配置文件即可,mysql目录如下: my2中主要改两个配置内容 datadir = D:/Program Files/Mysql/mysql-5.7.2 ...

  9. c预处理和宏

    文件的预处理 #include "xxx.h" 1 首先查找当前源文件所在的路径 2 查找工程的头文件搜索路径 #include <xxxx.h> 查找工程的头文件搜索 ...

  10. matplotlib的学习6-annotation的标注

    import matplotlib.pyplot as plt import numpy as np ''' 当图线中某些特殊地方需要标注时,我们可以使用 annotation. matplotlib ...