Java 反射由實體類生產hive或mysql表對應欄位
阿新 • • 發佈:2019-02-09
如果一個實體類欄位特別多,那麼建立對應的RDBMS對應的表會讓我感到抓狂,可以利用Java反射特性,生產對應欄位
1.建立一個藉口,定義一些常量
public interface Constants {
/**引數預設值*/
public static final String DEFAULT_PARAM_VAL = "";
/**中文字符集*/
public static final String CHAR_UTF8 = "utf-8";
/**資料預設分隔符*/
public static final String DEFAULT_SEPAREATOR = "|" ;
/**欄位預設分隔符*/
public static final String DEFAULT_FIELDS_SEPAREATOR = ",";
/**空格字串*/
public static final String BLANK_SPACE = " ";
/**換行字元*/
public static final String LINE_FEED = "\n";
/**製表符字元*/
public static final String TAB = "\t";
}
2.建立一個列舉類,定義一些常用語句 (可以根據,你自己的需求定義)
/**
*
* @author puppy
* @data 2015-04-02
*
*/
public enum HiveTableSyntax {
external("CREATE EXTERNAL TABLE ","external"),inner("CREATE TABLE ","inner"),
defalut("PARTITIONED BY(logdate String) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' ","default"),
sequence("PARTITIONED BY(logdate String) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS SequenceFile " ,"sequence");
private String value;
private String index;
private HiveTableSyntax() {
}
private HiveTableSyntax(String value) {
this.value=value;
}
private HiveTableSyntax(String value,String index)
{
this.value=value;
this.index=index;
}
public static String getValue(String index){
for(HiveTableSyntax hts:HiveTableSyntax.values()){
if(hts.getIndex().equalsIgnoreCase(index)){
return hts.getValue();
}
}
return null;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
}
3.建立生成表的類 (我這裡只寫了生成hive表,對應其他 請自己定義)
/**
*
* @author puppy
* @date 2015-04-01
*
*/
public class AutoCreateFields {
// 生成欄位
public static String create(Class<?> clazz) {
Field[] fs = clazz.getDeclaredFields();
Class<?> superClazz = clazz.getSuperclass();
if (superClazz != null) {
Field[] superFs = superClazz.getDeclaredFields();
fs = (Field[]) ArrayUtils.addAll(fs, superFs);
}
StringBuffer sb = new StringBuffer();
sb.append(Constants.LINE_FEED);
sb.append("(");
int size = fs.length;
for (int i = 0; i < size; i++) {
Field field = fs[i];
field.setAccessible(true);
sb.append(field.getName());
sb.append(Constants.BLANK_SPACE);
sb.append(field.getType().getSimpleName().equalsIgnoreCase("Integer")?"int":field.getType().getSimpleName());
if (i + 1 != size) {
sb.append(Constants.DEFAULT_FIELDS_SEPAREATOR);
sb.append(Constants.LINE_FEED);
}
}
sb.append(")");
sb.append(Constants.LINE_FEED);
return sb.toString();
}
//生成hive表
public static String createHiveTable(Class<?> clazz,String tblname,String externalOrInner,String storeTpye){
StringBuilder sb=new StringBuilder();
String fields=create(clazz);
String create=HiveTableSyntax.getValue(externalOrInner);
String st=HiveTableSyntax.getValue(storeTpye);
sb.append(create);
sb.append(tblname);
sb.append(fields);
sb.append(st);
return sb.toString();
}
public static void main(String[] args) {
String result="Need Params:aqy or partner or v1 or default";
if(args.length>0&&StringUtils.isNotBlank(args[0])){
String value=args[0];
switch (value){
case "aqy":
result=createHiveTable(AQYLogOriginal.class,"qiyi_log_original","external","sequence");
break;
case "partner":
result=createHiveTable(PartnerLogOriginal.class,"partner_log_original","external","sequence");
break;
case "v1":
result=createHiveTable(V1VodLogOriginal.class,"v1_log_original","external","sequence");
break;
default:
result=createHiveTable(V1VodLogOriginal.class,"v0v1_log_original","external","sequence");
break;
}
}
//String aqy = createHiveTable(AQYLogOriginal.class,"qiyi_log_original","external","sequence");
//String partner = createHiveTable(PartnerLogOriginal.class,"partner_log_original","external","sequence");
//String v1 = createHiveTable(V1VodLogOriginal.class,"v0v1_log_original","inner","sequence");
System.out.println(result);
}