目录
Makefile是什么
Makefile基本语法
Docker构建用的指令
参考
使用Makefile构建Docker
刚开始学习docker命令的时候,很喜欢一个字一个字敲,由于这样会记住命令。后来熟悉了以后,每次想要作一些操做的时候就不得不
重复的输入之前的命令。当切换一个项目以后,又重复输入相似但又不彻底相同的命令,仅仅经过history命令加速也有限。
因而想,把要用的命令写到shell里,而后调用shell脚本去作。刚开始确实是这样作的。好比https://github.com/Ryan-Miao/docker-yapi。
直到有一天,发现有人使用Makefile来存储操做,瞬间感受很棒。
这里简单记录Makefile的简单用法。
Makefile是什么
Makefile是make命令的规则配置文件。make命令是什么?
先来看看make在哪里
~ > whereis make
make: /usr/bin/make /usr/share/man/man1/make.1.gz
能够看到make是bin下的以可执行文件。 看看用户手册
MAKE(1) User Commands MAKE(1)
NAME
make - GNU make utility to maintain groups of programs
SYNOPSIS
make [OPTION]... [TARGET]...
DESCRIPTION
The make utility will determine automatically which pieces of a large program need to be recompiled, and issue the commands to recom‐
pile them. The manual describes the GNU implementation of make, which was written by Richard Stallman and Roland McGrath, and is cur‐
rently maintained by Paul Smith. Our examples show C programs, since they are very common, but you can use make with any programming
language whose compiler can be run with a shell command. In fact, make is not limited to programs. You can use it to describe any
task where some files must be updated automatically from others whenever the others change.
To prepare to use make, you must write a file called the makefile that describes the relationships among files in your program, and the
states the commands for updating each file. In a program, typically the executable file is updated from object files, which are in
turn made by compiling source files.
Once a suitable makefile exists, each time you change some source files, this simple shell command:
make
suffices to perform all necessary recompilations. The make program uses the makefile description and the last-modification times of
the files to decide which of the files need to be updated. For each of those files, it issues the commands recorded in the makefile.
make executes commands in the makefile to update one or more target names, where name is typically a program. If no -f option is
present, make will look for the makefiles GNUmakefile, makefile, and Makefile, in that order.
Normally you should call your makefile either makefile or Makefile. (We recommend Makefile because it appears prominently near the
beginning of a directory listing, right near other important files such as README.) The first name checked, GNUmakefile, is not recom‐
mended for most makefiles. You should use this name if you have a makefile that is specific to GNU make, and will not be understood by
other versions of make. If makefile is '-', the standard input is read.
make updates a target if it depends on prerequisite files that have been modified since the target was last modified, or if the target
does not exist.
大体是说make是GNU中维护和组织程序的。好比咱们的C语言编译, 再好比源码安装某些软件,好比nginx的时候。那么GNU是什么鬼?
GNU(GNU's Not Unix)是一个类Unix系统, 目标是建立一套彻底自由的操做系统。在Linux出现以前,GNU已经完成了除了内核以外大部分的软件。Linux出现以后,与GNU结合变成GNU/Linux。
严格的说,Linux只表明Linux内核,其余Linux软件称为Linux发行版。但因为商业发行商坚持称呼Linux, 虽然已经改名为GNU/Linux, 但你们依然叫Linux.
## 好比个人本机Ubuntu
~ ❯ uname
Linux
~ ❯ uname -a
Linux ryan-computer 4.18.0-20-generic #21~18.04.1-Ubuntu SMP Wed May 8 08:43:37 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
## 大部分基于Debian的docker镜像
airflow@88e36c088b81:~$ cat /etc/issue
Debian GNU/Linux 9 \n \l
## RedHat
[root@data-docker001 docker-airflow]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@data-docker001 docker-airflow]# uname -a
Linux data-docker001 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
make的基本用法就是
make target
Makefile基本语法
详细参见附录参考,这里为了减小认知成本,只罗列用到的知识点。
在当前目录建立一个叫作Makefile的文件。
声明变量
简单的变量赋值,好比声明name
name=ryan
声明规则Rule
Makefile文件由一系列规则(rules)构成。每条规则的形式以下。
<target> : <prerequisites>
[tab] <commands>
target 目标
prerequisites 前置条件
tab command必须由tab隔开
commands 只能有一行的shell
防止target和文件名同样
当咱们设置的target和当前目录下的文件名同样的话,target会被忽略,因此,一般,咱们把target都用作phony target。
.PHONY: build start push
表示, build start push 这3个target,不检查当前目录下的文件,直接执行命令。
Docker构建用的指令
我经常使用的Makefile以下
NAME = ryan/airflow
VERSION = 1.10.4
.PHONY: build start push
build: build-version
build-version:
docker build -t ${NAME}:${VERSION} .
tag-latest:
docker tag ${NAME}:${VERSION} ${NAME}:latest
start:
docker run -it --rm ${NAME}:${VERSION} /bin/bash
push: build-version tag-latest
docker push ${NAME}:${VERSION}; docker push ${NAME}:latest
构建一个版本的镜像
make build
构建完毕,运行一下镜像,看看内容是否正确
make start
最后推送到docker仓库
make push
从上面的操做中能够看出,Phoenix 支持大多数标准的 SQL 语法。关于 Phoenix 支持的语法、数据类型、函数、序列等详细信息,由于涉及内容不少,能够参考其官方文档,官方文档上有详细的说明:
语法 (Grammar) :https://www.jintianxuesha.com phoenix.apache.org/language/index.html
函数 (Functions) :http://www.zongxyuLe.com phoenix.apache.org/language/functions.html
数据类型 (Datatypes) :http://www.zheshengyule.com phoenix.apache.org/language/datatypes.html
序列 (Sequences) :http://www.yuanhuapt.cn phoenix.apache.org/sequences.html
联结查询 (Joins) :http://www.yasenyuLe.com phoenix.apache.org/joins.html
4、Phoenix Java API#
由于 Phoenix 遵循 JDBC 规范,并提供了对应的数据库驱动 PhoenixDriver,这使得采用 Java 语言对其进行操做的时候,就如同对其余关系型数据库同样,下面给出基本的使用示例。
4.1 引入Phoenix core JAR包#
若是是 maven 项目,直接在 maven 中央仓库找到对应的版本,导入依赖便可:
Copy
<!-- https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-core --> <dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core</artifactId> www.xinhegzx.cn <version>4.14.0-cdh5.14.2</version> </dependency>
若是是普通项目,则能够从 Phoenix 解压目录下找到对应的 JAR 包,而后手动引入:
4.2 简单的Java API实例#
Copy
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class PhoenixJavaApi { public static void main(String[] args) throws Exception { // 加载数据库驱动 Class.forName("org.apache.phoenix.jdbc.PhoenixDriver"); /* * 指定数据库地址,格式为 jdbc:phoenix:Zookeeper 地址 * 若是 HBase 采用 Standalone 模式或者伪集群模式搭建,则 HBase 默认使用内置的 Zookeeper,默认端口为 2181 */ Connection connection = DriverManager.getConnection("jdbc:phoenix:192.168.200.226:2181"); PreparedStatement statement = connection.prepareStatement("SELECT * FROM us_population"); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString("city") + " " + resultSet.getInt("population")); } statement.close(); connection.close(); }html