我的环境:hadoop 2.7.1、spark 1.6.0、hive 2.0、java 1.7
目标:通过java -jar xxx.jar的方式来运行提交spark应用,执行查询hive sql。
问题一:首先要提一下,按照java -jar执行,会报java.lang.OutOfMemoryError: PermGen space错误,所以需要使用以下参数启动
java -Xms1024m -Xmx1024m -XX:MaxNewSize=256m -XX:MaxPermSize=256m -jar spark.jar
问题二:如果不增加datanucleus的三个jar包,会报如下的错误
javax.jdo.JDOFatalUserException: Class org.datanucleus.api.jdo.JDOPersistenceManagerFactory was not found. at javax.jdo.JDOHelper.invokeGetPersistenceManagerFactoryOnImplementation(JDOHelper.java:1175) at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:808) at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:701) at org.apache.hadoop.hive.metastore.ObjectStore.getPMF(ObjectStore.java:365) at org.apache.hadoop.hive.metastore.ObjectStore.getPersistenceManager(ObjectStore.java:394) 。。。NestedThrowablesStackTrace:java.lang.ClassNotFoundException: org.datanucleus.api.jdo.JDOPersistenceManagerFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:274) at javax.jdo.JDOHelper$18.run(JDOHelper.java:2018) at javax.jdo.JDOHelper$18.run(JDOHelper.java:2016) at java.security.AccessController.doPrivileged(Native Method) at javax.jdo.JDOHelper.forName(JDOHelper.java:2015) at javax.jdo.JDOHelper.invokeGetPersistenceManagerFactoryOnImplementation(JDOHelper.java:1162) at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:808) at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:701) at org.apache.hadoop.hive.metastore.ObjectStore.getPMF(ObjectStore.java:365) at org.apache.hadoop.hive.metastore.ObjectStore.getPersistenceManager(ObjectStore.java:394) at org.apache.hadoop.hive.metastore.ObjectStore.initialize(ObjectStore.java:291) at org.apache.hadoop.hive.metastore.ObjectStore.setConf(ObjectStore.java:258) at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:73) at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:133) at org.apache.hadoop.hive.metastore.RawStoreProxy.(RawStoreProxy.java:57) at org.apache.hadoop.hive.metastore.RawStoreProxy.getProxy(RawStoreProxy.java:66) at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.newRawStore(HiveMetaStore.java:593) at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.getMS(HiveMetaStore.java:571) at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.createDefaultDB(HiveMetaStore.java:620) at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.init(HiveMetaStore.java:461) 。。。
问题三:java代码中SparkConf设置的master,即你选择的spark模式。我这里使用yarn-client模式,如果写yarn-cluster是会报错的。
1.如果你想把spark代码直接嵌入你的web app中,你需要使用yarn-client2.如果你想让你的spark代码足够松散耦合到yarn-cluster模式可以实际使用,你可以另起一个python的子线程来调用spark-submit来执行yarn-cluster模式。
问题四: 需要增加三个配置文件:core-site.xml、hdfs-site.xml、hive-site.xml。不然启动java -jar命令会直接报错。
所以,正确的java调用spark执行hive sql的代码如下:
创建java工程,引入spark-assembly-1.6.0-hadoop2.6.0.jar包。这个包在spark的安装目录的lib目录下有,178M,真的很大。
java调用代码如下,我的代码以后会打包为spark.jar,存放目录为/data/houxm/spark/spark.jar:
package cn.centaur.test.spark;import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaSparkContext;import org.apache.spark.sql.hive.HiveContext; public class SimpleDemo { public static void main(String[] args) { String[] jars = new String[]{"/data/houxm/spark/spark.jar"}; SparkConf conf = new SparkConf().setAppName("simpledemo").setMaster("yarn-client").set("executor-memory", "2g").setJars(jars).set("driver-class-path", "/data/spark/lib/mysql-connector-java-5.1.21.jar"); JavaSparkContext sc = new JavaSparkContext(conf); HiveContext hiveCtx = new HiveContext(sc); testHive(hiveCtx); sc.stop(); sc.close(); } //测试spark sql查询hive上面的表 public static void testHive(HiveContext hiveCtx) { hiveCtx.sql("create table temp_spark_java as select mobile,num from default.mobile_id_num02 limit 10"); }}
在java项目的根目录新建MANIFEST.MF文件,代码如下:
Manifest-Version: 1.0Class-Path: /data/spark/lib/spark-assembly-1.6.0-hadoop2.6.0.jar /data/spark/lib/mysql-connector-java-5.1.21.jar /data/spark/lib/datanucleus-api-jdo-3.2.6.jar /data/spark/lib/datanucleus-core-3.2.10.jar /data/spark/lib/datanucleus-rdbms-3.2.9.jarMain-Class: cn.centaur.test.spark.SimpleDemo
在resources目录(我的是maven工程,普通java工程在src下加入文件即可)下加入core-site.xml、hdfs-site.xml、hive-site.xml三个配置文件。
使用eclipse,按照此manifest文件把java代码打包。生成jar文件,上传至服务器,即可运行。