Skip to content

Commit 0fd1e8b

Browse files
authored
Merge pull request #355 from youngsofun/fix
add example.
2 parents 5c62500 + 46bef15 commit 0fd1e8b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

config/checkstyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
<module name="RedundantImport"/>
2020
<module name="IllegalImport"/>
2121
</module>
22-
</module>
22+
</module>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.databend.jdbc.examples;
2+
3+
import com.databend.jdbc.DatabendConnection;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
import java.sql.*;
10+
11+
public class LoadToTableFromStream {
12+
public static void main(String[] args) throws SQLException {
13+
uploadAndCopy();
14+
}
15+
16+
static void uploadAndCopy() throws SQLException {
17+
// assuming the file and stage1 and table1 already exist
18+
String filePath = "data.csv";
19+
20+
String stageName = "stage1";
21+
String prefix = "data_set1";
22+
String fileName = "data1";
23+
String path = "@stage1/data_set1/data1";
24+
25+
String url = "jdbc:databend://localhost:8000";
26+
try(Connection conn = DriverManager.getConnection(url, "databend", "databend");
27+
Statement stmt = conn.createStatement()) {
28+
29+
// upload
30+
InputStream inputStream = Files.newInputStream(Paths.get(filePath));
31+
long fileSize = Files.size(Paths.get("data.csv"));
32+
DatabendConnection databendConnection = conn.unwrap(DatabendConnection.class);
33+
databendConnection.uploadStream(inputStream, stageName, prefix, fileName, fileSize, false);
34+
35+
// https://docs.databend.com/sql/sql-functions/table-functions/list-stage
36+
String sql = String.format("list %s", path);
37+
try(ResultSet rs = stmt.executeQuery(sql)) {
38+
while (rs.next()) {
39+
rs.getString(1);
40+
}
41+
}
42+
43+
// copy into table
44+
// https://docs.databend.com/sql/sql-commands/dml/dml-copy-into-table
45+
sql = String.format("copy into table1 from %s file_format =(type=csv) purge=true", path);
46+
try(ResultSet rs = stmt.executeQuery(sql)) {
47+
while (rs.next()) {
48+
rs.getString("File");
49+
}
50+
}
51+
} catch (IOException e) {
52+
throw new RuntimeException(e);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)