2013-07-26 26 views
6

Tôi đang có một bảng trong MySQL viz. nas_comps.Hive - bảng phân vùng bên ngoài (động)

select comp_code, count(leg_id) from nas_comps_01012011_31012011 n group by comp_code; 
comp_code  count(leg_id) 
'J'   20640 
'Y'   39680 

Trước tiên, tôi nhập dữ liệu vào HDFSHadoop phiên bản 1.0.2) sử dụng Sqoop:

sqoop import --connect jdbc:mysql://172.25.37.135/pros_olap2 \ 
--username hadoopranch \ 
--password hadoopranch \ 
--query "select * from nas_comps where dep_date between '2011-01-01' and '2011-01-10' AND \$CONDITIONS" \ 
-m 1 \ 
--target-dir /pros/olap2/dataimports/nas_comps 

Sau đó, tôi đã tạo ra một bên ngoài, chia bảng Hive:

/*shows the partitions on 'describe' but not 'show partitions'*/ 
create external table nas_comps(DS_NAME string,DEP_DATE string, 
           CRR_CODE string,FLIGHT_NO string,ORGN string, 
           DSTN string,PHYSICAL_CAP int,ADJUSTED_CAP int, 
           CLOSED_CAP int) 
PARTITIONED BY (LEG_ID int, month INT, COMP_CODE string) 
location '/pros/olap2/dataimports/nas_comps' 

Các cột phân vùng được hiển thị khi được mô tả:

hive> describe extended nas_comps; 
OK 
ds_name string 
dep_date  string 
crr_code  string 
flight_no  string 
orgn string 
dstn string 
physical_cap int 
adjusted_cap int 
closed_cap  int 
leg_id int 
month int 
comp_code  string 

Detailed Table Information  Table(tableName:nas_comps, dbName:pros_olap2_optim, 
owner:hadoopranch, createTime:1374849456, lastAccessTime:0, retention:0, 
sd:StorageDescriptor(cols:[FieldSchema(name:ds_name, type:string, comment:null), 
FieldSchema(name:dep_date, type:string, comment:null), FieldSchema(name:crr_code, 
type:string, comment:null), FieldSchema(name:flight_no, type:string, comment:null), 
FieldSchema(name:orgn, type:string, comment:null), FieldSchema(name:dstn, type:string, 
comment:null), FieldSchema(name:physical_cap, type:int, comment:null), 
FieldSchema(name:adjusted_cap, type:int, comment:null), FieldSchema(name:closed_cap, 
type:int, comment:null), FieldSchema(name:leg_id, type:int, comment:null), 
FieldSchema(name:month, type:int, comment:null), FieldSchema(name:comp_code, type:string, 
comment:null)], location:hdfs://172.25.37.21:54300/pros/olap2/dataimports/nas_comps, 
inputFormat:org.apache.hadoop.mapred.TextInputFormat, 
outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, 
numBuckets:-1, serdeInfo:SerDeInfo(name:null, 
serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters: 
{serialization.format=1}), bucketCols:[], sortCols:[], parameters:{}), partitionKeys: 
[FieldSchema(name:leg_id, type:int, comment:null), FieldSchema(name:month, type:int, 
comment:null), FieldSchema(name:comp_code, type:string, comment:null)], 
parameters:{EXTERNAL=TRUE, transient_lastDdlTime=1374849456}, viewOriginalText:null, 
viewExpandedText:null, tableType:EXTERNAL_TABLE) 

Nhưng tôi không chắc chắn nếu các phân vùng được tạo ra bởi vì:

hive> show partitions nas_comps; 
OK 
Time taken: 0.599 seconds 


select count(1) from nas_comps; 

trả về 0 hồ sơ

Làm thế nào để tạo ra một bảng Hive bên ngoài với phân vùng động?

Trả lời

9

Hive sẽ không tạo phân vùng cho bạn theo cách này.
Chỉ cần tạo một bảng được phân đoạn bằng khóa phân vùng mong muốn, sau đó thực hiện insert overwrite table từ bảng ngoài sang bảng được phân đoạn mới (thiết lập hive.exec.dynamic.partition=truehive.exec.dynamic.partition.mode=nonstrict).

Nếu bạn phải giữ bảng phân vùng bên ngoài bạn phải tự tạo các thư mục (1 thư mục cho mỗi phân vùng tên nên PARTION_KEY=VALUE) sau đó sử dụng MSCK REPAIR TABLE table_name;command

5

động phân vùng

phân vùng được thêm tự động trong quá trình chèn các bản ghi vào bảng tổ ong.

  1. Chỉ hỗ trợ với câu lệnh chèn.
  2. Không được hỗ trợ với câu hỏi load data.
  3. Cần bật cài đặt phân vùng động trước khi chèn dữ liệu vào bảng hive. hive.exec.dynamic.partition.mode=nonstrict giá trị mặc định là strict hive.exec.dynamic.partition=true giá trị mặc định là false.

động phân vùng Query

SET hive.exec.dynamic.partition.mode=nonstrict; 
SET hive.exec.dynamic.partition=true; 
INSERT INTO table_name PARTITION (loaded_date) 
select * from table_name1 where loaded_date = 20151217 

Đây loaded_date = 20151217 là phân vùng và giá trị của nó.

Hạn chế:

  1. phân vùng động sẽ chỉ làm việc với tuyên bố trên.
  2. Nó sẽ tạo phân vùng động theo dữ liệu mà nó chọn từ cột loaded_date từ table_name1;

Nếu tình trạng của bạn không phù hợp với tiêu chí trên, thì:

Đầu tiên tạo ra một bảng phân vùng sau đó làm như thế này:

ALTER TABLE table_name ADD PARTITION (DS_NAME='partname1',DATE='partname2'); 

hoặc Vui lòng sử dụng Link này để tạo phân vùng động.

+0

Yeah, tôi đã kiểm tra này, nhưng đây không phải là động phân vùng - người dùng vẫn phải cung cấp giá trị cho phân vùng. –

+1

Phải, chạy nó thông qua shell script.Bạn có thể tạo một biến trong shell script cho phân vùng và vượt qua nó trong thay đổi bảng lệnh, nếu không không có tùy chọn có sẵn hiện tại: ( –