ndbcluster operator

ndbcluster operator

import java.sql.SQLException;

/************************************************
* @ClassName: NdbClusterOperator.java
* @Tables:
* @Author: wyl
* @CreateDate: 2019/5/20
***********************************************/
public class NdbClusterOperator {

/**
* 在任何给定时间,每个NDB群集实例只能有一个日志文件组
* @param isCreate
* @param logFileName
* @param initSize 设置日志文件的初始大小
* @param bufferSize
* @param dbUtil
*/
public static void createOrAlterdLogGroup(boolean isCreate, String logFileName, Integer initSize, Integer bufferSize, DBUtil dbUtil){
checkArgument(logFileName);
StringBuilder logSql = new StringBuilder();
logSql.append(createOrAlter(isCreate));
logSql.append("LOGFILE GROUP log_tk ADD UNDOFILE '");
logSql.append(logFileName);
logSql.append("' ");
if (null != initSize){
logSql.append("INITIAL_SIZE ");
logSql.append(initSize);
logSql.append("M");
}
if (null != bufferSize){
logSql.append("UNDO_BUFFER_SIZE ");
logSql.append(bufferSize);
logSql.append("M");
}
logSql.append("ENGINE NDBCLUSTER;");
try {
dbUtil.executeUpdate(logSql.toString(), null);
} catch (SQLException e) {
e.printStackTrace();
}

}

public static void createTableSpace(boolean isCreate, String spaceName, String dataFileName, Integer initSize, DBUtil dbUtil){
checkArgument(spaceName);
StringBuilder spaceSql = new StringBuilder();
spaceSql.append(createOrAlter(isCreate));
spaceSql.append("TABLESPACE ");
spaceSql.append(spaceName);

spaceSql.append(" ADD DATAFILE '");
spaceSql.append(dataFileName);
spaceSql.append("' ");
if (isCreate){
spaceSql.append("USE LOGFILE GROUP log_tk");
}
if (null != initSize){
spaceSql.append("INITIAL_SIZE ");
spaceSql.append(initSize);
spaceSql.append("M");
}
spaceSql.append("ENGINE NDBCLUSTER;");
try {
dbUtil.executeUpdate(spaceSql.toString(), null);
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 删除规则
* A log file group cannot be dropped as long as any tablespaces use it.
* A tablespace cannot be dropped as long as it contains any data files.
* You cannot drop any data files from a tablespace as long as there remain any tables which are using the tablespace.
* It is not possible to drop files created in association with a different tablespace other than the one with which the files were created.
* @param dbUtil
* @param spaceName
* @param dataFileName
* @param type "data"删除数据,"tablespace" :删除表空间 "loggroup":删除日志组
*/
public static void dropSequence(DBUtil dbUtil, String spaceName, String dataFileName, String type){
switch (type){
case "data":
dropTableSpaceData(dbUtil, spaceName, dataFileName);
break;
case "tablespace":
dropTableSpaceData(dbUtil, spaceName, dataFileName);
dropTableSpace(dbUtil,spaceName);
break;
case "loggroup":
dropTableSpaceData(dbUtil, spaceName, dataFileName);
dropTableSpace(dbUtil,spaceName);
dropLogGroup(dbUtil);
break;
default:
throw new IllegalArgumentException("参数错误");
}
}

public static void CreateDocumentTable(String tableName, String spaceName, DBUtil dbUtil){
checkArgument(spaceName);
checkNullValue(tableName);
StringBuilder executeSql = new StringBuilder();
executeSql.append("CREATE TABLE ");
executeSql.append(tableName);
executeSql.append(" `doc` json DEFAULT NULL,");
executeSql.append(" `_id` VARBINARY (32) GENERATED ALWAYS AS (json_unquote( json_extract (`doc`, _utf8mb4 '$._id'))) STORED NOT NULL,");
executeSql.append("PRIMARY KEY (`_id`),UNIQUE KEY `trnk_dc` (`_id`) USING BTREE) TABLESPACE ");
executeSql.append(spaceName);
executeSql.append(" STORAGE DISK ENGINE NDBCLUSTER;");
try {
dbUtil.executeUpdate(executeSql.toString(), null);
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void dropLogGroup(DBUtil dbUtil){
String dropSql = "DROP LOGFILE GROUP log_tk ENGINE = NDB;";
try {
dbUtil.executeUpdate(dropSql, null);
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void dropTableSpace(DBUtil dbUtil, String spaceName){
checkArgument(spaceName);
String dropSql = "DROP TABLESPACE "+spaceName+" ENGINE=NDB;";
try {
dbUtil.executeUpdate(dropSql, null);
} catch (SQLException e) {
e.printStackTrace();
}
}

private static void dropTableSpaceData(DBUtil dbUtil, String spaceName, String dataFileName){
checkArgument(spaceName);
checkNullValue(dataFileName);
String dropSql = "ALTER TABLESPACE "+spaceName+" DROP DATAFILE '"+dataFileName+"' ENGINE=NDB;";
try {
dbUtil.executeUpdate(dropSql, null);
} catch (SQLException e) {
e.printStackTrace();
}
}

private static boolean checkArgument(String name){
if (null != name && !"".equals(name) && !name.contains("."))
return true;
throw new IllegalArgumentException("请检查参数名称");
}

private static String createOrAlter(boolean isCreate){
String type = "ALTER ";
if (isCreate)
type = "CREATE ";
return type;
}

private static void checkNullValue(String value){
if (null == value && "".trim().equals(value))
throw new NullPointerException("参数不允许为空");
}

}
posted @ 2019-05-20 18:02 龘_曦 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值