无法使用 curl 从 json 配置文件中读取数据
Couldn't read data from json config file using curl
我正在尝试设置一个新连接器以使用 REST API 读取文件。我没有在命令行中传递文件配置,而是试图通过文件传递它。但是,在读取此文件时,出现错误。此文件位于 /usr/share 目录中。
命令:
curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/usr/share/fileConfigSource.json
Warning: Couldn't read data from file "/usr/share/fileConfigSource.json", this
Warning: makes an empty POST.
{"error_code":500,"message":null}
fileConfigSource.json :
{
"name": "file_source",
"config": {
"connector.class": "org.apache.kafka.connect.file.FileStreamSourceConnector",
"tasks.max": "1",
"file": "/usr/share/details.txt",
"topics": "KTest",
"key.converter": "org.apache.kafka.connect.storage.StringConverter",
"value.converter": "org.apache.kafka.connect.storage.StringConverter"
}
}
原因如下:
Couldn't read data from file
原因可能包括:
文件不存在。例如:
➜ cat /this/doesnot/exist
cat: /this/doesnot/exist: No such file or directory
➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/this/doesnot/exist
Warning: Couldn't read data from file "/this/doesnot/exist", this makes an
Warning: empty POST.
{"error_code":500,"message":null}%
文件确实存在但是执行curl
命令的用户没有权限访问它
➜ ls -l /tmp/foo.json
--w-r--r-- 1 rmoff wheel 390 2 Jun 17:50 /tmp/foo.json
➜ cat /tmp/foo.json
cat: /tmp/foo.json: Permission denied
➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/tmp/foo.json
Warning: Couldn't read data from file "/tmp/foo.json", this makes an empty
Warning: POST.
{"error_code":500,"message":null}%
如果文件位于正确的位置并且可以访问,您将得到这样的响应:
➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/tmp/foo.json
{"error_code":400,"message":"Connector configuration is invalid and contains the following 1 error(s):\nMissing required configuration \"topic\" which has no default value.\nYou can also find the above list of errors at the endpoint `/connector-plugins/{connectorType}/config/validate`"}%
修复该错误更改
"topics": "KTest",
为
"topic": "KTest",
这就成功了:
➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/tmp/foo.json
{"name":"file_source","config":{"connector.class":"org.apache.kafka.connect.file.FileStreamSourceConnector","tasks.max":"1","file":"/usr/share/details.txt","topic":"KTest","key.converter":"org.apache.kafka.connect.storage.StringConverter","value.converter":"org.apache.kafka.connect.storage.StringConverter","name":"file_source"},"tasks":[],"type":"source"}%
我正在尝试设置一个新连接器以使用 REST API 读取文件。我没有在命令行中传递文件配置,而是试图通过文件传递它。但是,在读取此文件时,出现错误。此文件位于 /usr/share 目录中。
命令:
curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/usr/share/fileConfigSource.json
Warning: Couldn't read data from file "/usr/share/fileConfigSource.json", this
Warning: makes an empty POST.
{"error_code":500,"message":null}
fileConfigSource.json :
{
"name": "file_source",
"config": {
"connector.class": "org.apache.kafka.connect.file.FileStreamSourceConnector",
"tasks.max": "1",
"file": "/usr/share/details.txt",
"topics": "KTest",
"key.converter": "org.apache.kafka.connect.storage.StringConverter",
"value.converter": "org.apache.kafka.connect.storage.StringConverter"
}
}
原因如下:
Couldn't read data from file
原因可能包括:
文件不存在。例如:
➜ cat /this/doesnot/exist cat: /this/doesnot/exist: No such file or directory ➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/this/doesnot/exist Warning: Couldn't read data from file "/this/doesnot/exist", this makes an Warning: empty POST. {"error_code":500,"message":null}%
文件确实存在但是执行
curl
命令的用户没有权限访问它➜ ls -l /tmp/foo.json --w-r--r-- 1 rmoff wheel 390 2 Jun 17:50 /tmp/foo.json ➜ cat /tmp/foo.json cat: /tmp/foo.json: Permission denied ➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/tmp/foo.json Warning: Couldn't read data from file "/tmp/foo.json", this makes an empty Warning: POST. {"error_code":500,"message":null}%
如果文件位于正确的位置并且可以访问,您将得到这样的响应:
➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/tmp/foo.json
{"error_code":400,"message":"Connector configuration is invalid and contains the following 1 error(s):\nMissing required configuration \"topic\" which has no default value.\nYou can also find the above list of errors at the endpoint `/connector-plugins/{connectorType}/config/validate`"}%
修复该错误更改
"topics": "KTest",
为
"topic": "KTest",
这就成功了:
➜ curl -X POST "http://localhost:8083/connectors" -H "Content-type:application/json" --data @/tmp/foo.json
{"name":"file_source","config":{"connector.class":"org.apache.kafka.connect.file.FileStreamSourceConnector","tasks.max":"1","file":"/usr/share/details.txt","topic":"KTest","key.converter":"org.apache.kafka.connect.storage.StringConverter","value.converter":"org.apache.kafka.connect.storage.StringConverter","name":"file_source"},"tasks":[],"type":"source"}%