如何在DataFrame中保持键值顺序与JSON相同? [英] How to maintain order of key-value in DataFrame same as JSON?

查看:149
本文介绍了如何在DataFrame中保持键值顺序与JSON相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例JSON数据:

{"name": "dev","salary": 100,"occupation": "engg","address": "noida"}
{"name": "karthik","salary": 200,"occupation": "engg","address": "blore"}

Spark Java代码:

Spark Java code:

DataFrame df = sqlContext.read().json(jsonPath);
df.printSchema();
df.show(false);

输出:

root
 |-- address: string (nullable = true)
 |-- name: string (nullable = true)
 |-- occupation: string (nullable = true)
 |-- salary: long (nullable = true)


+-------+-------+----------+------+
|address|name   |occupation|salary|
+-------+-------+----------+------+
|noida  |dev    |engg      |10000 |
|blore  |karthik|engg      |20000 |
+-------+-------+----------+------+

各列按字母顺序排列. 有什么方法可以维持自然秩序?

Columns are arranged in the alphabetical order. Is there any way to maintain natural order?

推荐答案

您可以在阅读json的同时提供schema,它会保持顺序.

You can provide schema while reading the json and it will maintain the order.

StructType schema = DataTypes.createStructType(new StructField[] { 
    DataTypes.createStructField("name", DataTypes.StringType, true),
    DataTypes.createStructField("salary", DataTypes.IntegerType, true),
    DataTypes.createStructField("occupation", DataTypes.StringType, true),
    DataTypes.createStructField("address", DataTypes.StringType, true)});

DataFrame df = sqlContext.read().schema(schema).json(jsonPath);
df.printSchema();
df.show(false);

这篇关于如何在DataFrame中保持键值顺序与JSON相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