Skip to content

Commit 21f4c3f

Browse files
committed
add example.
1 parent 61ad8a9 commit 21f4c3f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// optional list
36+
String sql = String.format("list %s", path);
37+
try(ResultSet rs = stmt.executeQuery(sql)) {
38+
while (rs.next()) {
39+
System.out.println(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+
System.out.println( rs.getString("ROWS_LOADED") + ", " +
49+
rs.getInt("ROWS_LOADED") + ", " +
50+
rs.getInt("ERRORS_SEEN"));
51+
}
52+
}
53+
} catch (IOException e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)