HIVE HBASE 整合

一直想将hbase 与hive整合在一起,因为公司项目工期一期紧似一期,故一直推后。还不知道推到什么时候呢。

今天尝试编译hive,看着官方文档。感觉非常easy:

1、svn co http://svn.apache.org/repos/asf/hive/trunk hive  先下载源码吧。我下载最新代码0.14

2、cd hive ;  mvn  package -Phadoop-2,dist, 编译后须要一大堆測试信息,故去掉測试过程

 mvn  package -Phadoop-2,dist -Dtar -DskipTests

刚開始编译到common的时候报错。 进入common模块单独编译,要增加hadoop2,而且要有hadoop2的相关文件。

下载一大堆文件后,显示编译成功。

注意编译hadoop本地包的时候须要增加-Pnative     

3、在hbase中创建表:

public static void createTable(HBaseAdmin admin) {
        try {
            TableName tn = TableName.valueOf(TABLENAME);
            HTableDescriptor tableDescripter = new HTableDescriptor(tn);
            HColumnDescriptor column = new HColumnDescriptor("x");
//            column.setTimeToLive(600000);
//            column.setCompressionType(Compression.Algorithm.LZO);
            tableDescripter.addFamily(column);
            byte[][] regions = new byte[][]{
                Bytes.toBytes("1"),
                Bytes.toBytes("2"),
                Bytes.toBytes("3"),
                Bytes.toBytes("4"),
                Bytes.toBytes("5"),
                Bytes.toBytes("6"),
                Bytes.toBytes("7"),
                Bytes.toBytes("8"),
                Bytes.toBytes("9"),
                Bytes.toBytes("0")
            };
            admin.createTable(tableDescripter,regions);
        } catch (IOException ex) {
            Logger.getLogger(HbaseConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

4、在hbase 中插入数据:


HbaseUtil util = new HbaseUtil();
        try (HTable table = new HTable(HbaseConfig.getConfig(), HbaseConfig.TABLENAME)) {
            for (int i = 0; i < 10; i++) {
                String dmpid = generateDmpid();
                String channel = "5030";
                String value = "dmpid" + dmpid;
                Put p = new Put(Bytes.toBytes(reverse(dmpid)));
                p.add(Bytes.toBytes("x"), Bytes.toBytes(channel), Bytes.toBytes(value));
                if (value != null && !"".equals(value) && !"null".equals(value)) {
                    p.add(Bytes.toBytes("x"), Bytes.toBytes("5020"), Bytes.toBytes(value));
                    //channel uid 
                }
                table.put(p);
                table.flushCommits();
            }
        }
5、在hive中创建关联表

使用下面命令启动hive:

hive --hiveconf hbase.zookeeper.quorum=host107,host108,host109

启动完毕在hive控制台运行以下命令

CREATE EXTERNAL TABLE emaruser(key string, google string,tanx string,tencent string) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,x:5010,x:5020,x:5030")
TBLPROPERTIES("hbase.table.name" = "emar_user_relation");

6、在hive中查询成功

hive> select * from emaruser;
OK
0054308844100908791141  NULL    dmpid1411978090014488034500     NULL
0101677395300318791141  NULL    dmpid1411978130035937761010     dmpid1411978130035937761010
0529909431699808791141  NULL    dmpid1411978089961349099250     NULL
0816959407102198791141  NULL    dmpid1411978912017049596180     dmpid1411978912017049596180
2144264024543118791141  dmpid1411978113454204624412     dmpid1411978113454204624412     NULL
2309952861202198791141  NULL    dmpid1411978912021682599032     dmpid1411978912021682599032
3106277691400318791141  NULL    dmpid1411978130041967726013     dmpid1411978130041967726013
3109618614400318791141  NULL    dmpid1411978130044168169013     dmpid1411978130044168169013
3207091484943118791141  dmpid1411978113494841907023     dmpid1411978113494841907023     NULL
3265583421200318791141  NULL    dmpid1411978130021243855623     dmpid1411978130021243855623
3449017552200908791141  NULL    dmpid1411978090022557109443     NULL
3645762585202198791141  NULL    dmpid1411978912025852675463     dmpid1411978912025852675463
3660257098743118791141  dmpid1411978113478907520663     dmpid1411978113478907520663     NULL
3701620688943118791141  dmpid1411978113498860261073     dmpid1411978113498860261073     NULL
380639403302198791141   NULL    dmpid141197891203304936083      dmpid141197891203304936083

大功告成。

by the way

使用两个节点的client,hbase入库达到35000条/秒。

集群6台server。

原文地址:https://www.cnblogs.com/claireyuancy/p/6698294.html