• Java Stream Load写入数据到Doris


    Java Stream Load写入数据到Doris demo 如下:

    import org.apache.commons.codec.binary.Base64;
    import org.apache.http.HttpHeaders;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.entity.FileEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.DefaultRedirectStrategy;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    
    
    public class StreamLoadExample {
        private final static String HOST = "192.168.56.104";
        private final static int PORT = 8040;
        private final static String DATABASE = "test"; // 数据库名
        private final static String TABLE = "order_info_example"; // 数据表名
        private final static String USER = "test"; // Doris 用户名
        private final static String PASSWD = "password123"; // Doris 密码
        private final static String LOAD_FILE_NAME = "src/main/resources/data.txt"; // 本地文件路径
    
        private final static String loadUrl = String.format("http://%s:%s/api/%s/%s/_stream_load",
                HOST, PORT, DATABASE, TABLE);
    
        private final static HttpClientBuilder httpClientBuilder = HttpClients
                .custom()
                .setRedirectStrategy(new DefaultRedirectStrategy() {
                    @Override
                    protected boolean isRedirectable(String method) {
                        // If the connection target is FE, you need to handle 307 redirect.
                        return true;
                    }
                });
    
        public void load(File file) throws Exception {
            try (CloseableHttpClient client = httpClientBuilder.build()) {
                HttpPut put = new HttpPut(loadUrl);
                put.setHeader(HttpHeaders.EXPECT, "100-continue");
                put.setHeader(HttpHeaders.AUTHORIZATION, basicAuthHeader(USER, PASSWD));
    
                // 设置stream load 的label, 用于避免重复导入数据
                put.setHeader("label","label-example");
                //设置字段分隔符
                put.setHeader("column_separator",",");
    
                // Set the import file.
                // StringEntity can also be used here to transfer arbitrary data.
                FileEntity entity = new FileEntity(file);
                put.setEntity(entity);
    
                try (CloseableHttpResponse response = client.execute(put)) {
                    String loadResult = "";
                    if (response.getEntity() != null) {
                        loadResult = EntityUtils.toString(response.getEntity());
                    }
    
                    final int statusCode = response.getStatusLine().getStatusCode();
                    if (statusCode != 200) {
                        throw new IOException(
                                String.format("Stream load failed. status: %s load result: %s", statusCode, loadResult));
                    }
    
                    System.out.println("Get load result: " + loadResult);
                }
            }
        }
    
        private String basicAuthHeader(String username, String password) {
            final String tobeEncode = username + ":" + password;
            byte[] encoded = Base64.encodeBase64(tobeEncode.getBytes(StandardCharsets.UTF_8));
            return "Basic " + new String(encoded);
        }
    
        public static void main(String[] args) throws Exception{
            StreamLoadExample loader = new StreamLoadExample();
            File file = new File(LOAD_FILE_NAME);
            loader.load(file);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
  • 相关阅读:
    python-pytorch 实现seq2seq+luong general concat attention笔记1.0.10
    【Kafka系列】(一)Kafka入门
    idea导入maven web项目,增加tomcat服务器
    Xcode 异常图片导致ipa包增大问题
    【STM32】IAP升级03关闭总中断,检测栈顶指针
    error: reference to ‘byte‘ is ambiguous使用QtCharts报的错误
    记录:CentOS安装配置MySQL8
    电容如何能做升压?(电荷泵的工作原理及特性)
    APT级全面免杀拿Shell
    vue学习-16vue的history模式与hash模式
  • 原文地址:https://blog.csdn.net/wangleigiser/article/details/133300789