|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse; |
| 17 | + |
| 18 | +import com.clickhouse.jdbc.ClickHouseConnection; |
| 19 | +import com.clickhouse.jdbc.ClickHouseStatement; |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | + |
| 23 | +import java.sql.PreparedStatement; |
| 24 | +import java.sql.ResultSet; |
| 25 | +import java.sql.SQLException; |
| 26 | +import java.util.Collections; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author intr3p1d |
| 30 | + */ |
| 31 | +public class ClickHouseITBase { |
| 32 | + private final Logger logger = LogManager.getLogger(getClass()); |
| 33 | + static final String TABLE_NAME = "jdbc_example_basic"; |
| 34 | + |
| 35 | + public ClickHouseITBase() { |
| 36 | + } |
| 37 | + |
| 38 | + public int dropAndCreateTable(ClickHouseConnection conn) throws SQLException { |
| 39 | + try (ClickHouseStatement stmt = conn.createStatement()) { |
| 40 | + // multi-statement query is supported by default |
| 41 | + // session will be created automatically during execution |
| 42 | + stmt.execute(String.format( |
| 43 | + "drop table if exists %1$s; create table %1$s(a String, b Nullable(String)) engine=Memory", |
| 44 | + TABLE_NAME)); |
| 45 | + return stmt.getUpdateCount(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public int query(ClickHouseConnection conn) throws SQLException { |
| 50 | + String sql = "select * from " + TABLE_NAME; |
| 51 | + try (ClickHouseStatement stmt = conn.createStatement()) { |
| 52 | + // set max_result_rows = 3, result_overflow_mode = 'break' |
| 53 | + // or simply discard rows after the first 3 in read-only mode |
| 54 | + stmt.setMaxRows(3); |
| 55 | + int count = 0; |
| 56 | + try (ResultSet rs = stmt.executeQuery(sql)) { |
| 57 | + while (rs.next()) { |
| 58 | + count++; |
| 59 | + } |
| 60 | + } |
| 61 | + return count; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void insertByteArray(ClickHouseConnection conn) throws SQLException { |
| 66 | + try (ClickHouseStatement s = conn.createStatement()) { |
| 67 | + s.execute("drop table if exists t_map;" |
| 68 | + + "CREATE TABLE t_map" |
| 69 | + + "(" |
| 70 | + + " `audit_seq` Int64 CODEC(Delta(8), LZ4)," |
| 71 | + + "`timestamp` Int64 CODEC(Delta(8), LZ4)," |
| 72 | + + "`event_type` LowCardinality(String)," |
| 73 | + + "`event_subtype` LowCardinality(String)," |
| 74 | + + "`actor_type` LowCardinality(String)," |
| 75 | + + "`actor_id` String," |
| 76 | + + "`actor_tenant_id` LowCardinality(String)," |
| 77 | + + "`actor_tenant_name` String," |
| 78 | + + "`actor_firstname` String," |
| 79 | + + "`actor_lastname` String," |
| 80 | + + "`resource_type` LowCardinality(String)," |
| 81 | + + "`resource_id` String," |
| 82 | + + "`resource_container` LowCardinality(String)," |
| 83 | + + "`resource_path` String," |
| 84 | + + "`origin_ip` String," |
| 85 | + + "`origin_app_name` LowCardinality(String)," |
| 86 | + + "`origin_app_instance` String," |
| 87 | + + "`description` String," |
| 88 | + + "`attributes` Map(String, String)" |
| 89 | + + ")" |
| 90 | + + "ENGINE = MergeTree " |
| 91 | + + "ORDER BY (resource_container, event_type, event_subtype) " |
| 92 | + + "SETTINGS index_granularity = 8192"); |
| 93 | + try (PreparedStatement stmt = conn.prepareStatement( |
| 94 | + "INSERT INTO t_map SETTINGS async_insert=1,wait_for_async_insert=1 VALUES (8481365034795008,1673349039830,'operation-9','a','service', 'bc3e47b8-2b34-4c1a-9004-123656fa0000','b', 'c', 'service-56','d', 'object','e', 'my-value-62', 'mypath', 'some.hostname.address.com', 'app-9', 'instance-6','x', ?)")) { |
| 95 | + stmt.setObject(1, Collections.singletonMap("key1", "value1")); |
| 96 | + stmt.execute(); |
| 97 | + |
| 98 | + try (ResultSet rs = s.executeQuery("select attributes from t_map")) { |
| 99 | + logger.info(rs.next()); |
| 100 | + logger.info(rs.getObject(1)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments