1

我有一个用 SQL 服务器编写的递归 CTE,我需要迁移到 HIVE。由于 hive 不支持 CTE 中的递归,所以我被困在这里。建议我为此编写一个 java UDF,但我不是 java 专家。如果有人可以在这里提供帮助,我会很棒。

代码(SQL服务器):

            WITH CTE_CATEGORY_TREE AS 
            (
                                            SELECT CT.seq
                                                            , CT.[name]
                                                            , CT.pseq
                                                            , convert(varchar(255), CT.seq) AS [sort]
                                                            , convert(nvarchar(255), CT.[name]) AS [depth_fullname]
                                            FROM [dbo].[table1] AS CT WITH(NOLOCK)
                                            WHERE CT.pseq = 0 
                            UNION ALL 
                                            SELECT PC.seq
                                            , PC.[name]
                                            , PC.pseq
                                            , convert(varchar(255), convert(nvarchar,CT2.sort) + N' > ' + convert(varchar(255), PC.seq)) AS [sort]
                                            , convert(nvarchar(255), convert(nvarchar,CT2.depth_fullname) + N' > ' + convert(nvarchar(255), PC.[name])) AS [depth_fullname] 
                                            FROM [dbo].[table1] AS PC WITH(NOLOCK)
                                            JOIN CTE_CATEGORY_TREE AS CT2 ON PC.pseq = CT2.seq
            )

            SELECT          seq
                            , [name]
                            , pseq
                            , sort
                            , depth_fullname 
            FROM CTE_CATEGORY_TREE
4

0 回答 0