为什么spark schema的.simpleString()方法截断了我的输出?

voj3qocg  于 2023-06-30  发布在  Apache
关注(0)|答案(1)|浏览(191)

我有一个很长的模式,我想返回字符串

  1. import org.apache.spark.SparkConf;
  2. import org.apache.spark.sql.SparkSession;
  3. import org.apache.spark.sql.Dataset;
  4. import org.apache.spark.sql.Row;
  5. ...
  6. SparkSession spark = SparkSession.builder().config(new SparkConf().setAppName("YourApp").setMaster("local")).getOrCreate();
  7. Dataset<Row> parquetData = spark.read().parquet("/Users/demo/test.parquet");
  8. String schemaString = parquetData.schema().simpleString();

问题是生成的模式看起来像(见“10个字段”):

  1. struct<test:struct<countryConfidence:struct<value:double>,... 10 more fields> etc etc>

使用

  1. <dependency>
  2. <groupId>org.apache.spark</groupId>
  3. <artifactId>spark-sql_2.12</artifactId>
  4. <version>3.2.4</version>
  5. </dependency>

是否有一些配置选项可以使用,这意味着.simpleString不截断?我试过parquetData.schema().toDDL(),但它不能打印我需要的格式。

u3r8eeie

u3r8eeie1#

如果你深入了解simpleString方法,你可以看到Spark使用了truncatedString,其中SQLConf.get.maxToStringFields作为第三个参数传递。
此配置的定义如下所述:

  1. val MAX_TO_STRING_FIELDS = buildConf("spark.sql.debug.maxToStringFields")
  2. .doc("Maximum number of fields of sequence-like entries can be converted to strings " +
  3. "in debug output. Any elements beyond the limit will be dropped and replaced by a" +
  4. """ "... N more fields" placeholder.""")
  5. .version("3.0.0")
  6. .intConf
  7. .createWithDefault(25)

Solution
spark.sql.debug.maxToStringFields调整为大于25的数字,例如50(任意,但应根据您的用例确定),例如:

  1. SparkSession spark = SparkSession.builder()
  2. .appName("Spark app name")
  3. .master("local[*]")
  4. .config("spark.sql.debug.maxToStringFields", 50)
  5. .getOrCreate();

祝你好运!

展开查看全部

相关问题