模型设计


SQL 解析器:值类型解析设计_leetcode


public class StringFieldValue extends FieldValue<String> {
public StringFieldValue(String value) {
this.value = value;
}

@Override
public String getSqlCondition() {
return "'" + this.value + "'";
}

@Override
public String getQlCondition() {
return this.value;
}

@Override
public String getValue() {
return value;
}
}


public class DoubleFieldValue extends FieldValue<Double> {
public DoubleFieldValue(Double value) {
this.value = value;
}

public DoubleFieldValue(String str){
this(Double.valueOf(str));
}

@Override
public String getSqlCondition() {
return String.valueOf(this.value);
}

@Override
public String getQlCondition() {
return String.valueOf(this.value);
}

@Override
public Double getValue() {
return this.value;
}
}


public class LongFieldValue extends FieldValue<Long> {
public LongFieldValue(Long value) {
this.value = value;
}

public LongFieldValue(String str){
this(Long.valueOf(str));
}

@Override
public String getSqlCondition() {
return String.valueOf(this.value);
}

@Override
public String getQlCondition() {
return String.valueOf(this.value);
}

@Override
public Long getValue() {
return this.value;
}

}

值类型对象解析器

/**
* 解析 fieldValue 值
*/
fun parseFieldValue(fieldCondition: FieldCondition, fieldValueType: KFieldValueType): List<FieldValue<*>?> {
val values = fieldCondition.values
if (values == null || values.isEmpty()) {
ExceptionHelper.bizError("illegal value size,values length must greater than 0.")
}

// 特征值类型
lateinit var clazz: Class<out FieldValue<*>>
when (fieldValueType) {
KFieldValueType.STRING -> clazz = StringFieldValue::class.java
KFieldValueType.LONG -> clazz = LongFieldValue::class.java
KFieldValueType.DOUBLE -> clazz = DoubleFieldValue::class.java
else -> ExceptionHelper.bizError("$fieldValueType fieldValueType not supported!")
}
return FieldValue.create(clazz, *values.toTypedArray())
}

定义表字段的值类型

public enum KFieldValueType {

/**
* string类型,字符类型统一抽象为string
*/
STRING("string"),

/**
* long类型,整数 & 布尔 统一抽象为long
*/
LONG("long"),

/**
* double类型,浮点统一抽象为double
*/
DOUBLE("double"),

/**
* map<string,string>
*/
MAP_STRING_STRING("map_string_string"),

/**
* map<string,long>
*/
MAP_STRING_LONG("map_string_long"),

/**
* map<string,double>
*/
MAP_STRING_DOUBLE("map_string_double"),

/**
* list<string>
*/
LIST_STRING("list_string"),


/**
* bitmap
*/
BITMAP("bitmap"),

;


private final String value;


KFieldValueType(String value) {
this.value = value;
}


public String getValue() {
return this.value;
}


/**
* 通过字符串获取标签选选项类型
*/
public static KFieldValueType getByValue(String value) {
if (value == null) {
return null;
}

for (KFieldValueType kFieldValueType : values()) {
if (kFieldValueType.value.equalsIgnoreCase(value)) {
return kFieldValueType;
}
}

return null;
}



/**
* 类型转化为hive值类型
*/
public String map2HiveColumnType() {
switch (this) {
case STRING:
return "string";
case LONG:
return "bigint";
case DOUBLE:
return "double";
case MAP_STRING_STRING:
return "map<string,string>";
case MAP_STRING_LONG:
return "map<string,bigint>";
case MAP_STRING_DOUBLE:
return "map<string,double>";
case LIST_STRING:
return "array<string>";
case BITMAP:
return "array<string>";
default:
throw new IllegalArgumentException("FIELD TYPE NOT SUPPORT");
}
}


/**
* 类型转化为ch值类型
*/
public String map2ChColumnType() {
switch (this) {
case STRING:
return "String";
case LONG:
return "Int64";
case DOUBLE:
return "Float64";
case MAP_STRING_STRING:
return "Map(String,String)";
case MAP_STRING_LONG:
return "Map(String,Int64)";
case MAP_STRING_DOUBLE:
return "Map(String,Float64)";
case LIST_STRING:
return "Array(String)";
case BITMAP:
return "BitMap64 BitEngineEncode";

default:
throw ExceptionHelper.bizError("No such field type in clickhouse");
}
}
}