/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. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. -->
  18. <document>
  19. <properties>
  20. <title>Hadoop Hive- Working with Bucketed Tables</title>
  21. <author email="hive-user@hadoop.apache.org">Hadoop Hive Documentation Team</author>
  22. </properties>
  23. <body>
  24. <section name="Defining Bucketed Tables" href="defining_bucketed_tables?">
  25. <p>
  26. This is a brief example on creating a populating bucketed tables. Bucketed tables
  27. are fantastic in that they allow much more efficient sampling than do non-bucketed
  28. tables, and they may later allow for time saving operations such as mapside joins.
  29. However, the bucketing specified at table creation is not enforced when the table
  30. is written to, and so it is possible for the table's metadata to advertise
  31. properties which are not upheld by the table's actual layout. This should obviously
  32. be avoided. Here's how to do it right.
  33. </p>
  34. <p>First theres table creation:</p>
  35. <source><![CDATA[CREATE TABLE user_info_bucketed(user_id BIGINT, firstname STRING, lastname STRING)
  36. COMMENT 'A bucketed copy of user_info'
  37. PARTITIONED BY(ds STRING)
  38. CLUSTERED BY(user_id) INTO 256 BUCKETS;
  39. ]]></source>
  40. <p>notice that we define user_id as the bucket column</p>
  41. </section>
  42. <section name="Populating Bucketed Tables" href="populating_bucketed_tables?">
  43. <source><![CDATA[set hive.enforce.bucketing = true;
  44. FROM user_id
  45. INSERT OVERWRITE TABLE user_info_bucketed
  46. PARTITION (ds='2009-02-25')
  47. SELECT userid, firstname, lastname WHERE ds='2009-02-25';
  48. ]]></source>
  49. <p>The command <strong>set hive.enforce.bucketing = true;</strong> allows the
  50. correct number of reducers and the cluster by column to be automatically selected
  51. based on the table. Otherwise, you would need to set the number of reducers to be
  52. the same as the number of buckets with
  53. <strong>set mapred.reduce.tasks = 256;</strong> and have a
  54. <strong>CLUSTER BY ...</strong> clause in the select.</p>
  55. </section>
  56. <section name="Bucketing Explained" href="bucketing_explained?">
  57. <p>
  58. How 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.
  59. </p>
  60. </section>
  61. <section name="What can go wrong?" href="bucketing_gone_wrong?">
  62. <p>
  63. So, what can go wrong? As long as you
  64. <strong>set hive.enforce.bucketing = true</strong>, and use the syntax above,
  65. the tables should be populated properly. Things can go wrong if the bucketing
  66. column type is different during the insert and on read, or if you manually
  67. cluster by a value that's different from the table definition.
  68. </p>
  69. </section>
  70. </body>
  71. </document>