我正在嘗試使用Java從遠程HDFS文件系統讀取鑲木地板文件。我為此使用了鑲木地板庫。這就是我的代碼的樣子,public Map run( Map inputs ){... final Configuration conf = new Configuration(); conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName()); conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName()); conf.set("fs.defaultFS", "hdfs://" + connHostName + ":" + connPort); conf.set("ipc.client.connect.timeout", "10000"); conf.set("ipc.client.connect.max.retries.on.timeouts", "3"); System.setProperty("hadoop.home.dir", "/"); Path path = new Path(filePath); ParquetMetadata readFooter = ParquetFileReader.readFooter(conf, path, ParquetMetadataConverter.NO_FILTER); MessageType schema = readFooter.getFileMetaData().getSchema();...}以下是我正在使用的專家依賴項,<dependency> <groupId>org.apache.parquet</groupId> <artifactId>parquet-hadoop</artifactId> <version>1.9.0</version></dependency><dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.1.0</version></dependency>另外,我嘗試添加2個依賴,hadoop核心和hadoop hdfs當我在上面運行鑲木地板閱讀器代碼時,它的工作正常,我面臨的問題是當我作為反射運行時。我用它創建了一個胖罐,并向其他程序提供類名以及jar,該程序將使用反射運行。反射代碼如下所示,String packageName = "com.mycompany.hdfs.parquet.Parquet";String jarPath = "/Users/.../hdfs-parquet-reader/target/hdfs-parquet-reader-0.0.1-jar-with-dependencies.jar";ClassLoader child = new URLClassLoader(new URL[] { new URL("file://" + jarPath)}, ClassLoader.getSystemClassLoader());Class classToLoad = Class.forName(packageName, true, child);String inputParamsString = "{}";Object obj = classToLoad.newInstance();當我運行上面的代碼時,我得到分布式文件系統.class找不到在行,ParquetMetadata readFooter = ParquetFileReader.readFooter(conf, path, ParquetMetadataConverter.NO_FILTER);我構建了胖罐,驗證的罐子包含類組織.apache.hdfs.分布式文件系統.class存在于罐子中。另外,我驗證了java -cp.jar類名.class是否按預期工作。
1 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
我自己找到了解決這個問題的方法,
問題出在這里
ClassLoader child = new URLClassLoader(new URL[] { new URL("file://" + jarPath)}, ClassLoader.getSystemClassLoader());
在這里,我正在加載系統類加載器,這導致從最終的類路徑中刪除依賴庫,
我把它改成了
ClassLoader child = new URLClassLoader(new URL[] { new URL("file://" + jarPath)}, Thread.currentThread().getContextClassLoader());
這對我有用。
添加回答
舉報
0/150
提交
取消