PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.1-rc2/hive/external/docs/xdocs/language_manual/working_with_bucketed_tables.xml

#
XML | 87 lines | 55 code | 14 blank | 18 comment | 0 complexity | c041f2f4b976766762e83affe3c81007 MD5 | raw file
 1<?xml version="1.0" encoding="UTF-8"?>
 2<!--
 3 Licensed to the Apache Software Foundation (ASF) under one
 4 or more contributor license agreements.  See the NOTICE file
 5 distributed with this work for additional information
 6 regarding copyright ownership.  The ASF licenses this file
 7 to you under the Apache License, Version 2.0 (the
 8 "License"); you may not use this file except in compliance
 9 with the License.  You may obtain a copy of the License at
10
11   http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing,
14 software distributed under the License is distributed on an
15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 KIND, either express or implied.  See the License for the
17 specific language governing permissions and limitations
18 under the License.
19-->
20
21<document>
22
23  <properties>
24    <title>Hadoop Hive- Working with Bucketed Tables</title>
25    <author email="hive-user@hadoop.apache.org">Hadoop Hive Documentation Team</author>
26  </properties>
27
28  <body>
29
30 <section name="Defining Bucketed Tables" href="defining_bucketed_tables?">
31
32<p>
33This is a brief example on creating a populating bucketed tables. Bucketed tables 
34are fantastic in that they allow much more efficient sampling than do non-bucketed 
35tables, and they may later allow for time saving operations such as mapside joins. 
36However, the bucketing specified at table creation is not enforced when the table 
37is written to, and so it is possible for the table's metadata to advertise 
38properties which are not upheld by the table's actual layout. This should obviously 
39be avoided. Here's how to do it right.
40</p>
41<p>First there’s table creation:</p>
42
43 <source><![CDATA[CREATE TABLE user_info_bucketed(user_id BIGINT, firstname STRING, lastname STRING)
44COMMENT 'A bucketed copy of user_info'
45PARTITIONED BY(ds STRING)
46CLUSTERED BY(user_id) INTO 256 BUCKETS;
47 ]]></source>
48
49<p>notice that we define user_id as the bucket column</p>
50</section>
51
52<section name="Populating Bucketed Tables" href="populating_bucketed_tables?">
53
54 <source><![CDATA[set hive.enforce.bucketing = true;  
55FROM user_id
56INSERT OVERWRITE TABLE user_info_bucketed
57PARTITION (ds='2009-02-25')
58SELECT userid, firstname, lastname WHERE ds='2009-02-25';
59 ]]></source>
60
61<p>The command <strong>set hive.enforce.bucketing = true;</strong>  allows the 
62correct number of reducers and the cluster by column to be automatically selected 
63based on the table. Otherwise, you would need to set the number of reducers to be 
64the same as the number of buckets with 
65<strong>set mapred.reduce.tasks = 256;</strong> and have a 
66<strong>CLUSTER BY ...</strong> clause in the select.</p>
67
68</section>
69
70<section name="Bucketing Explained" href="bucketing_explained?">
71<p>
72How does Hive distribute the rows across the buckets? In general, the bucket number is determined by the expression hash_function(bucketing_column) mod num_buckets. (There's a '0x7FFFFFFF in there too, but that's not that important). The hash_function depends on the type of the bucketing column. For an int, it's easy, hash_int(i) == i. For example, if user_id were an int, and there were 10 buckets, we would expect all user_id's that end in 0 to be in bucket 1, all user_id's that end in a 1 to be in bucket 2, etc. For other datatypes, it's a little tricky. In particular, the hash of a BIGINT is not the same as the BIGINT. And the hash of a string or a complex datatype will be some number that's derived from the value, but not anything humanly-recognizable. For example, if user_id were a STRING, then the user_id's in bucket 1 would probably not end in 0. In general, distributing rows based on the hash will give you a even distribution in the buckets.
73</p>
74
75</section>
76
77<section name="What can go wrong?" href="bucketing_gone_wrong?">
78<p>
79So, what can go wrong? As long as you 
80<strong>set hive.enforce.bucketing = true</strong>, and use the syntax above, 
81the tables should be populated properly. Things can go wrong if the bucketing 
82column type is different during the insert and on read, or if you manually 
83cluster by a value that's different from the table definition. 
84</p>
85</section>
86</body>
87</document>