[英]Convert Java object of scala.collection.Seq<String> to python list
In a pyspark sparkSession
, with sc
as my sparkContext
, calling the getRDDStorageInfo()
Java sparkContext, _jsc.sc()
, method returns a JavaObject instance of RDDInfo[]
that I can iterate over in python. 在pyspark
sparkSession
,以sc
作为我的sparkContext
,调用getRDDStorageInfo()
Java sparkContext _jsc.sc()
,该方法返回RDDInfo[]
的RDDInfo[]
实例,可以在python中进行迭代。
sc._jsc.sc().getRDDStorageInfo()
returns JavaObject id=o473
sc._jsc.sc().getRDDStorageInfo()
返回JavaObject id=o473
So the following works: 因此,以下工作:
for s in sc._jsc.sc().getRDDStorageInfo():
print s.name()
print s.memSize()
print s.numPartitions()
print s.numCachedPartitions()
However, the Java sparkContext method listFiles()
returns a JavaObject of scala.collection.Seq<String>
. 但是,Java sparkContext方法listFiles
listFiles()
返回scala.collection.Seq<String>
JavaObject。
sc._jsc.sc().listFiles()
returns JavaObject id=o475
sc._jsc.sc().listFiles()
返回JavaObject id=o475
and when I try and iterate over that I get TypeError: 'JavaObject' object is not iterable
当我尝试遍历时,我得到
TypeError: 'JavaObject' object is not iterable
How can I convert the JavaObject of scala.collection.Seq<String>
into a python list (or other python iterable)? 如何将
scala.collection.Seq<String>
的scala.collection.Seq<String>
转换为python列表(或其他可迭代的python)?
You can call scala methods size
and apply
to do this 您可以调用scala方法的
size
并apply
执行此操作
files = sc._jsc.sc().listFiles()
py_files = [files.apply(i) for i in range(files.size())]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.