通过java代码进行impala和kudu的对接
对于impala而言,开发人员是可以通过JDBC连接impala的,有了JDBC,开发人员可以通过impala来间接操作kudu;

maven导包:
<!-- https://mvnrepository.com/artifact/com.cloudera/ImpalaJDBC41 -->
<dependency>
<groupId>com.cloudera</groupId>
<artifactId>ImpalaJDBC41</artifactId>
<version>2.5.41</version>
</dependency>
通过JDBC连接impala操作kudu
使用JDBC连接impala操作kudu,与JDBC连接mysql做更重增删改查基本一样
创建实体类
package com.impala; /**
* Created by angel;
*/
public class Person { private int companyId;
private int workId ;
private String name;
private String gender;
private String photo;
public Person(int companyId , int workId , String name , String gender , String photo){
this.companyId = companyId ;
this.workId = workId ;
this.name = name ;
this.gender = gender ;
this.photo = photo ;
}
public int getCompanyId() {
return companyId;
} public int getWorkId() {
return workId;
} public String getGender() {
return gender;
} public String getName() {
return name;
} public String getPhoto() {
return photo;
} public void setCompanyId(int companyId) {
this.companyId = companyId;
} public void setGender(String gender) {
this.gender = gender;
} public void setName(String name) {
this.name = name;
} public void setPhoto(String photo) {
this.photo = photo;
} public void setWorkId(int workId) {
this.workId = workId;
} @Override
public String toString() {
return super.toString();
}
}
JDBC连接impala对kudu进行增删改查
package com.impala; import java.sql.*; /**
* Created by angel;
*/
public class Contants {
private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
private static String CONNECTION_URL = "jdbc:impala://hadoop01:21050/default;auth=noSasl";
static Connection con = null;
static ResultSet rs = null;
static PreparedStatement ps = null;
//连接
public static Connection getConn() { try {
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(CONNECTION_URL);
}catch (Exception e){
e.printStackTrace();
}
return con;
}
//查询
public static ResultSet QueryRows(String sql){
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//打印
public static void printRows(ResultSet rs){
try{ while (rs.next()) {
final int companyId = rs.getInt("companyid");
final int workId = rs.getInt("workid");
final String name = rs.getString("Name");
final String gender = rs.getString("gender");
final String photo = rs.getString("photo");
System.out.println(companyId + "----" + workId + "----" + name+ "----" +gender+ "----" + photo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭rs、ps和con
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//插入
public static void insertRows(Person person){
con = getConn();
String sql = "insert into external_table2 (companyid,workid,name,gender,photo) values(?,?,?,?,?)";
PreparedStatement pstmt;
try { pstmt = (PreparedStatement) con.prepareStatement(sql);
pstmt.setInt(1 , person.getCompanyId());
pstmt.setInt(2, person.getWorkId());
pstmt.setString(3, person.getName());
pstmt.setString(4, person.getGender());
pstmt.setString(5, person.getPhoto());
pstmt.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
}
} //更新
public static void updateRows(Person person){
Connection conn = getConn();
String sql = "update external_table2 set photo='" + person.getPhoto() + "' , name='"+person.getName()+"' where workid=" + person.getWorkId(); PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
}
} //删除
public static void deleteRows(int workID){
Connection conn = getConn();
String sql = "delete from external_table2 where workid="+workID;
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
} }
测试
package com.impala; import org.apache.log4j.Logger;
import java.sql.*; public class ImpalaJdbcClient {
private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
//默认数据库default,默认用户名就是登录账号 密码 为空
private static String CONNECTION_URL = "jdbc:impala://hadoop01:21050/default;auth=noSasl";
private static final Logger log = Logger.getLogger(ImpalaJdbcClient.class);
public static void main(String[] args) throws Exception {
final Connection con = Contants.getConn();
//查询
// String sql = "select * from external_table2;";
// final ResultSet rs = Contants.QueryRows(sql);
// Contants.printRows(rs);
//插入
// Contants.insertRows(new Person(11 , 11 , "zhansan" , "female" , "photo10"));
// String sql = "select * from external_table2;";
// final ResultSet rs = Contants.QueryRows(sql);
// Contants.printRows(rs);
//删除
// Contants.deleteRows(10);
// String sql = "select * from external_table2;";
// final ResultSet rs = Contants.QueryRows(sql);
// Contants.printRows(rs);
//更新
// Contants.updateRows(new Person(1,1,"aaaa" , "male" , "pppppp"));
// String sql = "select * from external_table2;";
// final ResultSet rs = Contants.QueryRows(sql);
// Contants.printRows(rs);
}
}
通过java代码进行impala和kudu的对接的更多相关文章
- Java实现impala操作kudu
推荐阅读: 论主数据的重要性(正确理解元数据.数据元) CDC+ETL实现数据集成方案 Java实现impala操作kudu 实战kudu集成impala 对于impala而言,开发人员是可以通过JD ...
- 使用impala操作kudu之创建kudu表(内部表和外部表)
依次启动HDFS.mysql.hive.kudu.impala 登录impala的shell控制端: Impala-shell 1:使用该impala-shell命令启动Impala Shell .默 ...
- 对一致性Hash算法,Java代码实现的深入研究
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...
- 怎样编写高质量的java代码
代码质量概述 怎样辨别一个项目代码写得好还是坏?优秀的代码和腐化的代码区别在哪里?怎么让自己写的代码既漂亮又有生命力?接下来将对代码质量的问题进行一些粗略的介绍.也请有过代码质量相关经验的朋友 ...
- 数据结构笔记--二叉查找树概述以及java代码实现
一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...
- java代码的初始化过程研究
刚刚在ITeye上看到一篇关于java代码初始化的文章,看到代码我试着推理了下结果,虽然是大学时代学的知识了,没想到还能做对.(看来自己大学时掌握的基础还算不错,(*^__^*) 嘻嘻……)但 ...
- JDBC——Java代码与数据库链接的桥梁
常用数据库的驱动程序及JDBC URL: Oracle数据库: 驱动程序包名:ojdbc14.jar 驱动类的名字:oracle.jdbc.driver.OracleDriver JDBC URL:j ...
- 利用Java代码在某些时刻创建Spring上下文
上一篇中,描述了如何使用Spring隐式的创建bean,但当我们需要引进第三方类库添加到我们的逻辑上时,@Conponent与@Autowired是无法添加到类上的,这时,自动装配便不适用了,我们需要 ...
- lombok 简化java代码注解
lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...
随机推荐
- jqueryui插件slider的简单使用
<!DOCTYPE html> <html> <head> <title>slider</title> <meta charset=& ...
- appium+java报错之nodejs报错
$ gulp(node:784) fs: re-evaluating native module sources is not supported. If you areusing the grace ...
- Tornado学习笔记(一) helloword/多进程/启动参数
前言 当你觉得你过得很舒服的时候,你肯定没有在进步.所以我想学习新的东西,然后选择了Tornado.因为我觉得Tornado更匹配目前的我的综合素质. Tornado学习笔记系列主要参考<int ...
- LINUX-redis & mongodb
ubuntu安装redis: apt-get -y install redis-serverubuntu启动redis: /etc/init.d/redis-server restart linux安 ...
- npm dev run 报错
解决办法: npm run dev --port 8088 Error: listen EACCES 0.0.0.0:8080at Object.exports._errnoException (ut ...
- laravel PC内部方法调用
/** * [api 内部请求] * @author Foreach * @param string $method [请求方式] * @param string $url [地址] * @param ...
- python(5):scipy之numpy介绍
python 的scipy 下面的三大库: numpy, matplotlib, pandas scipy 下面还有linalg 等 scipy 中的数据结构主要有三种: ndarray(n维数组), ...
- 4.8cf自训
发现cf以前的好题真的很多.. cf 730j 01背包变形 感觉很好的题 /* 先处理出最少需要t个瓶子 dp[i][j][k]前i个取k个,容量为j时的水的体积 滚动数组搞一下 本题的状态转移必须 ...
- AI-序列化-查-做接口
序列化最终代码(下边的可以不看) from rest_framework.views import APIView from rest_framework import serializers fro ...
- Unnamed namespaces
Unnamed namespaces The unnamed-namespace-definition is a namespace definition of the form inline(o ...