Hive - Sample Clause

> Database > Apache - Hive (HS|Hive Server)

1 - About

The sampling clause allows the users to write queries for samples of the data instead of the whole table. Currently the sampling is done on the clustered column. (ie columns specified in the CLUSTERED BY)

3 - Syntax

The buckets are numbered starting from 0.

In general the TABLESAMPLE syntax looks like:

TABLESAMPLE(BUCKET x OUT OF y)

where:

  • y has to be a multiple or divisor of the number of buckets in that table as specified at the table creation time.
  • The buckets chosen are determined when the following formula is true:

<MATH> \text{BucketNumber } \text{module } y = x </MATH>

Advertising

4 - Example

The table pv_gender_sum has 32 bucket.

  • the following statement will select 3 bucket.
SELECT pv_gender_sum.* FROM pv_gender_sum TABLESAMPLE(BUCKET 3 OUT OF 32);
  • the following statement will pick out the 3rd and 19th buckets
SELECT pv_gender_sum.* FROM pv_gender_sum TABLESAMPLE(BUCKET 3 OUT OF 16)
  • the following statement will pick out half of the 3rd bucket.
SELECT pv_gender_sum.* FROM pv_gender_sum TABLESAMPLE(BUCKET 3 OUT OF 64 ON userid)

5 - Documentation / Reference

db/hive/sample.txt · Last modified: 2018/07/01 10:00 by gerardnico