LinkedIn Coral 编译&SQL翻译使用
Coral是一个 SQL 翻译、分析和重写引擎。简单来说,可以将一种数据库的 SQL 方言翻译成另一种数据库的 SQL 方言。
构建
1.IDEA 编译代码
1 2 3
   | git clone https://github.com/linkedin/coral.git # 使用 Java 8 ./gradlew clean build
 
  | 
 
2.运行 Coral Service Rest 服务
方式1:
1 2 3 4 5 6 7
   | cd coral-service   # 运行 coral 服务,指定参数: 使用本地元存储运行 Coral 服务 ../gradlew bootRun --args='--spring.profiles.active=localMetastore'   # 后端服务在端口 8080 上运行(默认)
  # 启动阶段开启 remote debug ../gradlew bootRun --debug-jvm --args='--spring.profiles.active=localMetastore'  
 
  | 
 
方式2: 运行 com.linkedin.coral.coralservice.CoralServiceApplication
指定 vm arguments: -Dspring.profiles.active=localMetastore
1 2
   | -- 确定日志里打印使用了 localMetastore profile c.l.c.c.CoralServiceApplication          : The following profiles are active: localMetastore
 
  | 
 
3.编译运行 Coral Service UI 前端服务
运行 Coral Service UI
1 2 3 4 5 6 7 8
   | # Web UI 在端口 3000 上运行(默认) cd coral-service/frontend # 2.复制 .env.local.example 为 .env.local,配置 NEXT_PUBLIC_CORAL_SERVICE_API_URL="http://localhost:8080" # 3.安装npm依赖 npm install npm run dev # 浏览器访问 http://localhost:3000/
 
  | 
 
1 2 3 4 5 6 7
   | 前端ui功能     在本地元存储模式下创建数据库/表/视图         调用 /api/catalog-ops/execute api         输入 SQL 语句在本地元存储中创建数据库/表/视图     将 SQL 从源语言翻译成目标语言(带重写)         调用 /api/translations/translate api     生成 Coral 中间表示的 graphviz 可视化
 
  | 
 
前端建表功能。
1 2 3
   | CREATE DATABASE IF NOT EXISTS sharding_db
  CREATE TABLE sharding_db.t_order (order_id INT, user_id INT, status String)
 
  | 
 
建表成功提示

SQL 翻译功能测试
1
   | select * from sharding_db.t_order order by order_id
 
  | 
 


4.Rest API 调用 Coral Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   | # 创建 database curl --header "Content-Type: application/json" \   --request POST \   --data "CREATE DATABASE IF NOT EXISTS sharding_db" \   http://localhost:8080/api/catalog-ops/execute
  Creation successful
  # 建表 curl --header "Content-Type: application/json" \   --request POST \   --data "CREATE TABLE IF NOT EXISTS sharding_db.t_order (order_id INT, user_id INT, status String)" \   http://localhost:8080/api/catalog-ops/execute
  Creation successful
  # SQL 翻译 curl --header "Content-Type: application/json" \   --request POST \   --data '{     "sourceLanguage":"hive",      "targetLanguage":"trino",      "query":"SELECT * FROM sharding_db.t_order"   }' \   http://localhost:8080/api/translations/translate
 
  | 
 

参考
coral github