共计 6318 个字符,预计需要花费 16 分钟才能阅读完成。
序
本文主要研究一下 rocketmq-mysql 的 ColumnParser
ColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/ColumnParser.java
public abstract class ColumnParser {public static ColumnParser getColumnParser(String dataType, String colType, String charset) {switch (dataType) {
case "tinyint":
case "smallint":
case "mediumint":
case "int":
return new IntColumnParser(dataType, colType);
case "bigint":
return new BigIntColumnParser(colType);
case "tinytext":
case "text":
case "mediumtext":
case "longtext":
case "varchar":
case "char":
return new StringColumnParser(charset);
case "date":
case "datetime":
case "timestamp":
return new DateTimeColumnParser();
case "time":
return new TimeColumnParser();
case "year":
return new YearColumnParser();
case "enum":
return new EnumColumnParser(colType);
case "set":
return new SetColumnParser(colType);
default:
return new DefaultColumnParser();}
}
public static String[] extractEnumValues(String colType) {String[] enumValues = {};
Matcher matcher = Pattern.compile("(enum|set)\\((.*)\\)").matcher(colType);
if (matcher.matches()) {enumValues = matcher.group(2).replace("'","").split(",");
}
return enumValues;
}
public abstract Object getValue(Object value);
}
- ColumnParser 定义了 getValue 抽象方法;它提供了 getColumnParser 方法用于根据 dataType 获取对应的 ColumnParser 实现类
IntColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/IntColumnParser.java
public class IntColumnParser extends ColumnParser {
private int bits;
private boolean signed;
public IntColumnParser(String dataType, String colType) {switch (dataType) {
case "tinyint":
bits = 8;
break;
case "smallint":
bits = 16;
break;
case "mediumint":
bits = 24;
break;
case "int":
bits = 32;
}
this.signed = !colType.matches(".* unsigned$");
}
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof Long) {return value;}
if (value instanceof Integer) {Integer i = (Integer) value;
if (signed || i > 0) {return i;} else {return (1L << bits) + i;
}
}
return value;
}
}
- IntColumnParser 解析 tinyint、smallint、mediumint、int 类型
BigIntColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/BigIntColumnParser.java
public class BigIntColumnParser extends ColumnParser {private static BigInteger max = BigInteger.ONE.shiftLeft(64);
private boolean signed;
public BigIntColumnParser(String colType) {this.signed = !colType.matches(".* unsigned$");
}
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof BigInteger) {return value;}
Long l = (Long) value;
if (!signed && l < 0) {return max.add(BigInteger.valueOf(l));
} else {return l;}
}
}
- BigIntColumnParser 解析 bigint 类型
StringColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/StringColumnParser.java
public class StringColumnParser extends ColumnParser {
private String charset;
public StringColumnParser(String charset) {this.charset = charset.toLowerCase();
}
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof String) {return value;}
byte[] bytes = (byte[]) value;
switch (charset) {
case "utf8":
case "utf8mb4":
return new String(bytes, Charsets.UTF_8);
case "latin1":
case "ascii":
return new String(bytes, Charsets.ISO_8859_1);
case "ucs2":
return new String(bytes, Charsets.UTF_16);
default:
return new String(bytes, Charsets.toCharset(charset));
}
}
}
- StringColumnParser 解析 tinytext、text、mediumtext、longtext、varchar、char 类型
DateTimeColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/DateTimeColumnParser.java
public class DateTimeColumnParser extends ColumnParser {
private static SimpleDateFormat dateTimeFormat;
private static SimpleDateFormat dateTimeUtcFormat;
static {dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateTimeUtcFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateTimeUtcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof Timestamp) {return dateTimeFormat.format(value);
}
if (value instanceof Long) {return dateTimeUtcFormat.format(new Date((Long) value));
}
return value;
}
}
- DateTimeColumnParser 解析 date、datetime、timestamp 类型
TimeColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/TimeColumnParser.java
public class TimeColumnParser extends ColumnParser {
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof Timestamp) {return new Time(((Timestamp) value).getTime());
}
return value;
}
}
- TimeColumnParser 解析 time 类型
YearColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/YearColumnParser.java
public class YearColumnParser extends ColumnParser {
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof Date) {Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) value);
return calendar.get(Calendar.YEAR);
}
return value;
}
}
- YearColumnParser 解析 year 类型
EnumColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/EnumColumnParser.java
public class EnumColumnParser extends ColumnParser {private String[] enumValues;
public EnumColumnParser(String colType) {enumValues = extractEnumValues(colType);
}
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof String) {return value;}
Integer i = (Integer) value;
if (i == 0) {return null;} else {return enumValues[i - 1];
}
}
}
- EnumColumnParser 解析 enum 类型
SetColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/SetColumnParser.java
public class SetColumnParser extends ColumnParser {private String[] enumValues;
public SetColumnParser(String colType) {enumValues = extractEnumValues(colType);
}
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof String) {return value;}
StringBuilder builder = new StringBuilder();
long l = (Long) value;
boolean needSplit = false;
for (int i = 0; i < enumValues.length; i++) {if (((l >> i) & 1) == 1) {if (needSplit)
builder.append(",");
builder.append(enumValues[i]);
needSplit = true;
}
}
return builder.toString();}
}
- SetColumnParser 解析 set 类型
DefaultColumnParser
rocketmq-externals/rocketmq-mysql/src/main/java/org/apache/rocketmq/mysql/schema/column/DefaultColumnParser.java
public class DefaultColumnParser extends ColumnParser {
@Override
public Object getValue(Object value) {if (value == null) {return null;}
if (value instanceof byte[]) {return Base64.encodeBase64String((byte[]) value);
}
return value;
}
}
- DefaultColumnParser 通过 base64 将 byte 数组转为 string
小结
ColumnParser 定义了 getValue 抽象方法;它提供了 getColumnParser 方法用于根据 dataType 获取对应的 ColumnParser 实现类
doc
- ColumnParser