Hive結構體系html
https://blog.csdn.net/zhoudaxia/article/details/8855937
能夠在hive的jdbc接口中使用getMetaData方法來獲取hive表的相關元信息java
statement = connection.createStatement(); DatabaseMetaData meta = connection.getMetaData();
參考git
https://blog.csdn.net/u010368839/article/details/76358831
hive metadata源碼解析能夠參考sql
https://cloud.tencent.com/developer/article/1330250
hive thrift接口能夠參考apache
http://www.laicar.com/book/echapter/5cb0bcfe739207662ac88ed1/links/x_Chapter_16.html/OEBPS/Text/part0024.xhtml
得到表的信息接口,指定tableNamePattern爲hive表名bash
ResultSet tableRet = meta.getTables(null, "%", "ads_nsh_trade", new String[]{"TABLE"}); while (tableRet.next()) { System.out.println("TABLE_CAT:" + tableRet.getString("TABLE_CAT")); System.out.println("TABLE_SCHEM:" + tableRet.getString("TABLE_SCHEM")); System.out.println("TABLE_NAME => " + tableRet.getString("TABLE_NAME")); System.out.println("table_type => " + tableRet.getString("table_type")); System.out.println("remarks => " + tableRet.getString("remarks")); System.out.println("type_cat => " + tableRet.getString("type_cat")); System.out.println("type_schem => " + tableRet.getString("type_schem")); System.out.println("type_name => " + tableRet.getString("type_name")); System.out.println("self_referencing_col_name => " + tableRet.getString("self_referencing_col_name")); System.out.println("ref_generation => " + tableRet.getString("ref_generation")); }
其中的參數能夠是.net
table_cat, table_schem, table_name, table_type, remarks, type_cat, type_schem, type_name, self_referencing_col_name, ref_generation
若是填寫不正確將會拋出異常htm
java.sql.SQLException: Could not find COLUMN_NAME in [table_cat, table_schem, table_name, table_type, remarks, type_cat, type_schem, type_name, self_referencing_col_name, ref_generation] at org.apache.hive.jdbc.HiveBaseResultSet.findColumn(HiveBaseResultSet.java:100) at org.apache.hive.jdbc.HiveBaseResultSet.getString(HiveBaseResultSet.java:541)
輸出的結果blog
TABLE_CAT: TABLE_SCHEM:tmp TABLE_NAME => ads_nsh_trade table_type => TABLE remarks => ??????????? type_cat => null type_schem => null type_name => null self_referencing_col_name => null ref_generation => null TABLE_CAT: TABLE_SCHEM:default TABLE_NAME => ads_nsh_trade table_type => TABLE remarks => null type_cat => null type_schem => null type_name => null self_referencing_col_name => null ref_generation => null
若是再指定schemaPattern爲hive庫名接口
ResultSet tableRet = meta.getTables(null, "default", "ads_nsh_trade", new String[]{"TABLE"}); while (tableRet.next()) { System.out.println("TABLE_CAT:" + tableRet.getString("TABLE_CAT")); System.out.println("TABLE_SCHEM:" + tableRet.getString("TABLE_SCHEM")); System.out.println("TABLE_NAME => " + tableRet.getString("TABLE_NAME")); System.out.println("table_type => " + tableRet.getString("table_type")); System.out.println("remarks => " + tableRet.getString("remarks")); System.out.println("type_cat => " + tableRet.getString("type_cat")); System.out.println("type_schem => " + tableRet.getString("type_schem")); System.out.println("type_name => " + tableRet.getString("type_name")); System.out.println("self_referencing_col_name => " + tableRet.getString("self_referencing_col_name")); System.out.println("ref_generation => " + tableRet.getString("ref_generation")); }
輸出結果
TABLE_CAT: TABLE_SCHEM:default TABLE_NAME => ads_nsh_trade table_type => TABLE remarks => null type_cat => null type_schem => null type_name => null self_referencing_col_name => null ref_generation => null
在hive的元數據表中,表的信息主要在TBLS和TABLE_PARAMS這兩張表中
參考
https://blog.csdn.net/haozhugogo/article/details/73274832
好比TBLS表
和TABLE_PARAMS表
得到表的字段信息的接口
ResultSet rs1 = meta.getColumns("default", "%", "ads_nsh_trade", "%"); while (rs1.next()) { String tableCat = rs1.getString("table_cat"); String tableSchem = rs1.getString("table_schem"); String tableName = rs1.getString("table_name"); String columnName = rs1.getString("COLUMN_NAME"); String columnType = rs1.getString("TYPE_NAME"); String remarks = rs1.getString("REMARKS"); int datasize = rs1.getInt("COLUMN_SIZE"); int digits = rs1.getInt("DECIMAL_DIGITS"); int nullable = rs1.getInt("NULLABLE"); System.out.println(tableCat + " " + tableSchem + " " + tableName + " " + columnName + " " + columnType + " " + datasize + " " + digits + " " + nullable + " " + remarks); }
其中的參數能夠是
table_cat, table_schem, table_name, column_name, data_type, type_name, column_size, buffer_length, decimal_digits, num_prec_radix, nullable, remarks, column_def, sql_data_type, sql_datetime_sub, char_octet_length, ordinal_position, is_nullable, scope_catalog, scope_schema, scope_table, source_data_type, is_auto_increment
輸出的結果
null default ads_nsh_trade test_string STRING 2147483647 0 1 string?????? null default ads_nsh_trade test_boolean BOOLEAN 0 0 1 boolean?????? null default ads_nsh_trade test_short SMALLINT 5 0 1 short?????? null default ads_nsh_trade test_double DOUBLE 15 15 1 double?????? null default ads_nsh_trade test_byte TINYINT 3 0 1 byte?????? null default ads_nsh_trade test_list array<string> 0 0 1 list<String>???? null default ads_nsh_trade test_map map<string,int> 0 0 1 map<String,Int>???? null default ads_nsh_trade test_int INT 10 0 1 int?????? null default ads_nsh_trade test_set array<bigint> 0 0 1 set<Long>?????? null default ads_nsh_trade col_name DECIMAL 10 2 1 null null default ads_nsh_trade col_name2 DECIMAL 10 2 1 null null default ads_nsh_trade test_long BIGINT 19 0 1 null null tmp ads_nsh_trade test_boolean BOOLEAN 0 0 1 boolean?????? null tmp ads_nsh_trade test_short SMALLINT 5 0 1 short?????? null tmp ads_nsh_trade test_double DOUBLE 15 15 1 double?????? null tmp ads_nsh_trade test_byte TINYINT 3 0 1 byte?????? null tmp ads_nsh_trade test_list array<string> 0 0 1 list<String>???? null tmp ads_nsh_trade test_map map<string,int> 0 0 1 map<String,Int>???? null tmp ads_nsh_trade test_int INT 10 0 1 int?????? null tmp ads_nsh_trade test_set array<bigint> 0 0 1 set<Long>?????? null tmp ads_nsh_trade test_long BIGINT 19 0 1 null null tmp ads_nsh_trade test_string STRING 2147483647 0 1 null