From fd94c7ff367aaace16aff4f8e2aaf853cdaad323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Sat, 22 Feb 2025 07:58:29 +0800 Subject: [PATCH] 'commit' --- AI/ClickHouse/get_mysql_table_structure.py | 30 ++++++-- AI/ClickHouse/schemas/t_station.sql | 88 ++++++++++++++++++++++ 2 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 AI/ClickHouse/schemas/t_station.sql diff --git a/AI/ClickHouse/get_mysql_table_structure.py b/AI/ClickHouse/get_mysql_table_structure.py index 838b1967..e115066c 100644 --- a/AI/ClickHouse/get_mysql_table_structure.py +++ b/AI/ClickHouse/get_mysql_table_structure.py @@ -78,7 +78,6 @@ def convert_to_clickhouse_type(mysql_type): return type_map.get(type_str, 'String') - def generate_clickhouse_ddl(table_name, columns, indexes, primary_keys): """生成ClickHouse建表语句""" # 处理字段定义 @@ -86,20 +85,37 @@ def generate_clickhouse_ddl(table_name, columns, indexes, primary_keys): for col in columns: name, data_type, column_type, is_nullable, default = col ch_type = convert_to_clickhouse_type(column_type) + # 修正浮点型定义 + if 'Float64' in ch_type and '(' in ch_type: + ch_type = 'Float64' nullable = 'Nullable(' + ch_type + ')' if is_nullable == 'YES' else ch_type column_defs.append(f' {name} {nullable}') # 处理主键 order_by = 'ORDER BY (' + ', '.join(primary_keys) + ')' if primary_keys else '' - # 处理索引(ClickHouse的索引语法不同) - index_defs = [] + # 处理索引(新增分组逻辑) + index_groups = {} for idx in indexes: index_name, column_name, non_unique = idx if index_name == 'PRIMARY': continue - index_type = 'INDEX' if non_unique else 'UNIQUE' - index_defs.append(f' {index_type} {index_name} {column_name} TYPE minmax GRANULARITY 3') + if index_name not in index_groups: + index_groups[index_name] = { + 'non_unique': non_unique, + 'columns': [] + } + index_groups[index_name]['columns'].append(column_name) + + index_defs = [] + for index_name, info in index_groups.items(): + columns = info['columns'] + # 修正索引语法(ClickHouse不支持UNIQUE KEY) + index_type = 'minmax' # 默认索引类型 + # 统一使用INDEX语法,ClickHouse 24.8+ 的MergeTree引擎不支持UNIQUE约束 + index_defs.append( + f' INDEX {index_name} ({", ".join(columns)}) TYPE {index_type} GRANULARITY 3' + ) # 组合DDL语句 ddl = f'CREATE TABLE {table_name} (\n' @@ -128,3 +144,7 @@ if __name__ == '__main__': # 生成ClickHouse DDL ddl = generate_clickhouse_ddl(table, columns, indexes, primary_keys) print(ddl) + # 保存到文件中 schemas/t_station.sql + with open(f'schemas/{table}.sql', 'w',encoding='utf-8') as f: + f.write(ddl) + print(f'✅ DDL语句已保存至:schemas/{table}.sql') diff --git a/AI/ClickHouse/schemas/t_station.sql b/AI/ClickHouse/schemas/t_station.sql new file mode 100644 index 00000000..4890d36b --- /dev/null +++ b/AI/ClickHouse/schemas/t_station.sql @@ -0,0 +1,88 @@ +CREATE TABLE t_station ( + id Int64, + station_sn String, + operator_id Int64, + equioment_owner_id String, + station_name String, + country_code String, + region_id Nullable(Int64), + region_code String, + regional_name Nullable(String), + regional_code Nullable(String), + address String, + station_tel Nullable(String), + service_tel String, + station_type Int16, + station_type_msg Nullable(String), + station_business_type Nullable(Int8), + station_business_type_msg Nullable(String), + station_status Int16, + station_status_msg Nullable(String), + park_nums Int16, + station_lng Float64, + station_lat Float64, + site_guide Nullable(String), + construction Int16, + construction_msg Nullable(String), + pictures Nullable(String), + match_cars Nullable(String), + park_info Nullable(String), + business_start_time Nullable(Int16), + business_end_time Nullable(Int16), + electricity_fee Nullable(String), + service_fee Nullable(String), + is_park_fee Nullable(Int8), + park_fee Nullable(String), + payment Nullable(Int8), + payment_msg Nullable(String), + support_order Nullable(Int8), + support_order_msg Nullable(String), + service_item Nullable(String), + service_item_image_url Nullable(String), + service_item_msg Nullable(String), + station_location_type Nullable(Int8), + station_location_type_msg Nullable(String), + station_charge_type Nullable(Int8), + station_charge_type_msg Nullable(String), + station_power Nullable(String), + station_voltage Nullable(String), + station_current Nullable(String), + is_open Nullable(Int8), + is_open_msg Nullable(String), + is_display Nullable(Int8), + is_display_msg Nullable(String), + sharing_code Nullable(String), + sharing_applet_picture Nullable(String), + theme_picture Nullable(String), + is_barrier_flag Nullable(Int8), + is_barrier_flag_msg Nullable(String), + charge_settle_type Nullable(Int8), + charge_settle_type_msg Nullable(String), + fast_connector_nums Nullable(Int32), + slow_connector_nums Nullable(Int32), + transformer_capacity Nullable(Float64), + installed_capacity Nullable(Float64), + remark Nullable(String), + record_unique_no String, + equipment_owner_name String, + supply_type Int8, + resident_no String, + watt_hour_meter_no String, + forward_power String, + operation_date DateTime, + charge_replace_type Int8, + is_hlht Int8, + is_subsidy Int8, + create_time Nullable(DateTime), + update_time Nullable(DateTime), + area_code_countryside Nullable(String), + business_expand_type Nullable(Int8), + capacity Float64, + video_monitor Nullable(Int8), + electricity_type Nullable(Int8), + INDEX station_sn (station_sn) TYPE minmax GRANULARITY 3, + INDEX operator_id (operator_id) TYPE minmax GRANULARITY 3, + INDEX idx_operator_id (operator_id) TYPE minmax GRANULARITY 3 +) ENGINE = MergeTree() +ORDER BY (id) +SETTINGS index_granularity = 8192; \ No newline at end of file