Phoenix连接jadbc的API操作HBase

package com.atguigu.day01;

import org.apache.phoenix.queryserver.client.ThinClientUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.*;
import java.util.Properties;
import java.util.Random;

/**
 * @ClassName: PhoenixTest
 * @Description:Phoneix-JDBC操作
 * @Author: kele
 * @Date: 2021/2/23 11:24
 **/
public class PhoenixTest {

    private Connection connection = null;

    private PreparedStatement statement = null;

    /**
     * 创建连接
     * @throws SQLException
     */
    @Before
    public void Init() throws SQLException {

        //获取url    ****需要先启动queryserver.py start
        String url = ThinClientUtil.getConnectionUrl("hadoop102",8765);

        Properties prop = new Properties();

        //******如果hbase设置namespace mapping,这里也必须设置,否则报错
        prop.setProperty("phoenix.schema.isNamespaceMappingEnabled","true");
        prop.setProperty("phoenix.schema.mapSystemTablesToNamespace","true");

        connection = DriverManager.getConnection(url,prop);

        //phoenix默认任务是手动提交事务,需要设置为自动提交,或者connection.commit()
        //在mysql中是自动提交事务
        connection.setAutoCommit(true);
    }

    /**
     * 关闭连接
     * @throws SQLException
     */

    @After
    public void closeLast() throws SQLException {

        if(connection == null)
            connection.close();
        if(statement == null)
            statement.close();
    }

    /**
     * 创建表
     * @throws SQLException
     */
    @Test
    public void createTable() throws SQLException {


        String sql = "create table dog(" +
                "id varchar primary key," +
                "name varchar," +
                "age varchar)COLUM_ENCODED_BYTES=0";

        statement = connection.prepareStatement(sql);

        statement.executeUpdate();
    }

    /**
     * 插入一条数据
     * @throws SQLException
     */
    @Test
    public void upsertTable() throws SQLException {

        String sql = "upsert into dog values('1001','zhangsan','20')";

        statement = connection.prepareStatement(sql);

        statement.executeUpdate();
    }

    /**
     * 批量插入数据
     * @throws SQLException
     */
    @Test
    public void upsertTableBatch() throws SQLException {

        Random random = new Random();

        String sql = "upsert into dog values(?,?,?)";

        statement = connection.prepareStatement(sql);

        int index=0;
        for (int i = 0; i < 123; i++) {

            statement.setString(1,"1000"+i);
            statement.setString(2,"zhang"+i);
            statement.setString(3,random.nextInt(100)+"");

            statement.addBatch();

            //满30次提交一下
            if(index%30==0){
                //提交batch
                statement.executeBatch();
                //清空batch
                statement.clearBatch();
            }
            index++;

            statement.executeBatch();

        }
    }

    /***********************
     * 数据查询
     * @throws SQLException
     */
    @Test
    public void selectData() throws SQLException {

        String sql = "select *from dog where id>=?";

        statement = connection.prepareStatement(sql);

        statement.setString(1,"10009");

        ResultSet resultSet = statement.executeQuery();

        /****************与HBase不同,HBase通过scan查询的数据需要通过iterator、listcell获取数据
         * **/
        //通过result判断是否查询到下一条数据

        while(resultSet.next()){

            String name = resultSet.getString("name");
            String age = resultSet.getString("age");

            System.out.println("name:"+name+",age:"+age);
        }

    }

    /**
     * 删除数据
     * @throws SQLException
     */
    @Test
    public void deleteData() throws SQLException {

        String sql = "delete from dog where id >= '10001'";

        statement = connection.prepareStatement(sql);

        statement.executeUpdate();
    }

    /**
     * 删除表
     * @throws SQLException
     */
    @Test
    public void dropTable() throws SQLException {

        statement = connection.prepareStatement("drop table dog");
        statement.executeUpdate();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值