Stream load doris

代码如下:经测试有效

package Demo;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.net.util.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.StringEntity;
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.protocol.RequestContent;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class DorisStreamLoader {

private  final String user;
private final String password;

private final String loadUrl;

public static void main(String[] args) throws Exception {
    /*ArrayList<JSONObject> list = new ArrayList<>();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("waybillNumber","a123456");
    list.add(jsonObject );
	JSONObject jsonObject2 = new JSONObject();
    jsonObject2.put("waybillNumber","789");
    list.add(jsonObject2 );
    String jsonData = list.toString();
    System.out.println(jsonData);
    String s = streamLoader.loadJson(jsonData);
*/

    DorisStreamLoader streamLoader = new DorisStreamLoader(host, port, "database", "table", "user", "password");
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("waybillNumber","a123456");
    String jsonData = jsonObject.toString();
    String s = streamLoader.loadJson(jsonData);
    JSONObject jsonObject1 = JSONObject.parseObject(s);
    String status = jsonObject1.getString("Status");

    if(status.equalsIgnoreCase("Success") || status.equalsIgnoreCase("Publish Timeout")){
        System.out.println("load Success");
    }else {
        s = streamLoader.loadJson(jsonData);
        if(! status.equalsIgnoreCase("Success") || status.equalsIgnoreCase("Publish Timeout") ){
            System.out.println("load Fail"+jsonData.toString());
        }
    }
}

public DorisStreamLoader(String host, int port, String database, String table, String user, String password) {
    this.user = user;
    this.password = password;
    this.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 deal with 307 redirect。
                return true;
            }
        }).addInterceptorLast(new RequestContent(true));


public String loadJson(String jsonData) throws Exception {
    String loadResult = "";
    try (CloseableHttpClient client = httpClientBuilder.build()) {
        HttpPut put = new HttpPut(loadUrl);
       /* put.removeHeaders(HttpHeaders.CONTENT_LENGTH);
        put.removeHeaders(HttpHeaders.TRANSFER_ENCODING);*/

        put.setHeader(HttpHeaders.EXPECT, "100-continue");
        put.setHeader(HttpHeaders.AUTHORIZATION, basicAuthHeader(user, password));

        put.setHeader("label", UUID.randomUUID().toString());
        put.setHeader("column_separator", ",");
        put.setHeader("format", "json");
         // strip_outer_array: 布尔类型,为true表示json数据以数组对象开始且将数组对象中进行展平,默认值是false
         //这个功能要求 Array 中的每行数据的字段顺序完全一致。Doris 仅会根据第一行的字段顺序做解析,然后以下标的形式访问之后的数据。
        put.setHeader("strip_outer_array", "true");


        StringEntity entity = new StringEntity(jsonData,"UTF-8");
        put.setEntity(entity);

        try (CloseableHttpResponse response = client.execute(put)) {

            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);
        }
    }
    return loadResult;
}

/**
 * 封装认证信息
 *
 * @param username String
 * @param password String
 * @return String
 */
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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值