zoukankan      html  css  js  c++  java
  • (转)Hive Index

    Hive中可以创建Index。

    索引是标准的数据库技术,hive 0.7版本之后支持索引。hive索引采用的不是'one size fites all'的索引实现方式,而是提供插入式接口,并且提供一个具体的索引实现作为参考。
    hive索引具有以下特点:
    1.索引key冗余存储,提供基于key的数据视图
    2.存储设计以优化查询&检索性能
    3.对于某些查询减少IO,从而提高性能。

    hive索引创建语句:

    [plain] view plaincopy
     
    1. CREATE INDEX index_name   
    2. ON TABLE base_table_name (col_name, ...)  
    3. AS 'index.handler.class.name'  
    4. [WITH DEFERRED REBUILD]  
    5. [IDXPROPERTIES (property_name=property_value, ...)]  
    6. [IN TABLE index_table_name]  
    7. [PARTITIONED BY (col_name, ...)]  
    8. [  
    9.    [ ROW FORMAT ...] STORED AS ...  
    10.    | STORED BY ...  
    11. ]  
    12. [LOCATION hdfs_path]  
    13. [TBLPROPERTIES (...)]  
    14. [COMMENT "index comment"]  

    注意:
    1.index的partition默认和数据表一致
    2.视图上不能创建index
    3. index可以通过stored as配置存储格式

    重建索引:

    [plain] view plaincopy
     
    1. ALTER INDEX index_name ON table_name [PARTITION (...)] REBUILD  

    假如在创建索引是我们使用“ WITH DEFERRED REBUILD”语句,则索引创建是为空,可以通过“Alter index ... REBUILD”在一个partition上或所有partition上构建索引。
    注意:
    1.当hive数据更新时,必须调用该语句更新索引。
    2. index rebuild操作时一个原子操作,因此,当rebuild失败时,先前构建的索引也无法使用

    删除索引:

    [plain] view plaincopy
     
    1. DROP INDEX index_name ON table_name  


    示例:
    创建表&索引

    [plain] view plaincopy
     
    1. hive> create table index_test(id INT, name STRING)  
    2.     > partitioned by (dt STRING)  
    3.     > row format delimited fields terminated by ',';  
    4. OK  
    5. Time taken: 32.446 seconds  
    6. hive> create table index_tmp(id INT, name STRING, dt STRING)  
    7.     > row format delimited fields terminated by ',';  
    8. OK  
    9. Time taken: 0.157 seconds  
    10. hive> load data local inpath '/home/work/data/alter_test.txt' into table index_tmp;  
    11. Copying data from file:/home/work/data/alter_test.txt  
    12. Copying file: file:/home/work/data/alter_test.txt  
    13. Loading data to table default.index_tmp  
    14. OK  
    15. Time taken: 1.587 seconds  
    16. hive> set hive.exec.dynamic.partition.mode=nonstrict;  
    17. hive> set hive.exec.dynamic.partition=true;    
    18. hive> insert overwrite table index_test partition(dt)  
    19.     > select id,name,dt  
    20.     > from index_tmp;  
    21. Total MapReduce jobs = 2  
    22. ......  
    23. OK  
    24. Time taken: 56.103 seconds  
    25. hive> create index index1_index_test on table index_test(id) AS  'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' WITH DEFERRED REBUILD ;     
    26. OK  
    27. Time taken: 0.764 seconds  
    28. hive> alter index index1_index_test on index_test rebuild;  
    29. Total MapReduce jobs = 3  
    30. ......  
    31. OK  
    32. Time taken: 138.589 seconds  
    33. hive> show index on index_test;  
    34. OK  
    35. index1_index_test       index_test              id                      default__index_test_index1_index_test__ compact               
    36. Time taken: 0.163 seconds  
    37. hive> show  partitions index_test;   
    38. OK  
    39. dt=2012-08-10  
    40. dt=2012-08-11  
    41. dt=2012-08-12  
    42. Time taken: 0.17 seconds  

    查看索引数据:
    $ hadoop fs -ls /user/hive/warehouse/default__index_test_index1_index_test__
    Found 3 items
    drwxr-xr-x   - work supergroup          0 2012-08-16 18:27 /user/hive/warehouse/default__index_test_index1_index_test__/dt=2012-08-10
    drwxr-xr-x   - work supergroup          0 2012-08-16 18:27 /user/hive/warehouse/default__index_test_index1_index_test__/dt=2012-08-11
    drwxr-xr-x   - work supergroup          0 2012-08-16 18:28 /user/hive/warehouse/default__index_test_index1_index_test__/dt=2012-08-12
    $ hadoop fs -cat /user/hive/warehouse/default__index_test_index1_index_test__/dt=2012-08-10/000000_0
    5,hdfs://localhost:9000/user/hive/warehouse/index_test/dt=2012-08-10/000000_0,0
    6,hdfs://localhost:9000/user/hive/warehouse/index_test/dt=2012-08-10/000000_0,10
    7,hdfs://localhost:9000/user/hive/warehouse/index_test/dt=2012-08-10/000000_0,20
    删除索引:

    [plain] view plaincopy
     
    1. hive> drop index index1_index_test on index_test;  
    2. OK  
    3. Time taken: 0.761 seconds  
    4. hive> show index on index_test;                    
    5. OK  
    6. Time taken: 0.095 seconds  

    索引数据也被删除
    $ hadoop fs -ls /user/hive/warehouse/default__index_test_index1_index_test__
    ls: Cannot access /user/hive/warehouse/default__index_test_index1_index_test__: No such file or directory.

  • 相关阅读:
    分布式系统学习一-概念篇
    JAVA多线程学习九-原子性操作类的应用
    JAVA多线程学习八-多个线程之间共享数据的方式
    JAVA多线程学习七-线程池
    vue 工作随笔
    智能云课堂整理
    mysql
    模板引挚 jade ejs
    node实战小例子
    昭山欢node资料学习笔记
  • 原文地址:https://www.cnblogs.com/end/p/2870352.html
Copyright © 2011-2022 走看看