Flink SQL实战演练之脚手架开发

flink sql目前开发的方式包括纯sql的开发,可以运行在zeppelin上,也可以采用代码进行开发,打包成jar运行,代码开发的话每次都要新建工程,会有一些重复性的工作要做,本章讲述通过脚手架的方式快速构建flink sql基本工程。

  • 创建工程,作为蓝本
mvn archetype:generate
-DarchetypeGroupId=org.apache.flink
-DarchetypeArtifactId=flink-quickstart-java
-DarchetypeVersion=1.14.2
-DgroupId=com.dpf.flink
-DartifactId=flink-sql
-Dversion=0.1
-Dpackage=com.dpf.flink
  • 添加基础代码
package com.dpf.flink;

import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

public class BatchSql {

    public static String hiveCatalog = "hive";
    public static String defaultDatabase = "default";
    public static String hiveConfDir = "/Users/dengpengfei/bigdata/apache-hive-3.1.2-bin/conf";

    public static void main(String[] args) {
        EnvironmentSettings settings = EnvironmentSettings.inBatchMode();
        TableEnvironment tableEnv = TableEnvironment.create(settings);

        //添加hive catalog
        HiveCatalog hive = new HiveCatalog(hiveCatalog, defaultDatabase, hiveConfDir);
        tableEnv.registerCatalog(hiveCatalog, hive);

        tableEnv.useCatalog(hiveCatalog);

        tableEnv.executeSql("select * from hive_table").print();
    }
}
  • 在项目pom中加入archetype插件
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-archetype-plugin</artifactId>
    <version>3.2.0</version>
</plugin>
  • 在maven中,双击archetype:create-from-project运行archetype插件


    脚手架生成

    *注:配置高版本的maven,idea中默认的可能出问题,运行结果如下:

[INFO] --------------------------[ maven-archetype ]---------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ flink-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using 'null' encoding to copy filtered properties files.
[INFO] Copying 7 resources
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ flink-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using 'null' encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-archetype-plugin:3.2.0:jar (default-jar) @ flink-archetype ---
[INFO] Building archetype jar: /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target/flink-archetype-1.0-SNAPSHOT.jar
[INFO] Building jar: /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target/flink-archetype-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.389 s
[INFO] Finished at: 2021-12-28T20:54:10+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Archetype project created in /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.704 s
[INFO] Finished at: 2021-12-28T20:54:10+08:00
[INFO] ------------------------------------------------------------------------
  • 执行安装命令
david.dong@localhost archetype % cd /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype
david.dong@localhost archetype % mvn clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.dpf:flink-archetype >-----------------------
[INFO] Building flink-archetype 1.0-SNAPSHOT
[INFO] --------------------------[ maven-archetype ]---------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ flink-archetype ---
[INFO] Deleting /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ flink-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using 'null' encoding to copy filtered properties files.
[INFO] Copying 7 resources
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ flink-archetype ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Using 'null' encoding to copy filtered properties files.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-archetype-plugin:3.2.0:jar (default-jar) @ flink-archetype ---
[INFO] Building archetype jar: /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target/flink-archetype-1.0-SNAPSHOT.jar
[INFO] Building jar: /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target/flink-archetype-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-archetype-plugin:3.2.0:integration-test (default-integration-test) @ flink-archetype ---
[INFO] Processing Archetype IT project: basic
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: flink-archetype:1.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: packageInPathFormat, Value: it/pkg
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[INFO] Project created from Archetype in dir: /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target/test-classes/projects/basic/project/basic
[INFO] 
[INFO] --- maven-install-plugin:3.0.0-M1:install (default-install) @ flink-archetype ---
[INFO] Installing /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/target/flink-archetype-1.0-SNAPSHOT.jar to /Users/david.dong/.m2/repository/com/dpf/flink-archetype/1.0-SNAPSHOT/flink-archetype-1.0-SNAPSHOT.jar
[INFO] Installing /Users/david.dong/workspace/archetype/flink/target/generated-sources/archetype/pom.xml to /Users/david.dong/.m2/repository/com/dpf/flink-archetype/1.0-SNAPSHOT/flink-archetype-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-archetype-plugin:3.2.0:update-local-catalog (default-update-local-catalog) @ flink-archetype ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.417 s
[INFO] Finished at: 2021-12-28T21:07:43+08:00
[INFO] ------------------------------------------------------------------------
  • 安装成功之后,就可以在本地仓库的根目录(如:/Users/david.dong/.m2/repository)中看到一个文件:archetype-catalog.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>com.dpf</groupId>
      <artifactId>flink-archetype</artifactId>
      <version>1.0-SNAPSHOT</version>
      <description>flink-archetype</description>
    </archetype>
  </archetypes>
</archetype-catalog>
  • 在idea中引入脚手架 点击菜单File -> new -> Project,选择maven,勾选Create from archetype,点击 Add Archetype按钮,填写groupId和artifactId、version信息(就是archetype-catalog.xml文件中的信息),点击OK,便添加完成,如下图:


    idea添加脚手架
  • 创建项目时使用脚手架


    使用脚手架

工欲善其事,必先利其器,将基础代码构建成脚手架很有必要。

?著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,029评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,238评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事?!?“怎么了?”我有些...
    开封第一讲书人阅读 159,576评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,214评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,324评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,392评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,416评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,196评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,631评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,919评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,090评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,767评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,410评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,090评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,328评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,952评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,979评论 2 351

推荐阅读更多精彩内容