Doris数据库--导入数据之 stream load Java调FE接口上传文件导入数据
在Java中使用Stream Load导入数据到Doris数据库,你可以通过HTTP请求调用FE(Frontend)接口来完成。以下是一个简单的Java代码示例,展示了如何使用Java的HttpURLConnection
类发送POST请求来进行数据导入。
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class DorisStreamLoad {
private static final String FE_HOST = "http://your_fe_host";
private static final int FE_PORT = 8030;
private static final String DB_NAME = "your_db_name";
private static final String TABLE_NAME = "your_table_name";
private static final String USER = "your_user";
private static final String PWD = "your_password";
public static void loadData(String filePath) throws IOException {
String loadUrl = FE_HOST + ":" + FE_PORT + "/api/" + DB_NAME + "/" + TABLE_NAME + "/_stream_load";
URL url = new URL(loadUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString((USER + ":" + PWD).getBytes()));
// 写入数据文件
try (OutputStream os = connection.getOutputStream(); FileInputStream fis = new FileInputStream(filePath)) {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
// 获取响应码和结果
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
}
} else {
System.out.println("Stream load failed with code: " + responseCode);
}
}
public static void main(String[] args) {
try {
loadData("/path/to/your/data/file.csv");
} catch (IOException e) {
e.printStackTrace();
}
}
}
确保替换FE_HOST
, FE_PORT
, DB_NAME
, TABLE_NAME
, USER
, 和 PWD
为你的Doris环境的实际信息。filePath
是你要导入的数据文件的路径。
这段代码首先构造了用于导入数据的URL,然后创建了一个HttpURLConnection
实例并设置了必要的属性,包括请求方法、输出和基本认证。接着,它打开文件输出流将文件内容写入HTTP
评论已关闭