Stream load之json数据导入
Stream load 是一个同步的导入方式,用户通过发送 HTTP 协议发送请求将本地文件或数据流导入到 Doris 中。Stream load 同步执行导入并返回导入结果。用户可直接通过请求的返回体判断本次导入是否成功。
Stream load 主要适用于导入本地文件,或通过程序导入数据流中的数据。
取消导入
用户无法手动取消 Stream load,Stream load 在超时或者导入错误后会被系统自动取消。
相关系统配置
FE 配置
-
stream_load_default_timeout_second
导入任务的超时时间(以秒为单位),导入任务在设定的 timeout 时间内未完成则会被系统取消,变成 CANCELLED。
默认的 timeout 时间为 600 秒。如果导入的源文件无法在规定时间内完成导入,用户可以在 stream load 请求中设置单独的超时时间。
或者调整 FE 的参数
stream_load_default_timeout_second
来设置全局的默认超时时间。
BE 配置
-
streaming_load_max_mb
Stream load 的最大导入大小,默认为 10G,单位是 MB。如果用户的原始文件超过这个值,则需要调整 BE 的参数
streaming_load_max_mb
。
具体请参考官方文档,这里直接上代码了.
官方文档链接: https://doris.apache.org/zh-CN/administrator-guide/load-data/stream-load-manual.html#%E5%88%9B%E5%BB%BA%E5%AF%BC%E5%85%A5
-
加入pom依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-http</artifactId> <version>5.7.22</version> </dependency>
-
LoadJson.java
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import java.util.UUID; /** * @Author: M.J * @Date: 2022/4/19 0019 14:27 */ public class LoadJson.java { public static void main(String[] args) throws Exception { // be String host = "192.168.31.62"; // be接口 int port = 8030; String database = "xxx"; String table = "xxx"; String user = "root"; String passwd = ""; String label = getUUID(); String s = "[{\"longitude\": \"19\", \"city\": \"北京\", \"latitude\": \"39.916927\"},{\"longitude\": \"20\", \"city\": \"北京\", \"latitude\": \"39.916927\"}]"; HttpResponse execute = HttpRequest.put("http://" + host + ":" + port + "/api/" + database + "/" + table + "/_stream_load") .basicAuth(user, passwd) .header("Expect", "100-continue") .header("label", label) .header("Content-Type", "text/plain; charset=UTF-8") .header("format", "json") // 导入json数组 .header("strip_outer_array", "true") .body(s).setFollowRedirects(true) .execute(); System.out.println(execute); } public static String getUUID() { String uuid = UUID.randomUUID().toString().trim().replaceAll("-", ""); return uuid; } }