分享

spring-data-hadoop使用--maven配置详解

howtodown 发表于 2015-3-22 20:35:52 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 26894


导读
本文主要是pom文件的条用,
明白配置完毕,如何调用即可?






这几天一直在纠结spring-data-hadoop的JAR包,总是运行不成功,现在终于可以运行了,pom内容为:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.         <modelVersion>4.0.0</modelVersion>
  4.         <groupId>winksi.com.cn</groupId>
  5.         <artifactId>hadoopTest</artifactId>
  6.         <version>0.0.1-SNAPSHOT</version>
  7.         <!-- 添加镜像依赖 ,在maven的官方库里是没有CDH相关包的,所以只能配置CDH的官网下载地址来下载jar包-->
  8.         <repositories>
  9.                 <repository>
  10.                         <id>cloudera</id>
  11.                         <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
  12.                 </repository>
  13.                 <repository>
  14.                         <id>spring-repo</id>
  15.                         <url>http://repo.springsource.org/libs-milestone</url>
  16.                 </repository>
  17.         </repositories>
  18.         <properties>
  19.                 <java.version>1.6</java.version>
  20.                 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21.                 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22.                 <!-- Spring -->
  23.                 <spring-framework.version>3.2.3.RELEASE</spring-framework.version>
  24.                 <!-- Logging -->
  25.                 <logback.version>1.0.13</logback.version>
  26.                 <slf4j.version>1.7.5</slf4j.version>
  27.                 <!-- Test -->
  28.                 <junit.version>4.11</junit.version>
  29.                 <!-- hadoop -->
  30.                 <CDH.version>2.0.0-cdh4.5.0</CDH.version>
  31.                 <hadoop.core.version>2.0.0-mr1-cdh4.5.0</hadoop.core.version>
  32.                 <groovy.version>1.8.5</groovy.version>
  33.         </properties>
  34.         <dependencies>
  35.                 <!-- Spring and Transactions -->
  36.                 <dependency>
  37.                         <groupId>org.springframework</groupId>
  38.                         <artifactId>spring-context</artifactId>
  39.                         <version>${spring-framework.version}</version>
  40.                 </dependency>
  41.                 <dependency>
  42.                         <groupId>org.springframework</groupId>
  43.                         <artifactId>spring-tx</artifactId>
  44.                         <version>${spring-framework.version}</version>
  45.                 </dependency>
  46.                 <!-- Spring-jdbc -->
  47.                 <dependency>
  48.                         <groupId>org.springframework</groupId>
  49.                         <artifactId>spring-jdbc</artifactId>
  50.                         <version>${spring-framework.version}</version>
  51.                 </dependency>
  52.                 <!-- Logging with SLF4J & LogBack -->
  53.                 <dependency>
  54.                         <groupId>org.slf4j</groupId>
  55.                         <artifactId>slf4j-api</artifactId>
  56.                         <version>${slf4j.version}</version>
  57.                         <scope>compile</scope>
  58.                 </dependency>
  59.                 <dependency>
  60.                         <groupId>ch.qos.logback</groupId>
  61.                         <artifactId>logback-classic</artifactId>
  62.                         <version>${logback.version}</version>
  63.                         <scope>runtime</scope>
  64.                 </dependency>
  65.                 <!-- Test Artifacts -->
  66.                 <dependency>
  67.                         <groupId>org.springframework</groupId>
  68.                         <artifactId>spring-test</artifactId>
  69.                         <version>${spring-framework.version}</version>
  70.                         <scope>test</scope>
  71.                 </dependency>
  72.                 <dependency>
  73.                         <groupId>junit</groupId>
  74.                         <artifactId>junit</artifactId>
  75.                         <version>${junit.version}</version>
  76.                         <scope>test</scope>
  77.                 </dependency>
  78.                 <dependency>
  79.                         <groupId>com.alibaba</groupId>
  80.                         <artifactId>fastjson</artifactId>
  81.                         <version>1.1.37</version>
  82.                 </dependency>
  83.                 <span style="color:#FF0000;"><!-- hadoop -->
  84.                 <dependency>
  85.                         <groupId>org.springframework.data</groupId>
  86.                         <artifactId>spring-data-hadoop</artifactId>
  87.                         <version>1.0.2.RELEASE-cdh4</version>
  88.                 </dependency>
  89.                 <dependency>
  90.                         <groupId>org.apache.hadoop</groupId>
  91.                         <artifactId>hadoop-hdfs</artifactId>
  92.                         <version>${CDH.version}</version>
  93.                 </dependency>
  94.                 <dependency>
  95.                         <groupId>org.apache.hadoop</groupId>
  96.                         <artifactId>hadoop-core</artifactId>
  97.                         <version>${hadoop.core.version}</version>
  98.                 </dependency>
  99.                 <dependency>
  100.                         <groupId>org.codehaus.groovy</groupId>
  101.                         <artifactId>groovy</artifactId>
  102.                         <version>${groovy.version}</version>
  103.                 </dependency>
  104.                 <dependency>
  105.                         <groupId>org.apache.hadoop</groupId>
  106.                         <artifactId>hadoop-client</artifactId>
  107.                         <version>${CDH.version}</version>
  108.                 </dependency>
  109.                 <dependency>
  110.                         <groupId>org.apache.hive</groupId>
  111.                         <artifactId>hive-service</artifactId>
  112.                         <version>0.10.0-cdh4.5.0</version>
  113.                 </dependency>
  114.                 <dependency>
  115.                         <groupId>org.apache.hbase</groupId>
  116.                         <artifactId>hbase</artifactId>
  117.                         <version>0.94.6-cdh4.5.0</version>
  118.                 </dependency></span>
  119.         </dependencies>
  120. </project>
复制代码



spring配置文件为:


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/hadoop"
  3.         xmlns:beans="http://www.springframework.org/schema/beans"
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.         xmlns:context="http://www.springframework.org/schema/context"
  6.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7.           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8.           http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd"
  9.           default-autowire="byName">
  10.         <context:property-placeholder location="classpath:config.properties" />
  11.         <configuration>
  12.                 fs.default.name=${hd.fs}
  13.                 mapred.job.tracker=${hd.mr}
  14.         </configuration>
  15.         <!-- This will throw a NPE at the end of running the app that should be ignored
  16.              
  17.              To avoid this, run against the stand-alone server, use the command line  
  18.              
  19.              hive -hiveconf fs.default.name=hdfs://localhost:9000 -hiveconf mapred.job.tracker=localhost:9001
  20.              
  21.              -->
  22.        
  23.         <hive-server port="${hive.port}" auto-startup="true"/>                     
  24.        
  25.         <hive-client-factory host="${hive.host}" port="${hive.port}"/>       
  26.        
  27.         <hive-template id="hiveTemplate"/>                       
  28.        
  29. </beans:beans>
复制代码


参数配置文件为:
hd.fs=hdfs://172.16.1.50:8020
hd.mr=172.16.1.50:8021
hd.zk.port=2181
hd.zk.host=172.16.1.50
hive.host=172.16.1.50
hive.port=10000

程序中调用方式:
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        HiveTemplate hiveTemplate = (HiveTemplate) ac.getBean("hiveTemplate");
        List list = hiveTemplate.query("show tables;");
        System.out.println(JSON.toJSONString(list));
调用成功


没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条