首页 > 解决方案 > pyspark 数据框 printschema 到树视图以在 reactjs 树视图中显示 - Python

问题描述

我一直在努力将以下 pyspark 数据框 printschema 转换为树视图以显示在 reactjs 树视图中。我正在尝试在 Python 中执行此操作。

输入:(字符串类型 - 可以使用 python 逐行读取)

|-- a: struct (nullable = true)
|    |-- b: array (nullable = true)
|    |    |-- c: struct (containsNull = true)
|    |    |    |-- d: string (nullable = true)
|    |    |    |-- e: string (nullable = true)
|    |-- f: string (nullable = true)

我想使用python将上述字符串类型的树格式转换为以下json结构。如果将来在输入中添加或删除列,这需要是动态的。

预期输出:

[
    {
        "title": 'a',
        "key": 'a',
        "children": [
            {
                "title": 'b: array',
                "key": 'b: array',
                "children": [
                    {
                        "title": 'c: struct',
                        "key": 'c: struct',
                        "children": [
                            {
                                "title": 'd',
                                "key": 'd',
                            },
                            {
                                "title": 'e',
                                "key": 'e',
                            }
                        ]
                    }
                ],
            },
            {
                "title": 'f: array',
                "key": 'f: array',
            },
        ],
    },
]

我一直在尝试逐行读取 printschema 输出并构建上面的 json 模式。如果我得到上面的树 json,我可以将它们传递给 reactjs 并显示如下。

在此处输入图像描述

标签: pythonreactjsreact-nativeapache-sparkpyspark

解决方案


推荐阅读