lundi 1 avril 2019

HDFS Parquet file reader throwing DistributedFileSystem.class not found when run using java reflection

I am trying to read parquet file from remote HDFS file system using java. I used parquet-hadoop library for this.

This is how my code looks like,

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();
...

}

below are the maven dependencies i am using,

<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>

Also i tried adding 2 more dependecies, hadoop core and hadoop hdfs

When I run above Parquet reader code its working fine, issue I am facing is while I run as reflection.

I create a fat jar out of it and provide Class name along with jar to other program, which will run using reflection.

Reflection code looks like this,

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();



Type type = new TypeToken<Map<String, Object>>() {

        }.getType();
Map<String, Object> inputs = gson.fromJson(inputParamsString, type);

Method runMethod = obj.getClass().getDeclaredMethod("run", Map.class);

Object result = runMethod.invoke(obj, inputs);


When I run above code i am getting DistributedFileSystem.class not found at line, ParquetMetadata readFooter = ParquetFileReader.readFooter(conf, path, ParquetMetadataConverter.NO_FILTER);

I built fat jar, Verified jar contains the class org.apache.hdfs.DistributedFileSystem.class present in the jar.

Also I verified java -cp jarname.jar className.class is working as expected.

Below is my pom file,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>aaa</artifactId>
        <groupId>bbb</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>hdfs-parquet-reader</artifactId>

    <dependencies>
        <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>



    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>

                    <archive>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!--<finalName>${project.artifactId}-${project.version}-${maven.build.timestamp}</finalName> -->
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>


</project>


I tried with shaded plugin and build shaded jar, but problem remains same.

I heared that hadoop-commons library use Thread.currentThread.getClassLoader() to load the class files, seems like problems here.

Help me in fixing this,

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire