1. 程式人生 > >java通過jdbc連線impala和pom.xml以及增查操作

java通過jdbc連線impala和pom.xml以及增查操作

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>


  <groupId>com.insurance</groupId>
  <artifactId>hiveProj</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>hiveProj</name>
  <url>http://maven.apache.org</url>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
    <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0</version>
    </dependency>
    <dependency>  
            <groupId>org.apache.hive</groupId>  
            <artifactId>hive-metastore</artifactId>  
            <version>1.1.0</version>  
    </dependency>  
    <dependency>  
            <groupId>org.apache.hive</groupId>  
            <artifactId>hive-service</artifactId>  
            <version>1.1.0</version>  
    </dependency>
    <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.polyglotted/impala-jdbcimport -->
<!-- <dependency>
    <groupId>org.polyglotted</groupId>
    <artifactId>impala-jdbcimport</artifactId>
    <version>1.0.0</version>
</dependency> -->


  </dependencies>
</project>

程式碼:

package com.insurance.hiveProj;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
   
public class ImpalaTestCase {  
   
    // here is an example query based on one of the Hue Beeswax sample tables   
    private static final String SQL_STATEMENT = "SELECT * FROM tab001";  
   
    // set the impalad host  
    private static final String IMPALAD_HOST = "10.253.128.27";  
   
    // port 21050 is the default impalad JDBC port   
    private static final String IMPALAD_JDBC_PORT = "21050";  
   
    private static final String CONNECTION_URL = "jdbc:hive2://" + IMPALAD_HOST + ':' + IMPALAD_JDBC_PORT + "/testdb;auth=noSasl";  
   
    private static final String JDBC_DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";  
   
    public static void main(String[] args) {  
   
        System.out.println("\n=============================================");  
        System.out.println("Cloudera Impala JDBC Example");  
        System.out.println("Using Connection URL: " + CONNECTION_URL);  
        System.out.println("Running Query: " + SQL_STATEMENT);  
   
        Connection con = null;  
   
        try {  
   
            Class.forName(JDBC_DRIVER_NAME);  
   
            con = DriverManager.getConnection(CONNECTION_URL);  
   
            Statement stmt = con.createStatement();  
            
            //insert(stmt);
            //delete(stmt);
   
            ResultSet rs = stmt.executeQuery(SQL_STATEMENT);  
   
            System.out.println("\n== Begin Query Results ======================");  
   
            // print the results to the console  
            while (rs.next()) {  
                // the example query returns one String column  
                System.out.print(rs.getString("userid")+":");  
                System.out.println(rs.getString("age"));  
            }  
   
            System.out.println("== End Query Results =======================\n\n");  
   
        } catch (SQLException e) {  
            e.printStackTrace();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                con.close();  
            } catch (Exception e) {  
                // swallow  
            }  
        }  
    }
    public static void insert(Statement stmt) {
    String sql = " INSERT INTO tab001(userid, age, city,name) VALUES (101, 22, 'bj','wanghongxiang001') "  ;
    try {
stmt.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
  

說明: impala支援select / insert , 不支援 delete/update單行語句。