大数据技术之HBase(四)Hive连接HBase

一、添加jar包

将hbase中的jar包导入到hive中。

cp  /opt/soft/hbase235/lib/* /opt/soft/hive312/lib/

二、修改Hive配置文件

hive-site.xml中添加如下配置:


  hive.zookeeper.quorum
  192.168.95.150


  hbase.zookeeper.quorum
  192.168.95.150


  hive.aux.jars.path
  file:///opt/soft/hive312/lib/hive-hbase-handler-3.1.2.jar,file:///opt/soft/hive312/lib/zookeeper-3.4.6.jar,file:///opt/soft/hive312/lib/hbase-client-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-common-2.3.5-tests.jar,file:///opt/soft/hive312/lib/hbase-server-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-common-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-protocol-2.3.5.jar,file:///opt/soft/hive312/lib/htrace-core-3.2.0-incubating.jar

三、创建关联表

create external table student(
    id string,
    name string,
    age int,
    birthday string,
    address string,
    schoolname string
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
    serdeproperties ("hbase.columns.mapping"=
        ":key,baseinfo:name,baseinfo:age,baseinfo:birthday,schoolinfo:address,schoolinfo:name")
    tblproperties ("hbase.table.name"="bigdata:student");
大数据技术之HBase(四)Hive连接HBase_第1张图片

四、示例

JDBC连接HBase

public class CarTest {
    static Configuration config  = null;
    private Connection conn = null;
    private Admin admin = null;

    @Before
    public void init() throws IOException {
        config = HBaseConfiguration.create();
        config.set(HConstants.HBASE_DIR,"hdfs://192.168.153.139:9000/hbase");
        config.set(HConstants.ZOOKEEPER_QUORUM,"192.168.153.139");
        config.set(HConstants.CLIENT_PORT_STR,"2181");

        conn = ConnectionFactory.createConnection(config);
        admin = conn.getAdmin();
    }

    @Test
    public void createNameSpace() throws IOException {
        NamespaceDescriptor bigdata = NamespaceDescriptor.create("bigdata").build();
        admin.createNamespace(bigdata);
    }

    @Test
    public void createTable() throws IOException {
        TableName tableName = TableName.valueOf("bigdata:car");
        HTableDescriptor desc = new HTableDescriptor(tableName);

        HColumnDescriptor family1 = new HColumnDescriptor("carinfo");
        HColumnDescriptor family2 = new HColumnDescriptor("brandinfo");
        HColumnDescriptor family3 = new HColumnDescriptor("saleinfo");


        desc.addFamily(family1);
        desc.addFamily(family2);
        desc.addFamily(family3);

        admin.createTable(desc);

    }

    @Test
    public void deleteTable() throws IOException {
        admin.disableTable(TableName.valueOf("bigdata:car"));
        admin.deleteTable(TableName.valueOf("bigdata:car"));
    }

    @Test
    public void insertData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("bigdata:car"));
        Put car1 = new Put(Bytes.toBytes("car1"));
        car1.addColumn("carinfo".getBytes(),"weight".getBytes(),"700kg".getBytes());
        car1.addColumn("carinfo".getBytes(),"height".getBytes(),"2.4m".getBytes());
        car1.addColumn("carinfo".getBytes(),"broad".getBytes(),"1.2m".getBytes());
        car1.addColumn("brandinfo".getBytes(),"name".getBytes(),"宝马".getBytes());
        car1.addColumn("brandinfo".getBytes(),"series".getBytes(),"x5".getBytes());
        car1.addColumn("saleinfo".getBytes(),"money".getBytes(),"520000".getBytes());
        car1.addColumn("saleinfo".getBytes(),"time".getBytes(),"2023-03-08".getBytes());

        Put car2 = new Put(Bytes.toBytes("car2"));
        car2.addColumn("carinfo".getBytes(),"weight".getBytes(),"900kg".getBytes());
        car2.addColumn("carinfo".getBytes(),"height".getBytes(),"2.3m".getBytes());
        car2.addColumn("carinfo".getBytes(),"broad".getBytes(),"1.4m".getBytes());
        car2.addColumn("brandinfo".getBytes(),"name".getBytes(),"宝马".getBytes());
        car2.addColumn("brandinfo".getBytes(),"series".getBytes(),"华晨".getBytes());
        car2.addColumn("saleinfo".getBytes(),"money".getBytes(),"250000".getBytes());
        car2.addColumn("saleinfo".getBytes(),"time".getBytes(),"2023-03-06".getBytes());

        table.put(car1);
        table.put(car2);
    }

    @Test
    public void queryData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("bigdata:car"));
        Get car1 = new Get(Bytes.toBytes("car1"));
        Result result = table.get(car1);
        byte[] value11 = result.getValue(Bytes.toBytes("carinfo"), Bytes.toBytes("weight"));
        System.out.println("重量:"+Bytes.toString(value11));
        byte[] value12 = result.getValue(Bytes.toBytes("carinfo"), Bytes.toBytes("height"));
        System.out.println("高度:"+Bytes.toString(value12));
        byte[] value13 = result.getValue(Bytes.toBytes("carinfo"), Bytes.toBytes("broad"));
        System.out.println("宽度:"+Bytes.toString(value13));
        byte[] value21 = result.getValue(Bytes.toBytes("brandinfo"), Bytes.toBytes("money"));
        System.out.println("品牌:"+Bytes.toString(value21));
        byte[] value22 = result.getValue(Bytes.toBytes("brandinfo"), Bytes.toBytes("series"));
        System.out.println("系列:"+Bytes.toString(value22));
        byte[] value31 = result.getValue(Bytes.toBytes("saleinfo"), Bytes.toBytes("weight"));
        System.out.println("售价:"+Bytes.toString(value31));
        byte[] value32 = result.getValue(Bytes.toBytes("saleinfo"), Bytes.toBytes("time"));
        System.out.println("销售时间:"+Bytes.toString(value32));
    }

    @Test
    public void scanDate() throws IOException {
        Table table = conn.getTable(TableName.valueOf("bigdata:car"));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result rs : scanner) {
            byte[] value = rs.getValue(Bytes.toBytes("carinfo"), Bytes.toBytes("weight"));
            System.out.println("重量:"+Bytes.toString(value));
        }
    }

    @After
    public void close() throws IOException {
        if(admin!=null){
            admin.close();
        }
        if(conn!=null){
            conn.close();
        }
    }

}

使用dataGrip查看数据

create external table car(
    id string,
    weight string,
    height string,
    broad string,
    name string,
    series string,
    money int,
    saletime string
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
serdeproperties ("hbase.columns.mapping"=":key,carinfo:weight,carinfo:height,carinfo:broad,brandinfo:name,brandinfo:series,saleinfo:money,saleinfo:time")
tblproperties ("hbase.table.name"="bigdata:car");

select * from car;

你可能感兴趣的:(大数据,hbase,hive,大数据,数据库)