search

hive查询hbase

Jun 07, 2016 pm 03:22 PM
hbasehiveInquirebackground

1. 背景 2.hbase查询的确是不太方便,除了指定rowkey,或者通过指定startkey stopkey进行scan之外,没有更有效的查询方式 如果想通过列值过滤,只能全表扫描了 如果要搞什么group by或者order by(除非你的rowkey做了相应设计) 更是没法弄 在传统的mysql/or

1. 背景

2.hbase查询的确是不太方便,除了指定rowkey,或者通过指定startkey stopkey进行scan之外,没有更有效的查询方式 如果想通过列值过滤,只能全表扫描了 如果要搞什么group by或者order by(除非你的rowkey做了相应设计) 更是没法弄 在传统的mysql/oracle得心应手的查询在hbase上就是束手束脚

3.当然可以通过写hadoop job解决问题,但为了查询去写job,代价未免有点高 于是hive出现了

4.有两个方法可以集成hive和hbase

1.使用HBaseStorageHandler,这个会直接操作HBase,可能会对线上产生影响

2.将HBase定期导入到HDFS,再通过hive访问HDFS

下面将详述第二种方法

HDFS导入

1.使用datax将HBase表导入到HDFS上,比如/group/wireless-arctic/task/arctic_task

2.hive产生外部表,从而避免导入数据
 CREATE EXTERNAL TABLE task_history (
 biz_type string,
 cid string,
 content string,
 ctime string,
 gmt_create string,
 hostName string,
 item string,
 mtime string,
 otags string,
 priority string,
 retry string,
 result string,
 srcImages string,
 src_url string,
 status string,
 summary string,
 task_type string,
 title string,
 userId string,
 userNick string,
 utags string,
 writer string
 )
 ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001'
 LOCATION '/group/wireless-arctic/task';location是云梯文件的目录

3.测试
 select cid,result from task_history limit 10;
 Total MapReduce jobs = 1
 Launching Job 1 out of 1
 Number of reduce tasks is set to 0 since there's no reduce operator
 Selecting distributed mode: Input Size (= 2578823293 = 2 gigabytes 411 megabytes 366 kilobytes 125 bytes) is larger than hive.exec.mode.local.auto.inputbytes.max (= 134217728 = 128 megabytes 0 kilobytes 0 bytes)
 Starting Job = job_201311281255_6734353, Tracking URL = http://hdpjt2.alibaba-inc.com/jobdetails.jsp?jobid=job_201311281255_6734353
 Kill Command = /home/hadoop/hadoop-current/bin/../bin/hadoop job  -Dmapred.job.tracker=hdpjt:9001 -kill job_201311281255_6734353
 Hadoop job information for Stage-1: number of mappers: 10; number of reducers: 0
 2013-12-19 18:53:02,891 Stage-1 map = 0%,  reduce = 0%
 2013-12-19 18:53:11,017 Stage-1 map = 50%,  reduce = 0%
 2013-12-19 18:53:12,033 Stage-1 map = 90%,  reduce = 0%
 2013-12-19 18:53:19,394 Stage-1 map = 100%,  reduce = 100%
 Ended Job = job_201311281255_6734353
 OK
 200011928538    success
 200011928538    success
 200011909281    success
 200011928474    success
 200011909281    success
 200011928474    success
 110010569498    failure:userId:1782836127,contentId:110010569498  ImageFlow,call error and ret:1
 110010523403    success
 110010523921    success
 110010524299    success
 Time taken: 23.137 seconds = 23 seconds 137 milliseconds添加分区及自动化

1.完成了上面的步骤,你就可以查询数据了,但面临一个问题,数据更新怎么办?

一个比较通用的做法就是每天跑一个定时任务将HBase表dump到HDFS,即每天一个快照每天的快照可以存放在以日期命名的目录中,这样可以保存多份快照,出了问题也好追踪2.hive如何利用这每天的快照?

那就是hive分区

分区的本意是数据量大了切分数据,但目前我们并未如此使用,而是利用分区来区分快照删除之前的表

drop table task_history;产生一张分区表
CREATE EXTERNAL TABLE task_history (
biz_type string,
cid string,
content string,
ctime string,
gmt_create string,
hostName string,
item string,
mtime string,
otags string,
priority string,
retry string,
result string,
srcImages string,
src_url string,
status string,
summary string,
task_type string,
title string,
userId string,
userNick string,
utags string,
writer string
)
PARTITIONED BY (dt string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\001'
LOCATION '/group/wireless-arctic/task';其实就是在之前的建表语句中加了一行PARTITIONED BY (dt string)

添加分区

ALTER TABLE task_history ADD PARTITION(dt='20131223') LOCATION '/group/wireless-arctic/task/20131223';3.如何自动化

通过工具比如datax或者其他导出工具将HBase表导出到HDFS,正如前面提到的每天一个目录(以日期命名)

将每天的数据目录挂载到hive分区
hive -e "ALTER TABLE task_history ADD PARTITION(dt=`date -d yesterday +%Y%m%d`) LOCATION '/group/wireless-arctic/task/`date -d yesterday +%Y%m%d`';"将前面2个步骤的脚本整合到crontab 中就可以做到自动化了

最后如何通过分区查询
select * from task_history where dt='20131223' limit 10;即加上分区查询条件dt='20131223'

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)