ru.yandex.clickhouse.settings.ClickHouseProperties Java Examples

The following examples show how to use ru.yandex.clickhouse.settings.ClickHouseProperties. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClickHousePreparedStatementImpl.java    From clickhouse-jdbc with Apache License 2.0 6 votes vote down vote up
public ClickHousePreparedStatementImpl(CloseableHttpClient client, ClickHouseConnection connection,
                                       ClickHouseProperties properties, String sql, TimeZone serverTimeZone,
                                       int resultSetType) throws SQLException
{
    super(client, connection, properties, resultSetType);
    this.sql = sql;
    PreparedStatementParser parser = PreparedStatementParser.parse(sql);
    this.parameterList = parser.getParameters();
    this.insertBatchMode = parser.isValuesMode();
    this.sqlParts = parser.getParts();
    int numParams = countNonConstantParams();
    this.binds = new ClickHousePreparedStatementParameter[numParams];
    dateTimeTimeZone = serverTimeZone;
    if (properties.isUseServerTimeZoneForDates()) {
        dateTimeZone = serverTimeZone;
    } else {
        dateTimeZone = TimeZone.getDefault();
    }
}
 
Example #2
Source File: ErrorsTest.java    From clickhouse-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorDecompression() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    properties.setCompress(true);
    DataSource dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    Connection connection = dataSource.getConnection();

    connection.createStatement().execute("DROP TABLE IF EXISTS test.table_not_exists");

    PreparedStatement statement = connection.prepareStatement("INSERT INTO test.table_not_exists (d, s) VALUES (?, ?)");

    statement.setDate(1, new Date(System.currentTimeMillis()));
    statement.setInt(2, 1);
    try {
        statement.executeBatch();
    } catch (Exception e) {
        Assert.assertTrue(getClickhouseException(e).getMessage().startsWith("ClickHouse exception, code: 60, host: localhost, port: 8123; Code: 60, e.displayText() = DB::Exception: Table test.table_not_exists doesn't exist."));
        return;
    }
    Assert.assertTrue(false, "didn' find correct error");
}
 
Example #3
Source File: ClickHouseStatementImplTest.java    From clickhouse-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testResultSetWithExtremes() throws SQLException {
    ClickHouseProperties properties = new ClickHouseProperties();
    properties.setExtremes(true);
    ClickHouseDataSource dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    Connection connection = dataSource.getConnection();

    try {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("select 1");
        StringBuilder sb = new StringBuilder();
        while (rs.next()) {
            sb.append(rs.getString(1)).append("\n");
        }

        Assert.assertEquals(sb.toString(), "1\n");
    } finally {
        connection.close();
    }
}
 
Example #4
Source File: ClickHouseConnectionImpl.java    From clickhouse-jdbc with Apache License 2.0 6 votes vote down vote up
private void initTimeZone(ClickHouseProperties properties) {
    if (properties.isUseServerTimeZone() && !Strings.isNullOrEmpty(properties.getUseTimeZone())) {
        throw new IllegalArgumentException(String.format("only one of %s or %s must be enabled", ClickHouseConnectionSettings.USE_SERVER_TIME_ZONE.getKey(), ClickHouseConnectionSettings.USE_TIME_ZONE.getKey()));
    }
    if (!properties.isUseServerTimeZone() && Strings.isNullOrEmpty(properties.getUseTimeZone())) {
        throw new IllegalArgumentException(String.format("one of %s or %s must be enabled", ClickHouseConnectionSettings.USE_SERVER_TIME_ZONE.getKey(), ClickHouseConnectionSettings.USE_TIME_ZONE.getKey()));
    }
    if (properties.isUseServerTimeZone()) {
        ResultSet rs = null;
        try {
            timezone = TimeZone.getTimeZone("UTC"); // just for next query
            rs = createStatement().executeQuery("select timezone()");
            rs.next();
            String timeZoneName = rs.getString(1);
            timezone = TimeZone.getTimeZone(timeZoneName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            StreamUtils.close(rs);
        }
    } else if (!Strings.isNullOrEmpty(properties.getUseTimeZone())) {
        timezone = TimeZone.getTimeZone(properties.getUseTimeZone());
    }
}
 
Example #5
Source File: ClickHouseStatementTest.java    From clickhouse-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testCredentials() throws SQLException, URISyntaxException {
    ClickHouseProperties properties = new ClickHouseProperties(new Properties());
    ClickHouseProperties withCredentials = properties.withCredentials("test_user", "test_password");
    assertTrue(withCredentials != properties);
    assertNull(properties.getUser());
    assertNull(properties.getPassword());
    assertEquals(withCredentials.getUser(), "test_user");
    assertEquals(withCredentials.getPassword(), "test_password");

    ClickHouseStatementImpl statement = new ClickHouseStatementImpl(
            HttpClientBuilder.create().build(),null, withCredentials, ResultSet.TYPE_FORWARD_ONLY
            );

    URI uri = statement.buildRequestUri(null, null, null, null, false);
    String query = uri.getQuery();
    assertTrue(query.contains("password=test_password"));
    assertTrue(query.contains("user=test_user"));
}
 
Example #6
Source File: ClickHouseStatementTest.java    From clickhouse-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdditionalRequestParams() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    ClickHouseStatementImpl statement = new ClickHouseStatementImpl(
            HttpClientBuilder.create().build(),
            null,
            properties,
            ResultSet.TYPE_FORWARD_ONLY
    );

    URI uri = statement.buildRequestUri(
            null,
            null,
            null,
            ImmutableMap.of("cache_namespace", "aaaa"),
            false
    );
    String query = uri.getQuery();
    assertTrue(query.contains("cache_namespace=aaaa"), "cache_namespace param is missing in URL");
}
 
