ClickHouse创建账号和连接测试

在之前搭建ClickHouse的时候,把账户相关的去掉了,所以登录和连接的时候是不需要账号密码的,但是实际项目中,肯定是需要根据需要创建账号。

一,创建账号

1,进入到 /etc/clickhouse-server, 编辑users.xml

找到users这个标签对, 这个里面子标签就是用户名。比如clickhouse的默认用户default,默认是没有密码的,如果需要密码,可以在<password></password>标签里输入密码。

修改之后重启服务

systemctl restart clickhouse-server.service

下面这个access_management开启之后,就可以使用default账号进行创建账号和授权给其他账号。

2,连接clickhouse,执行sql添加账号,并授权

clickhouse-client -m --password

再输入上面这个default账号的密码
-- 创建用户clickhouse

CREATE USER clickhouse HOST IP '0.0.0.0' IDENTIFIED WITH sha256_password BY 'clickhouse';

其中IP '0.0.0.0'表示允许使用clickhouse用户从任何一台主机上登录clickhouse。

-- 给用户授权

GRANT ALL ON *.* TO clickhouse WITH GRANT OPTION;

-- 查看用户权限

show create user clickhouse;

-- 查看授权

show grants for clickhouse;

3, 另外一种方式创建用户

直接在上面那个users.xml里添加用户也是可以的。

在<users></users>标签中间添加

<clickhouse>

<password>clickhouse</password>

<clickhouse>

注: 这里是创建了一个账号clickhouse,密码是clickhouse。 你可以根据你的需要去创建账号。

完成保存,之后重启一下服务

二,连接ClickHouse

1,使用clickhouse账户登录

clickhouse-client -m --user clickhouse --password clickhouse

2,查看数据库

show databases;

3, 创建数据库,并切换数据库

登录之后,默认使用的schema是default

--创建数据库

CREATE DATABASE IF NOT EXISTS account;

--切换数据库

use account;

4,创建表

CREATE TABLE student

(

`StudentID` UInt32,

`StudentName` String,

`Address` String,

`Grade` Int8,

`Age` Int8,

`Gender` UInt8,

`StartTime` DateTime

)

ENGINE = CollapsingMergeTree(Grade)

PARTITION BY toYYYYMM(StartTime)

ORDER BY (StudentID);

5,查询表

show tables

6,删除表

drop table student1;

语法和Mysql相似。

三,代码中连接ClickHouse

1,导入pom依赖

XML 复制代码
<!-- ClickHouse Start -->
<dependency>
    <groupId>ru.yandex.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.1.40</version>
</dependency>
<!-- ClickHouse End -->

2,加载驱动,插入数据,查询数据

java 复制代码
package com.x.bigdata;

import java.sql.*;

public class TestConnectionClickHouse {
    private static Connection connection = null;
    static {
        try {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");// 驱动包
            String url = "jdbc:clickhouse://x.x.x.x:8123/account";// url路径
            String user = "clickhouse";// 账号
            String password = "clickhouse";// 密码
            connection = DriverManager.getConnection(url, user, password);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        insert();
        select();
        if (connection != null){
            connection.close();
        }
    }

    /**
     * 插入数据
     * @throws SQLException
     */
    public static void insert() throws SQLException {
        PreparedStatement prepareStatement = connection.prepareStatement("insert into account.student values (?,?,?,?,?,?,?)");
        prepareStatement.setInt(1,1002);
        prepareStatement.setString(2,"小猫猫");
        prepareStatement.setString(3,"中国-贵州-贵阳");
        prepareStatement.setInt(4,1);// 年级
        prepareStatement.setInt(5,21);
        prepareStatement.setInt(6,1); // 1:男,0:女
        prepareStatement.setDate(7,new Date(2020,12,22));
        prepareStatement.executeUpdate();
        if (prepareStatement != null){
            prepareStatement.close();
        }
    }

    /**
     * 查询数据
     * @throws SQLException
     */
    public static void select() throws SQLException {
        PreparedStatement statement = connection.prepareStatement("select * from account.student");
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()){
            int studentID = resultSet.getInt(1);
            String studentName = resultSet.getString(2);
            System.out.println(studentID + "," + studentName);
        }
    }
}

后续就是集成到SpringBoot项目中。。。

相关推荐
l1t3 天前
PostgreSQL pg_clickhouse插件的安装和使用
数据库·clickhouse·postgresql·插件
honder试试4 天前
Springboot实现Clickhouse连接池的配置和接口查询
spring boot·后端·clickhouse
Mr_wilson_liu4 天前
通过DBeaver22.0.5 连接数据库ck(clickhouse)、pg(postgres)
数据库·clickhouse
波波仔866 天前
clickhouse表存储引擎
clickhouse·表存储引擎
波波仔866 天前
clickhouse存储和分区
clickhouse·排序·分区
波波仔866 天前
clickhouse insert与update区别
clickhouse·insert·update
波波仔866 天前
clickhouse简介
数据库·clickhouse
深色風信子6 天前
ClickHouse 快速入门
clickhouse·列式存储
波波仔866 天前
行存储与列存储的区别
数据库·clickhouse·行存储·列储存
吃喝不愁霸王餐APP开发者6 天前
霸王餐用户行为埋点:Kafka Connect+ClickHouse实时OLAP分析
分布式·clickhouse·kafka