Home » Java » apache spark – Binding Hive table to Java Object-Exceptionshub

apache spark – Binding Hive table to Java Object-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to map an Hive table into a java object.
The idea is to do something similar to Hibernate does.

For example.
I have the following columns of the table:

| column_name1 | column_name2 |

and the java class to bind:

class Example implements Serializable{
      String columnName1;
      String columnName2;

// constructor, getters and setters
}

When I try to:

SparkSessionManager.sc()
                .sqlContext()
                .sql("SELECT * FROM tableName" )
                .as(Encoders.bean(Example.class));

It doesn’t work. It is trying to find column_name1 in the class.
Is there way I map the variable into a column name?

I know a workaround => I can put in the select select column_name1 As columnName1 ... and this works but it is not pretty.

I am using Spark version 2.3.0

Thanks!

How to&Answers: