import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class TestConnection {
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
Driver driver = (Driver) Class.forName("com.cloudera.impala.jdbc41.Driver").newInstance();
Properties propertes = new Properties();
Connection connetction = driver
.connect("jdbc:impala://192.168.44.141:21051/data;AuthMech=3;UID=hive;PWD=hive", propertes); // 使用第三种安全验证
String sql = "SELECT * FROM data.article a WHERE id=?";
PreparedStatement stmt = connetction.prepareStatement(sql);
stmt.setString(1, "100");
ResultSet rs = stmt.executeQuery();
System.out.println("完成执行===============================");
if (rs.next()) {
System.out.println("返回:" + rs.getObject(1));
}
rs.close();
connetction.close();
}
}
在pom.xml中引入依赖包,其中jar包到${project.basedir}/src/main/webapp/WEB-INF/lib/
目录中
<!-- impala start -->
<dependency>
<groupId>com.cloudera</groupId>
<artifactId>ImpalaJDBC41</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/ImpalaJDBC41.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive_metastore</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/hive_metastore.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>hive_service</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/hive_service.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-codec</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/commons-codec-1.3.jar</systemPath>
</dependency>
<dependency>
<groupId>com.facebook</groupId>
<artifactId>libfb303-0.9.0</artifactId>
<version>0.9.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/libfb303-0.9.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.facebook</groupId>
<artifactId>libthrift-0.9.0</artifactId>
<version>0.9.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/libthrift-0.9.0.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>ql</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/ql.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>TCLIServiceClient</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/TCLIServiceClient.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/zookeeper-3.4.6.jar</systemPath>
</dependency>
<!-- impala -->