Example #7
Source File: ColumnsInfoServlet.java    From clickhouse-jdbc-bridge with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    try (Connection connection = manager.get(req.getParameter("connection_string")); Statement sth = connection.createStatement()) {
        String schema = req.getParameter("schema");
        String table = req.getParameter("table");

        String quote = connection.getMetaData().getIdentifierQuoteString();
        String schemaAndTable = Stream.of(schema, table)
                .filter(s -> !StringUtil.isBlank(s))
                .map(s -> quote + s + quote)
                .collect(Collectors.joining("."));

        String queryRewrite = "SELECT * FROM " + schemaAndTable + " WHERE 1 = 0";
        log.info("Inferring schema by query {}", queryRewrite);

        ResultSet resultset = sth.executeQuery(queryRewrite);
        String ddl = converter.getColumnsDDL(resultset.getMetaData());
        resp.setContentType("application/octet-stream");
        ClickHouseRowBinaryStream stream = new ClickHouseRowBinaryStream(resp.getOutputStream(), null, new ClickHouseProperties());
        stream.writeString(ddl);
    } catch (Exception err) {
        log.error(err.getMessage(), err);
        resp.sendError(HttpStatus.INTERNAL_SERVER_ERROR_500, err.getMessage());
    }
}
 
Example #8
Source File: ClickhouseJdbcUrlParserTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePathAndDb() throws Exception {
    ClickHouseProperties props = new ClickHouseProperties();
    ClickHouseProperties chProps = ClickhouseJdbcUrlParser.parse(
        "jdbc:clickhouse://foo.yandex:1337/db?database=dbname", props.asProperties());
    Assert.assertEquals(chProps.getDatabase(), "db");
    Assert.assertEquals(chProps.getPath(), "/");
}
 
Example #9
Source File: ClickhouseJdbcUrlParserTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePathDefaultDb2() throws Exception {
    ClickHouseProperties props = new ClickHouseProperties();
    props.setPath("/path");
    props.setUsePathAsDb(false);
    ClickHouseProperties chProps = ClickhouseJdbcUrlParser.parse(
        "jdbc:clickhouse://foo.yandex:1337", props.asProperties());
    Assert.assertEquals(chProps.getDatabase(), "default");
    Assert.assertEquals(chProps.getPath(), "/"); //uri takes priority
}
 
Example #10
Source File: ClickhouseJdbcUrlParserTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePathAndDb2() throws Exception {
    ClickHouseProperties props = new ClickHouseProperties();
    props.setUsePathAsDb(false);
    ClickHouseProperties chProps = ClickhouseJdbcUrlParser.parse(
        "jdbc:clickhouse://foo.yandex:1337/db?database=dbname", props.asProperties());
    Assert.assertEquals(chProps.getDatabase(), "dbname");
    Assert.assertEquals(chProps.getPath(), "/db");
}
 
Example #11
Source File: ClickHousePreparedStatementTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetDateOtherTimeZone() throws Exception {
    ClickHousePreparedStatement s = createStatement(
        TimeZone.getTimeZone("Asia/Tokyo"),
        new ClickHouseProperties());
    s.setDate(1, new Date(1557168043000L));
    assertParamMatches(s, "'2019-05-06'");
}
 
Example #12
Source File: ClickHousePreparedStatementTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #13
Source File: ClickHousePreparedStatementTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetDateOtherTimeZoneServerTime() throws Exception {
    ClickHouseProperties props = new ClickHouseProperties();
    props.setUseServerTimeZoneForDates(true);
    ClickHousePreparedStatement s = createStatement(
        TimeZone.getTimeZone("Asia/Tokyo"),
        props);
    s.setDate(1, new Date(1557168043000L));
    assertParamMatches(s, "'2019-05-07'");
}
 
Example #14
Source File: ClickHousePreparedStatementTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetTimeNormalOtherTimeZone() throws Exception {
    ClickHousePreparedStatement s = createStatement(
        TimeZone.getTimeZone("America/Los_Angeles"),
        new ClickHouseProperties());
    s.setTime(1, new Time(1557168043000L));
    assertParamMatches(s, "'2019-05-06 11:40:43'");
}
 
