-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7d051c
commit 81cc54a
Showing
2 changed files
with
353 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
(ns metabase.driver.scratch | ||
(:require [clojure.java.io :as io] | ||
[clojure.java.jdbc :as jdbc] | ||
[metabase [config :as config]] | ||
[metabase.driver.clickhouse-introspection] | ||
[metabase.driver.clickhouse-nippy] | ||
[metabase.driver.clickhouse-qp] | ||
[metabase.driver.sql-jdbc [common :as sql-jdbc.common] | ||
[connection :as sql-jdbc.conn]] | ||
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute] | ||
[metabase.query-processor.writeback :as qp.writeback] | ||
[metabase.test :as mt]) | ||
(:import [java.io ByteArrayInputStream] | ||
[java.nio.charset StandardCharsets] | ||
[com.clickhouse.data.value ClickHouseArrayValue] | ||
[com.clickhouse.data ClickHouseFormat ClickHouseFile ClickHouseCompression ClickHouseDataStreamFactory | ||
ClickHousePipedOutputStream ClickHouseInputStream] | ||
[com.clickhouse.client ClickHouseNode ClickHouseProtocol ClickHouseCredentials ClickHouseClient])) | ||
|
||
(def server | ||
(.. (ClickHouseNode/builder) | ||
(host "127.0.0.1") | ||
(port ClickHouseProtocol/HTTP) | ||
(database "default") | ||
(credentials (ClickHouseCredentials/fromUserAndPassword "default" "")) | ||
(build))) | ||
|
||
(def file | ||
(ClickHouseFile/of "/Users/callumherries/meta/mb49/t.csv" ClickHouseCompression/NONE ClickHouseFormat/CSV)) | ||
|
||
(def client | ||
(ClickHouseClient/newInstance (into-array ClickHouseProtocol [(.getProtocol server)]))) | ||
|
||
(.. client | ||
(write server) | ||
(set "input_format_csv_skip_first_lines" "1") | ||
(set "format_csv_delimiter" ",") | ||
(table "t") | ||
(data file) | ||
(executeAndWait)) | ||
|
||
(defn- new-input-stream [content] | ||
(ByteArrayInputStream. (.getBytes content StandardCharsets/US_ASCII))) | ||
|
||
(let [input-stream (new-input-stream "1,1\n2,3")] | ||
(.. client | ||
(write server) | ||
(set "input_format_csv_skip_first_lines" "1") | ||
(set "format_csv_delimiter" ",") | ||
(format ClickHouseFormat/CSV) | ||
(table "t") | ||
(data input-stream) | ||
(executeAndWait))) | ||
|
||
(q "SELECT * FROM t") | ||
(e! "DELETE FROM t WHERE 1=1") | ||
|
||
(e! "CREATE TABLE t (one Int32, two Int32) ENGINE = MergeTree ORDER BY ()") | ||
(e! "DROP TABLE IF EXISTS t") | ||
|
||
(comment | ||
(defmethod driver/create-table! :clickhouse | ||
[driver db-id table-name column-definitions & {:keys [primary-key]}] | ||
(let [sql (create-table!-sql driver table-name column-definitions :primary-key primary-key)] | ||
(qp.writeback/execute-write-sql! db-id sql))) | ||
|
||
(def db-id 6) | ||
|
||
(def table-name "t") | ||
|
||
(defn e! [e] | ||
(jdbc/with-db-transaction [conn (sql-jdbc.conn/db->pooled-connection-spec db-id)] | ||
(jdbc/execute! conn e))) | ||
|
||
(defn q [e] | ||
(jdbc/with-db-transaction [conn (sql-jdbc.conn/db->pooled-connection-spec db-id)] | ||
(jdbc/query conn e))) | ||
|
||
(e! "CREATE TABLE t (one Int32, two Int32) ENGINE = MergeTree ORDER BY ()") | ||
(e! "INSERT INTO t FROM INFILE 't.csv' FORMAT CSV") | ||
(q "SELECT * FROM t") | ||
) | ||
|
||
;; ClickHouseNode server = ClickHouseNode.builder() | ||
;; .host( "127.0.0.1" ) | ||
;; .port( ClickHouseProtocol.HTTP ) | ||
;; .database( "mydatabase" ) | ||
;; .credentials( ClickHouseCredentials | ||
;; .fromUserAndPassword( "username", "password" ) ) | ||
;; .build(); | ||
|
||
;; // Create a CSV file reference | ||
|
||
;; ClickHouseFile file = ClickHouseFile.of( | ||
;; "/path/to/file.csv", ClickHouseCompression.NONE, ClickHouseFormat.CSV ); | ||
|
||
;; // Create a client | ||
|
||
;; ClickHouseClient client = ClickHouseClient.newInstance( server.getProtocol() ); | ||
|
||
;; // Specify settings, load data to the specified table and wait for completion | ||
|
||
;; client.write( server ) | ||
;; .set( "input_format_csv_skip_first_lines", "1" ) | ||
;; .set( "format_csv_delimiter", "," ) | ||
;; .table( "mytable" ) | ||
;; .data( file ) | ||
;; .executeAndWait(); | ||
|
||
|
||
;; https://github.com/ClickHouse/clickhouse-java/blob/6a4ffc4ae0b76a6f65a57ee0db3246d45fbc78c1/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Advanced.java#L37 | ||
;; private static ByteArrayInputStream newInputStream (String content) | ||
;; {return new ByteArrayInputStream (content.getBytes (StandardCharsets.US_ASCII)); | ||
;; } | ||
|
||
;; static String exteralTables(String url) throws SQLException { | ||
;; String sql = "select a.name as n1, b.name as n2 from {tt 'table1'} a inner join {tt 'table2'} b on a.id=b.id"; | ||
;; try (Connection conn = getConnection(url); PreparedStatement ps = conn.prepareStatement(sql)) { | ||
;; ps.setObject(1, | ||
;; ClickHouseExternalTable.builder().name("table1").columns("id Int32, name Nullable(String)") | ||
;; .format(ClickHouseFormat.CSV) | ||
;; .content(newInputStream("1,a\n2,b")).build()); | ||
;; ps.setObject(2, | ||
;; ClickHouseExternalTable.builder().name("table2").columns("id Int32, name String") | ||
;; .format(ClickHouseFormat.JSONEachRow) | ||
;; .content(newInputStream("{\"id\":3,\"name\":\"c\"}\n{\"id\":1,\"name\":\"d\"}")).build()); | ||
;; try (ResultSet rs = ps.executeQuery()) { | ||
;; if (!rs.next()) { | ||
;; throw new IllegalStateException("Should have at least one record"); | ||
;; } | ||
|
||
;; // n1=a, n2=d | ||
;; return String.format("n1=%s, n2=%s", rs.getString(1), rs.getString(2)); | ||
;; } | ||
;; } | ||
;; } | ||
|
||
;; static long insert(ClickHouseNode server, String table) throws ClickHouseException { | ||
;; try (ClickHouseClient client = ClickHouseClient.newInstance(server.getProtocol())) { | ||
;; ClickHouseRequest.Mutation request = client.read(server).write().table(table) | ||
;; .format(ClickHouseFormat.RowBinary); | ||
;; ClickHouseConfig config = request.getConfig(); | ||
;; CompletableFuture<ClickHouseResponse> future; | ||
;; // back-pressuring is not supported, you can adjust the first two arguments | ||
;; try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance() | ||
;; .createPipedOutputStream(config, (Runnable) null)) { | ||
;; // in async mode, which is default, execution happens in a worker thread | ||
;; future = request.data(stream.getInputStream()).execute(); | ||
|
||
;; // writing happens in main thread | ||
;; for (int i = 0; i < 10_000; i++) { | ||
;; BinaryStreamUtils.writeString(stream, String.valueOf(i % 16)); | ||
;; BinaryStreamUtils.writeNonNull(stream); | ||
;; BinaryStreamUtils.writeString(stream, UUID.randomUUID().toString()); | ||
;; } | ||
;; } | ||
|
||
;; // response should be always closed | ||
;; try (ClickHouseResponse response = future.get()) { | ||
;; ClickHouseResponseSummary summary = response.getSummary(); | ||
;; return summary.getWrittenRows(); | ||
;; } | ||
;; } catch (InterruptedException e) { | ||
;; Thread.currentThread().interrupt(); | ||
;; throw ClickHouseException.forCancellation(e, server); | ||
;; } catch (ExecutionException | IOException e) { | ||
;; throw ClickHouseException.of(e, server); | ||
;; } | ||
;; } | ||
|
||
|
||
(defn create-file [file-path content] | ||
(with-open [wrtr (io/writer file-path)] | ||
(.write wrtr content))) | ||
|
||
(create-file "f.csv" "1,one\n2,two") | ||
|
||
(mt/db) | ||
|
||
(def spec (sql-jdbc.conn/connection-details->spec :clickhouse (:details (mt/db)))) | ||
|
||
(sql-jdbc.execute/do-with-connection-with-options | ||
:clickhouse spec nil | ||
(fn [^java.sql.Connection conn] | ||
(jdbc/execute! {:connection conn} "SELECT 1"))) | ||
|
||
(let [props (doto (java.util.Properties.) | ||
(.setProperty "localFile" "true")) | ||
f (io/file "f.csv") | ||
spec (sql-jdbc.conn/connection-details->spec :clickhouse (:details (mt/db)))] | ||
#_(when (.exists f) | ||
(io/delete-file f)) | ||
|
||
(qp.writeback/execute-write-sql! (mt/id) (str "drop table if exists test_load_infile_with_params;")) | ||
(qp.writeback/execute-write-sql! (mt/id) (str "create table test_load_infile_with_params(n Int32, s String) engine=MergeTree ORDER BY ()")) | ||
;; (qp.writeback/execute-write-sql! (mt/id) "insert into test_load_infile_with_params from infile 'f.csv' FORMAT CSV") | ||
(with-open [conn (.getConnection (metabase.driver.sql-jdbc.execute/do-with-resolved-connection-data-source :clickhouse spec {})) | ||
stmt (.prepareStatement conn "insert into test_load_infile_with_params from infile ? format CSV")] | ||
(.setString stmt 1 (.getName f)) | ||
(.addBatch stmt) | ||
(.setString stmt 1 (.getName f)) | ||
(.addBatch stmt) | ||
(.setString stmt 1 (str (.getName f) "!")) | ||
(.addBatch stmt) | ||
(.executeBatch stmt)) | ||
#_(sql-jdbc.execute/do-with-connection-with-options | ||
:clickhouse spec nil | ||
(fn [^java.sql.Connection conn] | ||
(with-open [stmt (.prepareStatement conn "insert into test_load_infile_with_params from infile ? format CSV")] | ||
(.setString stmt 1 (.getName f)) | ||
(.addBatch stmt) | ||
(.setString stmt 1 (.getName f)) | ||
(.addBatch stmt) | ||
(.setString stmt 1 (str (.getName f) "!")) | ||
(.addBatch stmt) | ||
(.executeBatch stmt))))) | ||
|
||
;; Use non-csv formatted data | ||
;; // 'format RowBinary' is the hint to use streaming mode, you may use different | ||
;; // format like JSONEachRow as needed | ||
;; sql = String.format("insert into %s format RowBinary", TABLE_NAME); | ||
;; try (PreparedStatement ps = conn.prepareStatement(sql)) { | ||
;; // it's streaming so there's only one parameter(could be one of String, byte[], | ||
;; // InputStream, File, ClickHouseWriter), and you don't have to process batch by | ||
;; // batch | ||
;; ps.setObject(1, new ClickHouseWriter() { | ||
;; @Override | ||
;; public void write(ClickHouseOutputStream output) throws IOException { | ||
;; // this will be executed in a separate thread | ||
;; for (int i = 0; i < 1_000_000; i++) { | ||
;; output.writeUnicodeString("a-" + i); | ||
;; output.writeBoolean(false); // non-null | ||
;; output.writeUnicodeString("b-" + i); | ||
;; } | ||
;; } | ||
;; }); | ||
;; ps.executeUpdate(); | ||
;; } | ||
;; ByteArrayInputStream | ||
;; try (InputStream inputStream = Files.newInputStream(file); | ||
;; ClickHouseStatement statement = clickHouseConnection.createStatement()) { | ||
;; statement.write() | ||
;; .table(tableName)) | ||
;; .option("format_csv_delimiter", ";") | ||
;; .data(inputStream, ClickHouseFormat.CSV) | ||
;; .send(); | ||
;; } | ||
|
||
|
||
|
||
;;;; EITHER THIS | ||
|
||
;; // 4. fastest(close to Java client) but requires manual serialization and it's | ||
;; // NOT portable(as it's limited to ClickHouse) | ||
;; // 'format RowBinary' is the hint to use streaming mode, you may use different | ||
;; // format like JSONEachRow as needed | ||
;; sql = String.format("insert into %s format RowBinary", TABLE_NAME); | ||
;; try (PreparedStatement ps = conn.prepareStatement(sql)) { | ||
;; // it's streaming so there's only one parameter(could be one of String, byte[], | ||
;; // InputStream, File, ClickHouseWriter), and you don't have to process batch by | ||
;; // batch | ||
;; ps.setObject(1, new ClickHouseWriter() { | ||
;; @Override | ||
;; public void write(ClickHouseOutputStream output) throws IOException { | ||
;; // this will be executed in a separate thread | ||
;; for (int i = 0; i < 1_000_000; i++) { | ||
;; output.writeUnicodeString("a-" + i); | ||
;; output.writeBoolean(false); // non-null | ||
;; output.writeUnicodeString("b-" + i); | ||
;; } | ||
;; } | ||
;; }); | ||
;; ps.executeUpdate(); | ||
;; } | ||
|
||
;; return count; | ||
;; } |