[英]Storing data as hive table in Apache Spark using Java
I am doing one RND where i want to store my RDD to hive table. 我正在做一个RND,我要将RDD存储到配置单元表中。 I have wirtten the code in Java and creating the RDD.
我已经用Java编写了代码并创建了RDD。 After converting the RDD i am converting it to Data Frame and then store it in Hive table.
转换RDD后,我将其转换为数据帧,然后将其存储在Hive表中。 But here i am facing two kind of different errors.
但是这里我面临两种不同的错误。
public static void main(String[] args) {
SparkConf sparkConf = new SparkConf().setAppName("SparkMain");
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
HiveContext hiveContext = new HiveContext(ctx.sc());
hiveContext.setConf("hive.metastore.uris", "thrift://address:port");
DataFrame df = hiveContext.read().text("/filepath");
df.write().saveAsTable("catAcctData");
df.registerTempTable("catAcctData");
DataFrame sql = hiveContext.sql("select * from catAcctData");
sql.show();
ctx.close();
} }
If i am executing this program, it is working perfectly fine. 如果我正在执行此程序,则工作正常。 I can see the table data in console.
我可以在控制台中看到表数据。
But if i try below code it is saying org.apache.spark.sql.AnalysisException: Table not found: java 但是,如果我尝试下面的代码,则是说org.apache.spark.sql.AnalysisException:未找到表:java
public static void main(String[] args) {
SparkConf sparkConf = new SparkConf().setAppName("SparkMain");
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
HiveContext hiveContext = new HiveContext(ctx.sc());
hiveContext.setConf("hive.metastore.uris", "thrift://address:port");
DataFrame sql = hiveContext.sql("select * from catAcctData");
sql.show();
ctx.close();
} }
And if i try to save the table data using sqlContext it is saying java.lang.RuntimeException: Tables created with SQLContext must be TEMPORARY. 而且,如果我尝试使用sqlContext保存表数据,那就是说java.lang.RuntimeException:用SQLContext创建的表必须是TEMPORARY。 Use a HiveContext instead.
请改用HiveContext。
public static void main(String[] args) {
SparkConf sparkConf = new SparkConf().setAppName("SparkMain");
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
SQLContext hiveContext = new SQLContext(ctx.sc());
hiveContext.setConf("hive.metastore.uris", "thrift://address:port");
DataFrame df = hiveContext.read().text("/filepath");
df.write().saveAsTable("catAcctData");
df.registerTempTable("catAcctData");
DataFrame sql = hiveContext.sql("select * from catAcctData");
sql.show();
ctx.close();
} }
I am bit confuse here. 我在这里有点困惑。 Please solve my query.
请解决我的查询。
Regards, Pratik 问候,Pratik
Your problem is that you create your table using different HiveContext. 您的问题是您使用其他HiveContext创建表。 In other words, HiveContext from the second program doesn't see "catAcctData" table because you've created this table with another HiveContext.
换句话说,第二个程序中的HiveContext看不到“ catAcctData”表,因为您已经用另一个HiveContext创建了该表。 Use one HiveContext for creating and reading tables.
使用一个HiveContext创建和读取表。
Also I don't understand why you do this df.write().saveAsTable("catAcctData");
我也不明白为什么要这么做
df.write().saveAsTable("catAcctData");
before creating temporary table. 在创建临时表之前。 If you want to create temporary table you just need to use
df.registerTempTable("catAcctData");
如果要创建临时表,只需使用
df.registerTempTable("catAcctData");
without df.write().saveAsTable("catAcctData");
没有
df.write().saveAsTable("catAcctData");
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.