[已解决]Windows 下使用pyflink出现PyFlink module java.base does not "opens java.lang" to unnamed module
问题复现
最近在用flink在windows环境下敲代码,但是遇到了如下报错:
Traceback (most recent call last):
File "D:\pythonProject\kafkatest\test.py", line 5, in <module>
ds.print()
File "D:\pythonProject\kafkatest\venv\lib\site-packages\pyflink\datastream\data_stream.py", line 883, in print
j_data_stream_sink = self._align_output_type()._j_data_stream.print()
File "D:\pythonProject\kafkatest\venv\lib\site-packages\py4j\java_gateway.py", line 1322, in __call__
return_value = get_return_value(
File "D:\pythonProject\kafkatest\venv\lib\site-packages\pyflink\util\exceptions.py", line 146, in deco
return f(*a, **kw)
File "D:\pythonProject\kafkatest\venv\lib\site-packages\py4j\protocol.py", line 326, in get_return_value
raise Py4JJavaError(
py4j.protocol.Py4JJavaError: An error occurred while calling o59.print.
: java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module @799d4f69
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:180)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:174)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:106)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:69)
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.clean(StreamExecutionEnvironment.java:2317)
at org.apache.flink.streaming.api.datastream.DataStream.clean(DataStream.java:202)
at org.apache.flink.streaming.api.datastream.DataStream.addSink(DataStream.java:1244)
at org.apache.flink.streaming.api.datastream.DataStream.print(DataStream.java:937)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at org.apache.flink.api.python.shaded.py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at org.apache.flink.api.python.shaded.py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:374)
at org.apache.flink.api.python.shaded.py4j.Gateway.invoke(Gateway.java:282)
at org.apache.flink.api.python.shaded.py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at org.apache.flink.api.python.shaded.py4j.commands.CallCommand.execute(CallCommand.java:79)
at org.apache.flink.api.python.shaded.py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.base/java.lang.Thread.run(Thread.java:833)
为了排查问题,我用了一段简单的代码:
from pyflink import datastream
env=datastream.StreamExecutionEnvironment.get_execution_environment()
env.set_parallelism(1)
ds=env.from_collection([1,2,3,4,5,6,7,8,9,10])
ds.print()
env.close()
其实如果没有:
ds.print()
这一行,其实程序是能够正常退出的,然而有了这一行之后,就会报上面提到的错.
报错原因
Flink doesn’t support JDK 19, only 8 and 11.
Flink不支持JDK 19,只支持8和11,所以我们需要使用jdk8
着手解决
找到 jdk1.8.安装好之后,由于笔者不希望改变当前的环境变量,所以找到了pyflink的配置文件夹(一般是\venv\Lib\site-packages\pyflink\conf)
打开flink-conf.yaml ,把JDK1.8路径写入:
env.java.home: E:\java\jdk-1.8
保存之后再运行程序即可: