packagecn.itcast.impala.impala;import java.sql.*;public classContants {private static String JDBC_DRIVER="com.cloudera.impala.jdbc41.Driver";private static String CONNECTION_URL="jdbc:impala://node1:21050/default;auth=noSasl";//定义数据库连接
static Connection conn=null;//定义PreparedStatement对象
static PreparedStatement ps=null;//定义查询的结果集
static ResultSet rs= null;//数据库连接
public staticConnection getConn(){try{
Class.forName(JDBC_DRIVER);
conn=DriverManager.getConnection(CONNECTION_URL);
}catch(Exception e) {
e.printStackTrace();
}returnconn;
}//创建一个表
public static voidcreateTable(){
conn=getConn();
String sql="CREATE TABLE impala_kudu_test" +
"(" +
"companyId BIGINT," +
"workId BIGINT," +
"name STRING," +
"gender STRING," +
"photo STRING," +
"PRIMARY KEY(companyId)" +
")" +
"PARTITION BY HASH PARTITIONS 16 " +
"STORED AS KUDU " +
"TBLPROPERTIES (" +
"'kudu.master_addresses' = 'node1:7051,node2:7051,node3:7051'," +
"'kudu.table_name' = 'impala_kudu_test'" +
");";try{
ps=conn.prepareStatement(sql);
ps.execute();
}catch(SQLException e) {
e.printStackTrace();
}
}//查询数据
public staticResultSet queryRows(){try{//定义执行的sql语句
String sql="select * from impala_kudu_test";
ps=getConn().prepareStatement(sql);
rs=ps.executeQuery();
}catch(SQLException e) {
e.printStackTrace();
}returnrs;
}//打印结果
public static voidprintRows(ResultSet rs){/**private int companyId;
private int workId;
private String name;
private String gender;
private String photo;*/
try{while(rs.next()){//获取表的每一行字段信息
int companyId = rs.getInt("companyId");int workId = rs.getInt("workId");
String name= rs.getString("name");
String gender= rs.getString("gender");
String photo= rs.getString("photo");
System.out.print("companyId:"+companyId+" ");
System.out.print("workId:"+workId+" ");
System.out.print("name:"+name+" ");
System.out.print("gender:"+gender+" ");
System.out.println("photo:"+photo);
}
}catch(SQLException e) {
e.printStackTrace();
}finally{if(ps!=null){try{
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(conn !=null){try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}//插入数据
public static voidinsertRows(Person person){
conn=getConn();
String sql="insert into table impala_kudu_test(companyId,workId,name,gender,photo) values(?,?,?,?,?)";try{
ps=conn.prepareStatement(sql);//给占位符?赋值
ps.setInt(1,person.getCompanyId());
ps.setInt(2,person.getWorkId());
ps.setString(3,person.getName());
ps.setString(4,person.getGender());
ps.setString(5,person.getPhoto());
ps.execute();
}catch(SQLException e) {
e.printStackTrace();
}finally{if(ps !=null){try{//关闭
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(conn !=null){try{//关闭
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}//更新数据
public static voidupdateRows(Person person){//定义执行的sql语句
String sql="update impala_kudu_test set workId="+person.getWorkId()+
",name='"+person.getName()+"' ,"+"gender='"+person.getGender()+"' ,"+
"photo='"+person.getPhoto()+"' where companyId="+person.getCompanyId();try{
ps=getConn().prepareStatement(sql);
ps.execute();
}catch(SQLException e) {
e.printStackTrace();
}finally{if(ps !=null){try{//关闭
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(conn !=null){try{//关闭
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}//删除数据
public static void deleteRows(intcompanyId){//定义sql语句
String sql="delete from impala_kudu_test where companyId="+companyId;try{
ps=getConn().prepareStatement(sql);
ps.execute();
}catch(SQLException e) {
e.printStackTrace();
}
}//删除表
public static voiddropTable() {
String sql="drop table if exists impala_kudu_test";try{
ps=getConn().prepareStatement(sql);
ps.execute();
}catch(SQLException e) {
e.printStackTrace();
}
}
}