简体   繁体   English

使用JDBC从Hive表中选择数组字段

[英]Select array fields from Hive table using JDBC

I have a Hive table which has column with array data type. 我有一个Hive表,其中的列具有数组数据类型。 I am using JDBC to select rows from the table. 我正在使用JDBC从表中选择行。

SELECT col1 FROM hive_table WHERE condition = 'condition'

After receiving the resultset, I am using res.getArray() method for the specific array field while looping through resultset. 收到结果集后,我在遍历结果集的同时对特定的数组字段使用res.getArray()方法。

Array arrayCol = res.getArray(1);

This is throwing a "Method not supported" error. 这将引发“不支持方法”错误。 Is it valid to use getArray() method for such queries executed on Hive table? 将getArray()方法用于在Hive表上执行的此类查询是否有效?

Unfortunately, no. 抱歉不行。 You can see getArray() method is not implemented in ResultSet class for Hive JDBC. 您可以看到Hive JDBC的ResultSet类中未实现getArray()方法。 The actual class name is HiveBaseResultSet and the source code is located here . 实际的类名称为HiveBaseResultSet,源代码位于此处

Depends on what type of values the array holds, a client program need to decode its value by itself. 取决于数组所保存的值的类型,客户端程序需要自行解码其值。 For example, a column of type array<string> is encoded as a single String object like `["VALUE1","VALUE2",...,"VALUEN"]'. 例如,类型为array <string>的列被编码为单个String对象,例如“ [“ VALUE1”,“ VALUE2”,...,“ VALUEN”]“。 And we can use getString() method and freely re-construct any object of type Array<String> or List<String>. 我们可以使用getString()方法并自由地重建Array <String>或List <String>类型的任何对象。

You can loop though the result set and add the column values to arraylist in java. 您可以遍历结果集并将列值添加到Java中的arraylist中。 See example below assuming that your table column is of String type. 假设您的表列为String类型,请参见下面的示例。

List<String> list = new ArrayList<String>();

     while (res.next()) {
                      list.add( res.getString(1));
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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