hive表字段跟字段对应的值转为json数组

第一种方式 直接用hive 函数实现

复制代码
select collect_list(named_struct('id',id,'name',name)) from table  

此方式不适用于字段数量过多的情况(比较麻烦)

第二种方式 写udf 函数

复制代码
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.metadata.Table;
import org.apache.hadoop.hive.ql.metadata.TableNotFoundException;
import org.json.JSONArray;
import org.json.JSONObject;

@Description(
    name = "table_to_json_array",
    value = "Converts data from a Hive table to a JSON array.",
    extended = "Example:\n" +
            "  SELECT table_to_json_array('your_table') AS json_array FROM your_table;"
)
public class TableToJsonArray extends UDF {

    public String evaluate(String tableName) {
        try {
            // 获取 Hive 表对象
            Table table = getTable(tableName);

            // 获取表的结构(字段名)
            String[] fieldNames = table.getAllCols().stream().map(column -> column.getName()).toArray(String[]::new);

            // 构建查询
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.append("SELECT ");

            for (int i = 0; i < fieldNames.length; i++) {
                queryBuilder.append("named_struct('")
                            .append(fieldNames[i])
                            .append("', ")
                            .append(fieldNames[i])
                            .append(")");

                if (i < fieldNames.length - 1) {
                    queryBuilder.append(", ");
                }
            }

            queryBuilder.append(" FROM ")
                        .append(tableName);

            // 执行查询
            String query = queryBuilder.toString();
            JSONArray jsonArray = executeQuery(query);

            // 返回 JSON 数组
            return jsonArray.toString();
        } catch (TableNotFoundException e) {
            // 处理表不存在的情况
            return null;
        }
    }

    // 获取 Hive 表对象
    private Table getTable(String tableName) throws TableNotFoundException {
        // 使用 Hive 元数据获取表对象
        // 这里需要适应你的环境和需求来获取表对象
        // 示例代码省略了实际获取表对象的细节
        throw new TableNotFoundException("Table not found: " + tableName);
    }

    // 执行查询并返回结果
    private JSONArray executeQuery(String query) {
        // 在这里执行查询并返回结果的代码,可以使用 Hive 的 JDBC 驱动程序或其他适当的方式执行查询
        // 返回的结果应该是一个 JSON 数组
        // 示例代码省略了实际查询和结果处理的细节
        return new JSONArray(); // 返回空数组作为示例
    }
}

此方式不适用于获取表种某几个字段及字段对应的值的情况

相关推荐
ID_180079054731 天前
闲鱼商品详情API接口基础架构解析
json
Justice Young1 天前
Hive第四章:HIVE Operators and Functions
大数据·数据仓库·hive·hadoop
LF3_1 天前
hive,Relative path in absolute URI: ${system:user.name%7D 解决
数据仓库·hive·hadoop
wtsolutions1 天前
Sheet-to-Doc数据格式支持:JSON/JSONL/CSV全解析
json
德彪稳坐倒骑驴1 天前
Hive SQL常遗忘的命令
hive·hadoop·sql
Justice Young1 天前
Hive第六章:Hive Optimization and Miscellaneous
数据仓库·hive·hadoop
Justice Young1 天前
Hive第五章:Integeration with HBase
大数据·数据仓库·hive·hbase
Justice Young1 天前
Hive第三章:HQL的使用
大数据·数据仓库·hive·hadoop
AC赳赳老秦2 天前
Python 爬虫进阶:DeepSeek 优化反爬策略与动态数据解析逻辑
开发语言·hadoop·spring boot·爬虫·python·postgresql·deepseek
zgl_200537792 天前
ZGLanguage 解析SQL数据血缘 之 标识提取SQL语句中的目标表
java·大数据·数据库·数据仓库·hadoop·sql·源代码管理