'Converting to string type in py4j causes AttributeError on _detach
I've got a type WriteableFileName that "acts like a string" in python. I can convert it to a string in java using this custom converter:
class WriteableFileNameConverter(object):
def can_convert(self, object):
return isinstance(object, WriteableFileName)
def convert(self, object, gateway_client):
str_representation = str(object)
String = JavaClass("java.lang.String", gateway_client)
ret_val = String(str(str_representation))
return ret_val
When I use that converter, I can execute something like df.write.format('csv').save(my_filename) and lazy eval my_filename to a file path. After save is done, the converted type fails with this error:
File "C:\Users\riguy\Anaconda3\envs\work37\lib\site-packages\pyspark\sql\readwriter.py", line 739, in save
self._jwrite.save(path)
File "C:\Users\riguy\Anaconda3\envs\work37\lib\site-packages\py4j\java_gateway.py", line 1260, in __call__
temp_arg._detach()
AttributeError: 'str' object has no attribute '_detach'
Weirdly (for me!) type(ret_val) above is still 'str' but it has a different id than str_representation.
So the question is (1) what's the detach error, and also (2) why is ret_val a 'str' type not a JavaClass?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
