spark 开发环境读取mysql

55 篇文章 3 订阅

问题导读
1、idea上运行local的spark sql hive流程是怎样的?
2、如何安装配置安装 remote metastore?
3、如何解决org.apache.spark的问题?





在本机上通过idea跑spark sql进行hive查询等操作,一方面可以用于debug spark sql相关源码,另一方面可以加快开发测试进度,比如添加Udf等。
这里总共两步:1、安装hive remote metastore模式 2、idea上的相关配置开发

hive 0.13安装 remote metastore
服务器test01作为remote端,test02作为client端, test02也需要有hadooop环境(和test01一样)
1、下载hive 0.13并解压
2、test01上配置hive-site.xml,加入mysql相关 (包括将mysql connect包放到lib中)

  1. <?xml version="1.0"?>  
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  

  3. <configuration>  

  4. <property>  
  5.   <name>hive.metastore.warehouse.dir</name>  
  6.   <value>/user/hive/warehouse</value>  
  7. </property>  

  8. <property>  
  9.   <name>javax.jdo.option.ConnectionURL</name>  
  10.   <value>jdbc:mysql://test01:3306/hive?createDatabaseIfNotExist=true</value>  
  11. </property>  

  12. <property>  
  13.   <name>javax.jdo.option.ConnectionDriverName</name>  
  14.   <value>com.mysql.jdbc.Driver</value>  
  15. </property>  

  16. <property>  
  17.   <name>javax.jdo.option.ConnectionUserName</name>  
  18.   <value>hive</value>  
  19. </property>  

  20. <property>  
  21.   <name>javax.jdo.option.ConnectionPassword</name>  
  22.   <value>password</value>  
  23. </property>
复制代码



3、启动test01上的metastore server: nohup hive - -service metastore> metastore.log 2>&1 &
默认端口为9083
4、配置test02上的hive-site.xml

  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <configuration>
  4.     <property>
  5.         <name>hive.metastore.warehouse.dir</name>
  6.         <value>/user/hive/warehouse</value>
  7.     </property>

  8.     <property>
  9.         <name>hive.metastore.local</name>
  10.         <value>false</value>
  11.     </property>

  12.     <property>
  13.         <name>hive.metastore.uris</name>
  14.         <value>thrift://test01:9083</value>
  15.     </property>
  16. </configuration>
复制代码



这样test02上通过hive就可以访问test01上的了

idea上运行local的spark sql
前提:本机上的hive client可以使用, 同时我这里用了hadoop2.2版本
我使用的是maven 工程, pom.xml如下:

  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.apache.spark</groupId>
  4.         <artifactId>spark-core_2.10</artifactId>
  5.         <version>1.1.0</version>

  6.         <exclusions>
  7.             <exclusion>
  8.             <!--默认是1.0.4版本,要去掉-->
  9.                 <groupId>org.apache.hadoop</groupId>
  10.                 <artifactId>hadoop-client</artifactId>
  11.             </exclusion>
  12.         </exclusions>
  13.     </dependency>
  14.     <!-- 引入和hadoop集群相同版本的-->
  15.     <dependency>
  16.         <groupId>org.apache.hadoop</groupId>
  17.         <artifactId>hadoop-client</artifactId>
  18.         <version>2.2.0</version>
  19.     </dependency>
  20.     <dependency>
  21.         <groupId>org.apache.spark</groupId>
  22.         <artifactId>spark-mllib_2.10</artifactId>
  23.         <version>1.1.0</version>
  24.     </dependency>
  25.     <dependency>
  26.         <groupId>org.apache.spark</groupId>
  27.         <artifactId>spark-sql_2.10</artifactId>
  28.         <version>1.1.0</version>
  29.     </dependency>
  30.     <dependency>
  31.         <groupId>org.apache.spark</groupId>
  32.         <artifactId>spark-hive_2.10</artifactId>
  33.         <version>1.1.0</version>
  34.     </dependency>
  35. </dependencies>

  36. <build>
  37.     <plugins>
  38.         <plugin>
  39.             <groupId>org.apache.maven.plugins</groupId>
  40.             <artifactId>maven-source-plugin</artifactId>
  41.             <version>2.2.1</version>
  42.             <executions>
  43.                 <execution>
  44.                     <id>attach-sources</id>
  45.                     <phase>package</phase>
  46.                     <goals>
  47.                         <goal>jar</goal>
  48.                     </goals>
  49.                 </execution>
  50.             </executions>
  51.         </plugin>
  52.         <plugin>
  53.             <groupId>net.alchim31.maven</groupId>
  54.             <artifactId>scala-maven-plugin</artifactId>
  55.             <version>3.1.0</version>
  56.             <executions>
  57.                 <execution>
  58.                     <id>compile-scala</id>
  59.                     <phase>compile</phase>
  60.                     <goals>
  61.                         <goal>add-source</goal>
  62.                         <goal>compile</goal>
  63.                     </goals>
  64.                 </execution>
  65.                 <execution>
  66.                     <id>test-compile-scala</id>
  67.                     <phase>test-compile</phase>
  68.                     <goals>
  69.                         <goal>add-source</goal>
  70.                         <goal>testCompile</goal>
  71.                     </goals>
  72.                 </execution>
  73.             </executions>
  74.         </plugin>
  75.     </plugins>
  76. </build>
复制代码



将hive client的hive-site.xml放到resources目录下,这样就可以访问远程的hive metadata了。

Test程序:

  1. object Test{
  2.   def main(args:Array[String]): Unit = {
  3.     val sc = new SparkContext("local", "test")
  4.     val hiveContext = new HiveContext(sc)
  5.     val rdd = hiveContext.sql("select * from test")
  6.     rdd.collect().foreach(println)
  7.   }
  8. }
复制代码



运行时有可能报:

  1. Exception in thread "main" org.apache.hadoop.ipc.RemoteException: Server IPC version 9 cannot communicate with client version 4
复制代码



这是因为org.apache.spark默认使用的是hadoop-client-1.0.4, 而我的hadoop是2.2.0, 所以版本不一致,解决方法是将hadoop-client-1.0.4改成hadoop-2.2.0, 解决方法已经在上面的pom.xml中加入了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值