Example #15
Source File: ClickHousePreparedStatementTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetTimestampNormalOtherTimeZone() throws Exception {
    ClickHousePreparedStatement s = createStatement(
        TimeZone.getTimeZone("America/Los_Angeles"),
        new ClickHouseProperties());
    s.setTimestamp(1, new Timestamp(1557168043000L));
    assertParamMatches(s, "'2019-05-06 11:40:43'");
}
 
Example #16
Source File: ClickHousePreparedStatementTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
private static ClickHousePreparedStatement createStatement(TimeZone timezone,
    ClickHouseProperties props) throws Exception
{
    return new ClickHousePreparedStatementImpl(
        Mockito.mock(CloseableHttpClient.class),
        Mockito.mock(ClickHouseConnection.class),
        props,
        "INSERT INTO foo (bar) VALUES (?)",
        timezone,
        ResultSet.TYPE_FORWARD_ONLY);
}
 
Example #17
Source File: ClickhouseJdbcUrlParserTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePathDefaultDb() throws Exception {
    ClickHouseProperties props = new ClickHouseProperties();
    props.setPath("/path");
    ClickHouseProperties chProps = ClickhouseJdbcUrlParser.parse(
        "jdbc:clickhouse://foo.yandex:1337/", props.asProperties());
    Assert.assertEquals(chProps.getDatabase(), "default");
    Assert.assertEquals(chProps.getPath(), "/path");
}
 
Example #18
Source File: BalancedClickhouseDataSourceTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisableConnection() throws Exception {
    BalancedClickhouseDataSource badDatasource = new BalancedClickhouseDataSource("jdbc:clickhouse://not.existed.url:8123", new ClickHouseProperties());
    badDatasource.actualize();
    try {
        Connection connection = badDatasource.getConnection();
        fail();
    } catch (Exception e) {
        // There is no enabled connections
    }
}
 
Example #19
Source File: ErrorsTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ClickHouseException.class)
public void testTableNotExists() throws SQLException {
    ClickHouseProperties properties = new ClickHouseProperties();
    DataSource dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    Connection connection = dataSource.getConnection();
    Statement statement = connection.createStatement();
    statement.execute("select * from table_not_exists");
}
 
Example #20
Source File: ErrorsTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrongUser() {
    ClickHouseProperties properties = new ClickHouseProperties();
    properties.setUser("not_existing");
    DataSource dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    try {
        Connection connection = dataSource.getConnection();
    } catch (Exception e) {
        Assert.assertEquals((getClickhouseException(e)).getErrorCode(), 516);
        return;
    }
    Assert.assertTrue(false, "didn' find correct error");
}
 
Example #21
Source File: BatchInsertsTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    ClickHouseDataSource dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
    dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setTimeZone(TimeZone.getDefault());
}
 
Example #22
Source File: CSVStreamTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #23
Source File: ClickhouseLZ4StreamTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    properties.setDecompress(true);
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #24
Source File: TSVStreamTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #25
Source File: WriterTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    properties.setDecompress(true);
    properties.setCompress(true);
    connection = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties).getConnection();
    statement = connection.createStatement();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
    connection.createStatement().execute("DROP TABLE IF EXISTS test.writer");
    connection.createStatement().execute("CREATE TABLE test.writer (id Int32, name String) ENGINE = Log");
    connection.createStatement().execute("TRUNCATE TABLE test.writer");
}
 
Example #26
Source File: ClickHouseDatabaseMetadataTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #27
Source File: TimeZoneTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseDataSource datasourceServerTz = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", new ClickHouseProperties());
    connectionServerTz = datasourceServerTz.getConnection();
    TimeZone serverTimeZone = connectionServerTz.getTimeZone();
    ClickHouseProperties properties = new ClickHouseProperties();
    properties.setUseServerTimeZone(false);
    int serverTimeZoneOffsetHours = (int) TimeUnit.MILLISECONDS.toHours(serverTimeZone.getOffset(currentTime));
    int manualTimeZoneOffsetHours = serverTimeZoneOffsetHours - 1;
    properties.setUseTimeZone("GMT" + (manualTimeZoneOffsetHours > 0 ? "+" : "")  + manualTimeZoneOffsetHours + ":00");
    ClickHouseDataSource dataSourceManualTz = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connectionManualTz = dataSourceManualTz.getConnection();

    connectionServerTz.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #28
Source File: StreamSQLTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #29
Source File: NativeStreamTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}
 
Example #30
Source File: RowBinaryStreamTest.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setUp() throws Exception {
    ClickHouseProperties properties = new ClickHouseProperties();
    dataSource = new ClickHouseDataSource("jdbc:clickhouse://localhost:8123", properties);
    connection = dataSource.getConnection();
    connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS test");
}