简体   繁体   中英

Get Hive column names without table name in JDBC ResultSet metadata

I'm trying to get the name of the columns from the metadata of a ResultSet but in Hive I get column names if the form of table_name.column_name and I'd like to have only column_name (similarly to what I get from MySQL for example). Is it possible ?

Yes it is possible. There are two ways by which it can be done:

1.) Using Hive JDBC connection(which you are looking for).

2.) Using HiveMetastoreClient.

Here are code snippets for the above two approaches:

First Approach:

ResultSet resultSet = <custom_class_for_hive_jdbc_connector>.executeQuery("DESCRIBE <TABLE_NAME>");
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {

System.out.println(" Column names : "+resultSet.getString(metaData.getColumnName(1)));

}

In this approach we need to create a resultsetmetadata object and using this object we can get the details of the query output. In your case "describe table"

Second Approach:

HiveMetaStoreClient metastoreClient = null;
HiveConf hiveConf = new HiveConf();         
metastoreClient = new HiveMetaStoreClient(hiveConf);
List<FieldSchema> list = metastoreClient.getFields(<database_name>,<table_name>);   
for (int i = 0; i < list.size(); i++) {
    System.out.println("Column names : "+list.get(i).getName());        
}                   
metastoreClient.close();

In this approach we are not running any query, we are simply connecting to the HiveMetastore and from there we are getting the details of a given table under given database.

Hope it helps...!!!

To prevent the Hive table names from being prepended to the column names , use this property setting in the Hive-site.xml file.

 <property>
   <name>hive.resultset.use.unique.column.names</name>
   <value>false</value>
 </property>

beginning with Hive .13, a new Hive property called hive.resultset.use.unique.column.names was created. The default value of this property is TRUE.

If the default value is used, the Hive table name is prepended to all column names. This is a change in behavior from previous versions of Hive.

This is also very important if you are trying to use Spring Jdbctemplate with Beanpropertyrowmapper to do the camel case to underscore format conversion for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM