背景:公司最近業務數據量上去了,需要做一個漏斗分析功能,實時性要求較高,mysql已經已經不在適用,做了個大數據技術棧選型調研后,決定使用StarRocks
StarRocks官網:StarRocks | A High-Performance Analytical Database
前置準備:
1、3臺CentOS虛擬機(2c4g),確保機器間互相可以通信(192.168.0.94、192.168.0.95、192.168.0.245)
2、下載源碼?StarRocks-3.4.2-centos-amd64.tar.gz(我使用的版本 3.4.2),上傳到虛擬機里
下載地址:Download StarRocks Free | StarRocks
3、3臺CentOS虛擬機安裝了 Java 11
sudo yum install java-11-openjdk
java -version
tips:可以先在一臺虛擬機上做好準備,然后復制虛擬機改下ip配置
4、下載一個mysql客戶端,我用的是HeidiSQL,按自己喜歡的來就可以
官方文檔的前期準備可以看一遍:前期準備 | StarRocks
安裝部署:
這里操作的是 94機器(1FE+1BE)
1、在根目錄下創建data文件夾,將StarRocks-3.4.2-centos-amd64.tar.gz文件放在里data下(可選)
2、解壓StarRocks-3.4.2-centos-amd64.tar.gz
3、設置軟連接(可選)
ln -s StarRocks-3.4.2-centos-amd64 starrocks
?4、配置FE節點信息
vi /data/starrocks/fe/conf/fe.conf
動了四個配置:
放開了LOG_DIR、meta_dir的注釋,修改jvm內存為1024(默認是8096)
確保此路徑存在并且擁有寫入權限?
priority_networks修改為本機ip?
完整配置:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.#####################################################################
## The uppercase properties are read and exported by bin/start_fe.sh.
## To see all Frontend configurations,
## see fe/fe-core/src/main/java/com/starrocks/common/Config.java# the output dir of stderr/stdout/gc
# 這里改了
LOG_DIR = ${STARROCKS_HOME}/logDATE = "$(date +%Y%m%d-%H%M%S)"
JAVA_OPTS="-Dlog4j2.formatMsgNoLookups=true -Xmx1024m -XX:+UseG1GC -Xlog:gc*:${LOG_DIR}/fe.gc.log.$DATE:time -XX:ErrorFile=${LOG_DIR}/hs_err_pid%p.log -Djava.security.policy=${STARROCKS_HOME}/conf/udf_security.policy"##
## the lowercase properties are read by main program.
### DEBUG, INFO, WARN, ERROR, FATAL
sys_log_level = INFO
# store metadata, create it if it is not exist.
# Default value is ${STARROCKS_HOME}/meta
# 這里改了
meta_dir = ${STARROCKS_HOME}/metahttp_port = 8030
rpc_port = 9020
query_port = 9030
edit_log_port = 9010
mysql_service_nio_enabled = true# Enable jaeger tracing by setting jaeger_grpc_endpoint
# jaeger_grpc_endpoint = http://localhost:14250# Choose one if there are more than one ip except loopback address.
# Note that there should at most one ip match this list.
# If no ip match this rule, will choose one randomly.
# use CIDR format, e.g. 10.10.10.0/24
# Default value is empty.
# 這里改了
priority_networks = 10.10.10.0/24;192.168.0.94/16# Advanced configurations
# log_roll_size_mb = 1024
# sys_log_dir = ${STARROCKS_HOME}/log
# sys_log_roll_num = 10
# sys_log_verbose_modules =
# audit_log_dir = ${STARROCKS_HOME}/log
# audit_log_modules = slow_query, query
# audit_log_roll_num = 10
# meta_delay_toleration_second = 10
# qe_max_connection = 1024
# max_conn_per_user = 100
# qe_query_timeout_second = 300
# qe_slow_log_ms = 5000
?5、配置BE節點信息
vi /data/starrocks/be/conf/be.conf
動了三個配置:
放開了storage_root_path?的注釋,priority_networks修改為本機ip ,增加了限制jvm的內存 JAVA_OPTS="-Xmx1024"
確保此路徑存在并且擁有寫入權限
完整配置:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.# INFO, WARNING, ERROR, FATAL
sys_log_level = INFO# ports for admin, web, heartbeat service
be_port = 9060
be_http_port = 8040
heartbeat_service_port = 9050
brpc_port = 8060
starlet_port = 9070# Enable jaeger tracing by setting jaeger_endpoint
# jaeger_endpoint = localhost:6831# Choose one if there are more than one ip except loopback address.
# Note that there should at most one ip match this list.
# If no ip match this rule, will choose one randomly.
# use CIDR format, e.g. 10.10.10.0/24
# Default value is empty.
# 這里改了
priority_networks = 10.10.10.0/24;192.168.0.94/16# data root path, separate by ';'
# you can specify the storage medium of each root path, HDD or SSD, seperate by ','
# eg:
# storage_root_path = /data1,medium:HDD;/data2,medium:SSD;/data3
# /data1, HDD;
# /data2, SSD;
# /data3, HDD(default);
#
# Default value is ${STARROCKS_HOME}/storage, you should create it by hand.
# 這里改了
storage_root_path = ${STARROCKS_HOME}/storage# Advanced configurations
sys_log_dir = ${STARROCKS_HOME}/log
# sys_log_roll_mode = SIZE-MB-1024
# sys_log_roll_num = 10
# sys_log_verbose_modules = *
# log_buffer_level = -1# JVM options for be
# eg:
# 這里改了
JAVA_OPTS="-Xmx1024"
# JAVA_OPTS="-Djava.security.krb5.conf=/etc/krb5.conf"
# For jdk 9+, this JAVA_OPTS will be used as default JVM options
# JAVA_OPTS_FOR_JDK_9_AND_LATER="-Djava.security.krb5.conf=/etc/krb5.conf"
6、配置94的防火墻(非必須)
因為是fe+be所以端口多點(直接關閉防火墻也是可以的)
sudo firewall-cmd --zone=public --add-port=9030/tcp --permanent
sudo firewall-cmd --zone=public --add-port=9020/tcp --permanent
sudo firewall-cmd --zone=public --add-port=9010/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8030/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8040/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8050/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8060/tcp --permanentsudo firewall-cmd --reloadsudo systemctl restart firewalld
7、95和245機器配置的be.config
與94節點的be一樣配置,就是jvm內存變成了2048?
vi /data/starrocks/be/conf/be.conf
動了三個配置:放開了storage_root_path?的注釋,priority_networks修改為本機ip(注意2臺機器的ip不同) ,增加了限制jvm的內存 JAVA_OPTS="-Xmx2048"
完整配置:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.# INFO, WARNING, ERROR, FATAL
sys_log_level = INFO# ports for admin, web, heartbeat service
be_port = 9060
be_http_port = 8040
heartbeat_service_port = 9050
brpc_port = 8060
starlet_port = 9070# Enable jaeger tracing by setting jaeger_endpoint
# jaeger_endpoint = localhost:6831# Choose one if there are more than one ip except loopback address.
# Note that there should at most one ip match this list.
# If no ip match this rule, will choose one randomly.
# use CIDR format, e.g. 10.10.10.0/24
# Default value is empty.
# 這里改了
priority_networks = 10.10.10.0/24;192.168.0.95/16# data root path, separate by ';'
# you can specify the storage medium of each root path, HDD or SSD, seperate by ','
# eg:
# storage_root_path = /data1,medium:HDD;/data2,medium:SSD;/data3
# /data1, HDD;
# /data2, SSD;
# /data3, HDD(default);
#
# Default value is ${STARROCKS_HOME}/storage, you should create it by hand.
# 這里改了
storage_root_path = ${STARROCKS_HOME}/storage# Advanced configurations
sys_log_dir = ${STARROCKS_HOME}/log
# sys_log_roll_mode = SIZE-MB-1024
# sys_log_roll_num = 10
# sys_log_verbose_modules = *
# log_buffer_level = -1# JVM options for be
# eg:
# 這里改了
JAVA_OPTS="-Xmx2048"
# JAVA_OPTS="-Djava.security.krb5.conf=/etc/krb5.conf"
# For jdk 9+, this JAVA_OPTS will be used as default JVM options
# JAVA_OPTS_FOR_JDK_9_AND_LATER="-Djava.security.krb5.conf=/etc/krb5.conf"
8、配置95和245的防火墻(非必須)
直接關閉防火墻也是可以的
sudo firewall-cmd --zone=public --add-port=9050/tcp --permanent
sudo firewall-cmd --zone=public --add-port=9060/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8040/tcp --permanent
sudo firewall-cmd --zone=public --add-port=8060/tcp --permanentsudo firewall-cmd --reloadsudo systemctl restart firewalld
9、啟動94機器fe節點和be節點
./data/starrocks/fe/bin/start_fe.sh --daemon
./data/starrocks/be/bin/start_be.sh --daemon
查看fe節點是否啟動成功
cat /data/starrocks/fe/log/fe.log | grep thrift
?如果日志打印以下內容,則說明該 FE 節點啟動成功:
"2022-08-10 16:12:29,911 INFO (UNKNOWN x.x.x.x_9010_1660119137253(-1)|1) [FeServer.start():52] thrift server started with port 9020."
查看be節點是否啟動成功
cat /data/starrocks/be/log/be.INFO | grep heartbeat
如果日志打印以下內容,則說明該 BE 節點啟動成功:
"I0614 17:41:39.782819 3717531 thrift_server.cpp:388] heartbeat has started listening port on 9050"
10、啟動95和245的be節點
./data/starrocks/be/bin/start_be.sh --daemon
?11、使用客戶端連接94節點的fe,端口9030
一開始默認用戶是root,密碼為空,這里是我進去后加上了密碼
連接后
12、查看fe節點狀態
在sql窗口運行即可
-- 查看 Leader FE 節點狀態
SHOW PROC '/frontends'
Alive=true證明是好的?
?13、將be節點加入集群
-- 將BE節點加入集群
ALTER SYSTEM ADD BACKEND "192.168.0.94:9050", "192.168.0.95:9050", "192.168.0.245:9050";
加錯了可以刪除
-- 將be節點刪除
ALTER SYSTEM DROP BACKEND "192.168.0.97:9050";
14、查看be狀態
2個語句一樣的效果
-- 查看 BE/CN 節點狀態,alive=true
SHOW PROC '/backends'
-- 查看 BE/CN 節點狀態,alive=true
SHOW backends
?Alive=true證明是好的?
?失敗了可以看看原因,這里是因為我的ip搞錯了
15、建表及查詢
-- 設置root密碼
SET PASSWORD = PASSWORD('root123')-- 創建數據庫
CREATE DATABASE IF NOT EXISTS david;-- 刪除表
DROP TABLE david.ad_events-- 創建表
CREATE TABLE `ad_events` (
-- 數據類型,https://docs.mirrorship.cn/zh/docs/sql-reference/data-types/`event_time` DATETIME NOT NULL COMMENT '時間',`event_time_hour` DATETIME NOT NULL COMMENT '時間(分、秒為0)',`event_type` TINYINT NOT NULL COMMENT '事件類型(1:請求,2:展示,3:點擊)',`user_id` INT NOT NULL COMMENT '用戶ID(映射為整數)',`jhn_more` TINYINT NOT NULL COMMENT '是否聚合',`ad_site_id` VARCHAR(128) NOT NULL COMMENT '代碼jihuoniaojihuoniaojihuoniao位id',`client_type` TINYINT NOT NULL COMMENT '客戶端類型',`my_app_id` VARCHAR(64) NOT NULL COMMENT '應用id',`ad_type` VARCHAR(32) NOT NULL COMMENT '廣告類型',`platform` TINYINT NOT NULL COMMENT '廣告源',`device_id` VARCHAR(64) NOT NULL COMMENT '設備號'
)
-- 表類型(明細表),https://docs.mirrorship.cn/zh/docs/table_design/table_types/duplicate_key_table/
ENGINE=OLAP
-- 按月分區, https://docs.mirrorship.cn/zh/docs/table_design/data_distribution/expression_partitioning/
PARTITION BY date_trunc('month', event_time)
-- 數據分桶,https://docs.mirrorship.cn/zh/docs/table_design/data_distribution/#%E8%AE%BE%E7%BD%AE%E5%88%86%E6%A1%B6%E6%95%B0%E9%87%8F
DISTRIBUTED BY HASH(user_id) BUCKETS 100
PROPERTIES (-- 分區保留12個月"partition_live_number" = "12",-- 3個副本,根據be節點數據來,生產推薦3個"replication_num" = "3"
);-- 插入表數據
INSERT INTO ad_events (event_time, event_time_hour, event_type, user_id, more, ad_site_id, client_type, my_app_id, ad_type, platform, device_id)
VALUES
('2025-04-24 11:38:01', '2025-04-24 11:00:00', 3, 10018, 0, 'jhn_ad_site_id', 1, 'jhn_my_app_id', 'banner', 0, '214685fd-63d3-7f4b-f43d-beb48b5f59be'),
('2025-04-24 11:27:01', '2025-04-24 11:00:00', 2, 10018, 0, 'jhn_ad_site_id', 1, 'jhn_my_app_id', 'banner', 0, '214685fd-63d3-7f4b-f43d-beb48b5f59be')-- 統計分析sql
select event_type, COUNT(*) as num, COUNT(DISTINCT device_id) dau FROM ad_events GROUP by event_type-- 查看表當前的分區情況
SHOW PARTITIONS FROM ad_events;
查詢結果
總結:按照官網的文檔自己琢磨一下就可以部署成功了,官網文檔的部署前提一定要看一遍,線上部署最好是3fe+3be保證高可用。
部署前提條件 | StarRocks
參考文檔:
表類型:明細表 | StarRocks
數據類型:Data type overview | StarRocks
數據分區:表達式分區(推薦) | StarRocks
數據分桶:數據分布 | StarRocks
sql參考:SQL 參考 | StarRocks