phoenix实现

This commit is contained in:
xielongwang
2020-08-27 21:45:37 +08:00
parent 2703ddebb4
commit 9c273815a3
13 changed files with 619 additions and 1 deletions

View File

@@ -127,6 +127,29 @@
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<id>phoenix</id>
<dependencies>
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>5.0.0-HBase-2.0</version>
</dependency>
<!--与mysql中的 protobuf-java 冲突-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
</profiles>
<!--build--> <!--build-->
<build> <build>
<plugins> <plugins>

View File

@@ -92,4 +92,16 @@ public class DefaultConstants implements Serializable {
* Y * Y
*/ */
public static final String Y = "Y"; public static final String Y = "Y";
/**
* phoenix 命名空间
*/
public static final String PHOENIX_NAMESPACE_MAPPING = "phoenix.schema.isNamespaceMappingEnabled";
/**
* phoenix 系统命名空间
*/
public static final String PHOENIX_SYS_NAMESPACE_MAPPING = "phoenix.schema.mapSystemTablesToNamespace";
} }

View File

@@ -26,6 +26,7 @@ import cn.smallbun.screw.core.query.hsql.HsqlDataBaseQuery;
import cn.smallbun.screw.core.query.mariadb.MariaDbDataBaseQuery; import cn.smallbun.screw.core.query.mariadb.MariaDbDataBaseQuery;
import cn.smallbun.screw.core.query.mysql.MySqlDataBaseQuery; import cn.smallbun.screw.core.query.mysql.MySqlDataBaseQuery;
import cn.smallbun.screw.core.query.oracle.OracleDataBaseQuery; import cn.smallbun.screw.core.query.oracle.OracleDataBaseQuery;
import cn.smallbun.screw.core.query.phoenix.PhoenixDataBaseQuery;
import cn.smallbun.screw.core.query.postgresql.PostgreSqlDataBaseQuery; import cn.smallbun.screw.core.query.postgresql.PostgreSqlDataBaseQuery;
import cn.smallbun.screw.core.query.sqlite.SqliteDataBaseQuery; import cn.smallbun.screw.core.query.sqlite.SqliteDataBaseQuery;
import cn.smallbun.screw.core.query.sqlservice.SqlServerDataBaseQuery; import cn.smallbun.screw.core.query.sqlservice.SqlServerDataBaseQuery;
@@ -124,7 +125,7 @@ public enum DatabaseType implements Serializable {
* Phoenix * Phoenix
*/ */
PHOENIX("phoenix", "Phoenix HBase数据库", PHOENIX("phoenix", "Phoenix HBase数据库",
OtherDataBaseQuery.class), PhoenixDataBaseQuery.class),
/** /**
* CacheDB * CacheDB

View File

@@ -0,0 +1,119 @@
package cn.smallbun.screw.core.query.phoenix;
import cn.smallbun.screw.core.exception.QueryException;
import cn.smallbun.screw.core.mapping.Mapping;
import cn.smallbun.screw.core.metadata.Database;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import cn.smallbun.screw.core.query.AbstractDatabaseQuery;
import cn.smallbun.screw.core.query.phoenix.model.PhoenixColumnModel;
import cn.smallbun.screw.core.query.phoenix.model.PhoenixPrimaryKeyModel;
import cn.smallbun.screw.core.query.phoenix.model.PhoenixSqlDatabaseModel;
import cn.smallbun.screw.core.query.phoenix.model.PhoenixlTableModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.JdbcUtils;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN;
/**
* @author xielongwang
* @create 2020/8/19 4:53 下午
* @email siaron.wang@gmail.com
* @description
*/
public class PhoenixDataBaseQuery extends AbstractDatabaseQuery {
public PhoenixDataBaseQuery(DataSource dataSource) {
super(dataSource);
}
@Override
public Database getDataBase() throws QueryException {
PhoenixSqlDatabaseModel model = new PhoenixSqlDatabaseModel();
model.setDatabase(getCatalog());
return model;
}
@Override
public List<PhoenixlTableModel> getTables() throws QueryException {
ResultSet resultSet = null;
try {
//查询
resultSet = getMetaData().getTables("", getSchema(), PERCENT_SIGN,
new String[]{"TABLE"});
//映射
return Mapping.convertList(resultSet, PhoenixlTableModel.class);
} catch (SQLException e) {
throw ExceptionUtils.mpe(e);
} finally {
JdbcUtils.close(resultSet);
}
}
@Override
public List<PhoenixColumnModel> getTableColumns(String table) throws QueryException {
Assert.notEmpty(table, "Table name can not be empty!");
ResultSet resultSet = null;
try {
//查询
resultSet = getMetaData().getColumns("", getSchema(), table, PERCENT_SIGN);
//映射
List<PhoenixColumnModel> list = Mapping.convertList(resultSet, PhoenixColumnModel.class);
//处理columnName
list.forEach(model -> model.setColumnType(model.getTypeName()));
return list;
} catch (SQLException e) {
throw ExceptionUtils.mpe(e);
} finally {
JdbcUtils.close(resultSet);
}
}
@Override
public List<PhoenixColumnModel> getTableColumns() throws QueryException {
return getTableColumns(PERCENT_SIGN);
}
@Override
public List<PhoenixPrimaryKeyModel> getPrimaryKeys(String table) throws QueryException {
ResultSet resultSet = null;
try {
//查询
resultSet = getMetaData().getPrimaryKeys("", getSchema(), table);
//映射
return Mapping.convertList(resultSet, PhoenixPrimaryKeyModel.class);
} catch (SQLException e) {
throw ExceptionUtils.mpe(e);
} finally {
JdbcUtils.close(resultSet, this.connection);
}
}
@Override
public List<? extends PrimaryKey> getPrimaryKeys() throws QueryException {
ResultSet resultSet = null;
try {
// 由于单条循环查询存在性能问题所以这里通过自定义SQL查询数据库主键信息
String sql = "SELECT '' AS TABLE_CAT, TABLE_SCHEM AS TABLE_SCHEM, TABLE_NAME, COLUMN_NAME AS COLUMN_NAME, KEY_SEQ, PK_NAME FROM SYSTEM.\"CATALOG\" WHERE PK_NAME IS NOT NULL AND KEY_SEQ IS NOT NULL GROUP BY TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, KEY_SEQ, PK_NAME";
// 拼接参数
resultSet = prepareStatement(sql).executeQuery();
return Mapping.convertList(resultSet, PhoenixPrimaryKeyModel.class);
} catch (SQLException e) {
throw new QueryException(e);
} finally {
JdbcUtils.close(resultSet);
}
}
@Override
protected String getCatalog() throws QueryException {
return "phoenix";
}
}

View File

@@ -0,0 +1,175 @@
/*
* screw-core - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.core.query.phoenix.model;
import cn.smallbun.screw.core.mapping.MappingField;
import cn.smallbun.screw.core.metadata.Column;
import lombok.Data;
/**
* 表字段信息
*
* @author siaron.wang@gmail.com
*/
@Data
public class PhoenixColumnModel implements Column {
private static final long serialVersionUID = -7231934486902707912L;
/**
*
*/
@MappingField(value = "SCOPE_TABLE")
private Object scopeTable;
/**
*
*/
@MappingField(value = "TABLE_CAT")
private String tableCat;
/**
*
*/
@MappingField(value = "BUFFER_LENGTH")
private String bufferLength;
/**
*
*/
@MappingField(value = "IS_NULLABLE")
private String isNullable;
/**
* 表名
*/
@MappingField(value = "TABLE_NAME")
private String tableName;
/**
* 默认值
*/
@MappingField(value = "COLUMN_DEF")
private String columnDef;
/**
*
*/
@MappingField(value = "SCOPE_CATALOG")
private Object scopeCatalog;
/**
*
*/
@MappingField(value = "TABLE_SCHEM")
private Object tableSchem;
/**
*
*/
@MappingField(value = "COLUMN_NAME")
private String columnName;
/**
*
*/
@MappingField(value = "NULLABLE")
private String nullable;
/**
* 说明
*/
@MappingField(value = "REMARKS")
private String remarks;
/**
* 小数位
*/
@MappingField(value = "DECIMAL_DIGITS")
private String decimalDigits;
/**
*
*/
@MappingField(value = "NUM_PREC_RADIX")
private String numPrecRadix;
/**
*
*/
@MappingField(value = "SQL_DATETIME_SUB")
private String sqlDatetimeSub;
/**
*
*/
@MappingField(value = "IS_GENERATEDCOLUMN")
private String isGeneratedColumn;
/**
*
*/
@MappingField(value = "IS_AUTOINCREMENT")
private String isAutoIncrement;
/**
*
*/
@MappingField(value = "SQL_DATA_TYPE")
private String sqlDataType;
/**
*
*/
@MappingField(value = "CHAR_OCTET_LENGTH")
private String charOctetLength;
/**
*
*/
@MappingField(value = "ORDINAL_POSITION")
private String ordinalPosition;
/**
*
*/
@MappingField(value = "SCOPE_SCHEMA")
private Object scopeSchema;
/**
*
*/
@MappingField(value = "SOURCE_DATA_TYPE")
private Object sourceDataType;
/**
* 数据类型
*/
@MappingField(value = "DATA_TYPE")
private String dataType;
/**
*
*/
@MappingField(value = "TYPE_NAME")
private String typeName;
/**
* 列表示给定列的指定列大小。
* 对于数值数据,这是最大精度。
* 对于字符数据,这是字符长度。
* 对于日期时间数据类型,这是 String 表示形式的字符长度(假定允许的最大小数秒组件的精度)。
* 对于二进制数据,这是字节长度。
* 对于 ROWID 数据类型,这是字节长度。对于列大小不适用的数据类型,则返回 Null。
*/
@MappingField(value = "COLUMN_SIZE")
private String columnSize;
/**
* 是否主键
*/
private String primaryKey;
/**
* 列类型(带长度)
*/
@MappingField(value = "COLUMN_TYPE")
private String columnType;
/**
* 列长度
*/
@MappingField(value = "COLUMN_LENGTH")
private String columnLength;
}

View File

@@ -0,0 +1,63 @@
/*
* screw-core - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.core.query.phoenix.model;
import cn.smallbun.screw.core.mapping.MappingField;
import cn.smallbun.screw.core.metadata.PrimaryKey;
import lombok.Data;
/**
* 表主键
*
* @author siaron.wang@gmail.com
*/
@Data
public class PhoenixPrimaryKeyModel implements PrimaryKey {
private static final long serialVersionUID = -4908250184995248600L;
/**
* 主键名称
*/
@MappingField(value = "PK_NAME")
private String pkName;
/**
*
*/
@MappingField(value = "TABLE_SCHEM")
private String tableSchem;
/**
*
*/
@MappingField(value = "KEY_SEQ")
private String keySeq;
/**
* tableCat
*/
@MappingField(value = "TABLE_CAT")
private String tableCat;
/**
* 列名
*/
@MappingField(value = "COLUMN_NAME")
private String columnName;
/**
* 表名
*/
@MappingField(value = "TABLE_NAME")
private String tableName;
}

View File

@@ -0,0 +1,18 @@
package cn.smallbun.screw.core.query.phoenix.model;
import cn.smallbun.screw.core.metadata.Database;
import lombok.Data;
/**
* @author xielongwang
* @create 2020/8/19 4:55 下午
* @email siaron.wang@gmail.com
* @description
*/
@Data
public class PhoenixSqlDatabaseModel implements Database {
/**
* 数据库名称
*/
private String database;
}

View File

@@ -0,0 +1,81 @@
/*
* screw-core - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.core.query.phoenix.model;
import cn.smallbun.screw.core.mapping.MappingField;
import cn.smallbun.screw.core.metadata.Table;
import lombok.Data;
/**
* 表信息
*
* @author siaron.wang@gmail.com
*/
@Data
public class PhoenixlTableModel implements Table {
/**
* refGeneration
*/
@MappingField(value = "REF_GENERATION")
private String refGeneration;
/**
* typeName
*/
@MappingField(value = "TYPE_NAME")
private String typeName;
/**
* typeSchem
*/
@MappingField(value = "TYPE_SCHEM")
private String typeSchem;
/**
* tableSchem
*/
@MappingField(value = "TABLE_SCHEM")
private String tableSchem;
/**
* typeCat
*/
@MappingField(value = "TYPE_CAT")
private String typeCat;
/**
* tableCat
*/
@MappingField(value = "TABLE_CAT")
private Object tableCat;
/**
* 表名称
*/
@MappingField(value = "TABLE_NAME")
private String tableName;
/**
* selfReferencingColName
*/
@MappingField(value = "SELF_REFERENCING_COL_NAME")
private String selfReferencingColName;
/**
* 说明
*/
@MappingField(value = "REMARKS")
private String remarks;
/**
* 表类型
*/
@MappingField(value = "TABLE_TYPE")
private String tableType;
}

View File

@@ -0,0 +1,25 @@
/*
* screw-core - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Database query
*
* @author xielong.wang
* Created by siaron.wang@gmail.com
*/
package cn.smallbun.screw.core.query.phoenix;
//phoenix 默认无法添加注释的(不解析 COMMENT ON COLUMN 语句). 但是可以再catalog表中remark字段上手动增加注释.符合jdbc的规范.导出时可以有注释的.

View File

@@ -0,0 +1,4 @@
driver=org.apache.phoenix.jdbc.PhoenixDriver
url=jdbc:phoenix:xxxx:2181
username=username
password=password

View File

@@ -0,0 +1,91 @@
/*
* screw-core - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.core.produce;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
import static cn.smallbun.screw.core.common.Constants.fileOutputDir;
/**
* Phoenix 文档生成测试
*
* @author xielong.wang
* Created by siaron.wang@gmail.com 2020/3/30 18:36
*/
public class PhoenixDocumentationBuilderTest extends AbstractDocumentationExecute {
/**
* 构建
*/
@Test
void build() throws IOException {
//数据源
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(getDriver());
hikariConfig.setJdbcUrl(getUrl());
hikariConfig.setUsername(getUserName());
hikariConfig.setPassword(getPassword());
hikariConfig.setMinimumIdle(2);
hikariConfig.setMaximumPoolSize(5);
Properties properties = new Properties();
properties.put("phoenix.schema.isNamespaceMappingEnabled", true);
properties.put("phoenix.schema.mapSystemTablesToNamespace", true);
hikariConfig.setDataSourceProperties(properties);
DataSource dataSource = new HikariDataSource(hikariConfig);
//生成配置
EngineConfig engineConfig = EngineConfig.builder()
//生成文件路径
.fileOutputDir(fileOutputDir)
//文件类型
.fileType(EngineFileType.HTML)
//生成模板实现
.produceType(EngineTemplateType.freemarker).build();
//配置
Configuration config = Configuration.builder()
//版本
.version("1.0.0")
//描述
.description("数据库设计文档生成")
//数据源
.dataSource(dataSource)
//生成配置
.engineConfig(engineConfig).build();
execute(config);
}
/**
* 获取配置文件
*
* @return {@link Properties}
*/
@Override
public String getConfigProperties() {
return System.getProperty("user.dir") + "/src/main/resources/properties/phoenix.properties";
}
}

View File

@@ -39,6 +39,7 @@ import org.apache.maven.plugins.annotations.Parameter;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.List; import java.util.List;
import java.util.Properties;
import static cn.smallbun.screw.core.constant.DefaultConstants.*; import static cn.smallbun.screw.core.constant.DefaultConstants.*;
import static cn.smallbun.screw.core.util.FileUtils.getRealFilePath; import static cn.smallbun.screw.core.util.FileUtils.getRealFilePath;
@@ -267,6 +268,11 @@ public class RunDocMojo extends AbstractMojo {
|| JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.MARIADB)) { || JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.MARIADB)) {
hikariConfig.addDataSourceProperty(USE_INFORMATION_SCHEMA, "true"); hikariConfig.addDataSourceProperty(USE_INFORMATION_SCHEMA, "true");
} }
//phoenix
if (JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.PHOENIX)) {
hikariConfig.addDataSourceProperty(PHOENIX_SYS_NAMESPACE_MAPPING,true);
hikariConfig.addDataSourceProperty(PHOENIX_NAMESPACE_MAPPING,true);
}
//oracle //oracle
if (JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.ORACLE)) { if (JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.ORACLE)) {
hikariConfig.addDataSourceProperty(ORACLE_REMARKS, "true"); hikariConfig.addDataSourceProperty(ORACLE_REMARKS, "true");