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 {
    /*JSONObject jsonObject = new JSONObject();
    jsonObject.put("waybillNumber","a123456");
    jsonObject.put("222aa","bb");
    String jsonData = jsonObject.toString();
    System.out.println(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");


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

你可能感兴趣的:(spring,boot,sql)