apache-spark - 如何在 spark scala 中计算数据框的大小
问题描述
我想编写一个带有重新分区的大型数据帧,所以我想计算我的源数据帧的重新分区数。
numberofpartition= {数据帧大小/default_blocksize}
所以请告诉我如何在 spark scala 中计算数据框的大小
提前致谢。
解决方案
一旦加载到内存中,我们就可以获取实际Dataframespark.sessionState.executePlan(df.queryExecution.logical).optimizedPlan.stats(spark.sessionState.conf).sizeInBytes
的大小,例如,您可以查看下面的代码。
scala> val df = spark.read.format("orc").load("/tmp/srinivas/")
df: org.apache.spark.sql.DataFrame = [channelGrouping: string, clientId: string ... 75 more fields]
scala> import org.apache.commons.io.FileUtils
import org.apache.commons.io.FileUtils
scala> val bytes = spark.sessionState.executePlan(df.queryExecution.logical).optimizedPlan.stats(spark.sessionState.conf).sizeInBytes
bytes: BigInt = 763275709
scala> FileUtils.byteCountToDisplaySize(bytes.toLong)
res5: String = 727 MB
scala> import sys.process._
import sys.process._
scala> "hdfs dfs -ls -h /tmp/srinivas/".!
Found 2 items
-rw-r----- 3 svcmxns hdfs 0 2020-04-20 01:46 /tmp/srinivas/_SUCCESS
-rw-r----- 3 svcmxns hdfs 727.4 M 2020-04-20 01:46 /tmp/srinivas/part-00000-9d0b72ea-f617-4092-ae27-d36400c17917-c000.snappy.orc
res6: Int = 0
val bytes = spark.sessionState.executePlan(df.queryExecution.logical).optimizedPlan.stats(spark.sessionState.conf).sizeInBytes
val dataSize = bytes.toLong
val numPartitions = (bytes.toLong./(1024.0)./(1024.0)./(10240)).ceil.toInt // May be you can change or modify this to get required partitions.
df.repartition(if(numPartitions == 0) 1 else numPartitions)
.[...]
Edit - 1
:请根据您的火花版本使用以下逻辑。
火花2.4
val bytes = spark.sessionState.executePlan(df.queryExecution.logical).optimizedPlan.stats(spark.sessionState.conf).sizeInBytes
火花2.3
val bytes = spark.sessionState.executePlan(df.queryExecution.logical).optimizedPlan.stats.sizeInBytes
对于 Python
spark._jsparkSession.sessionState().executePlan(df._jdf.queryExecution().logical()).optimizedPlan().stats().sizeInBytes()
推荐阅读
- javascript - 无法解构“(中间值)”的属性“数据”,因为它未定义
- css - 将 flex 对齐到行的底部
- sas - 计数器变量不打算重置
- python - 使用 MDAnalysis 获得径向分布函数
- javascript - Vue 和 Quasar - 模块 '"../../node_modules/vue/types"' 没有导出的成员 'defineComponent'
- matlab - 通过单击点图中的某些点打开子图(MATLAB)
- java - Android View layoutInflater 通过字符串获取布局
- excel - VBA或公式在Excel中分隔街道名称和后缀?
- python - 为什么在 django 中返回 json 响应后值会改变
- python - 我可以在 kivy 2d gui 中预览终端 bash 输出吗