摘要:在终端当中使用 curl
加载本地 ndjson
数据到 ElasticSearch
在 ES 中使用 curl 添加 json 文件数据
1 2 3
| curl -H 'Content-Type: application/x-ndjson' -X POST '127.0.0.1:9200/shakespeare/_bulk?pretty' --data-binary @shakespeare.json
|
命令行解释:
1 2 3 4 5
| - `-H 'Content-Type: application/x-ndjson'` - `-X POST` - `127.0.0.1:9200/shakespeare/_bulk?pretty` - `--data-binary` - `@shakespeare.json`
|
关于 ndjson 文件
图一:普通的 json 文件:
1
| [{ "name": "1" }, { "name": "2" }, { "name": "3" }]
|
图二:特别的 ndjson 文件:
1 2 3 4
| {"name":"1"} {"name":"2"} {"name":"3"}
|
其中图一是常见的 json 格式,而且整个 json 对象是一个列表:元素由逗号分隔,再由方括号闭合。图二则是一种称为 ndJSON 的格式,由换行符(0x0A)分隔每个 json 对象,最外面也没有闭合字符对。ndjson 的 mime 类型是application/x-ndjson
。
需要注意的是,图一和图二并不等同,就是说,图一和图二不仅使用了不同的序列化格式,数据所表达的含义也是不同的。图一只表达了一个对象:一个列表,图二则表达了 3 个对象:3 个“data”字典。这个区别是 json 和 ndjson 的本质区别。
ndjson(New-line Delimited JSON)是一个比较新的标准,本身超简单,就是一个.ndjson 文件中,每行都是一个传统 json 对象,当然每个 json 对象中要去掉原本用于格式化的换行符,而 json 的 string 中本身就不允许出现换行符(取而代之的是\n),所以 ndjson 在语法上基本不会出现歧义。
curl 文档
安装
请戳这里下载,根据使用的平台,下载适用的版本即可。
想全面了解可以下载一个说明文件:everything-curl
常用命令
1 2 3 4 5 6
| # 查看版本 $ curl -V # 用于打印更多信息,包括发送的请求信息 $ curl -v www.baidu.com # 将操作痕迹保存到dump文件中 $ curl --trace dump www.baidu.com
|
下载文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| # 保存文件,指定文件名 $ curl -o fileName http://sample.com/test.jpg # 保存文件,未指定文件名,使用默认的 $ curl -O http://img.sccnn.com/bimg/339/15020.jpg # 保存文件,为指定文件名,可以同时下载多个文件,文件名从12到22 $ curl -O http://img.sccnn.com/bimg/339/150[20-30].jpg # 保存文件,为指定文件名,可以同时下载多个文件,文件名从12到22,使用步长3 $ curl -O http://img.sccnn.com/bimg/339/150[20-30:3].jpg # 按照指定格式返回文件名 $ curl -o file#1.jpg http://img.sccnn.com/bimg/339/150[20-30:3].jpg # 其他演示,文件名使用正规表达式 $ curl -O http://example.com/section[a-z].html $ curl -O http://example.com/{one,two,three,alpha,beta}.html
|
使用配置文件
当参数比较多的时候,可以指定配置文件
1 2
| # 通过配置文件 $ curl -K config.txt www.baidu.com
|
配置文件 config.txt 的内容
1 2 3 4
| # this is a comment, we ask to follow redirects --location # ask to do a HEAD request --head
|
FTP 类型
获取 FTP 数据的时候,需要指定类型
1 2 3 4 5 6 7 8
| # ftp登录 $ curl -u user:pass ftp://sample.com # 使用AscII进行传输 curl "ftp://example.com/foo;type=A" # 使用二进制进行传输 curl "ftp://example.com/foo;type=I" # 传输目录 curl "ftp://example.com/foo;type=D"
|
http 操作
- Post 的提交方法
1 2 3 4 5 6 7 8 9 10 11
| # post的提交方法 $ curl -d "loginId=139&password=111111" http://posttest.com $ curl -d loginId=139 -d password=111111 http://posttest.com # 参数写入文件中 $ curl -d @param.txt http://posttest.com # 设置类型,默认使用application/x-www-form-urlencoded $ curl -d '{json}' -H 'Content-Type: application/json' https://example.com # Post二进制文件 $ curl --data-binary @filename http://example.com/ # 进行URL编码 $ curl --data-urlencode "test1=lxm&abc" http://localhost:8080/post
|
参数文件的书写方法
1 2
| test1=这是第一个参数& test2=这是第二个参数
|
- 特殊动作的提交方法
通过 - X 指定方法
1
| curl http://localhost:8080/put -X PUT -d "test1=param1"
|
官方文档说明