Hbase: the Definitive Guide
Total Page:16
File Type:pdf, Size:1020Kb
HBase: The Definitive Guide HBase: The Definitive Guide Lars George Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo HBase: The Definitive Guide by Lars George Copyright © 2011 Lars George. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editors: Mike Loukides and Julie Steele Indexer: Angela Howard Production Editor: Jasmine Perez Cover Designer: Karen Montgomery Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreader: Jasmine Perez Illustrator: Robert Romano Printing History: September 2011: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. HBase: The Definitive Guide, the image of a Clydesdale horse, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-39610-7 [LSI] 1314323116 For my wife Katja, my daughter Laura, and son Leon. I love you! Table of Contents Foreword ................................................................... xv Preface .................................................................... xix 1. Introduction ........................................................... 1 The Dawn of Big Data 1 The Problem with Relational Database Systems 5 Nonrelational Database Systems, Not-Only SQL or NoSQL? 8 Dimensions 10 Scalability 12 Database (De-)Normalization 13 Building Blocks 16 Backdrop 16 Tables, Rows, Columns, and Cells 17 Auto-Sharding 21 Storage API 22 Implementation 23 Summary 27 HBase: The Hadoop Database 27 History 27 Nomenclature 29 Summary 29 2. Installation ........................................................... 31 Quick-Start Guide 31 Requirements 34 Hardware 34 Software 40 Filesystems for HBase 52 Local 54 HDFS 54 vii S3 54 Other Filesystems 55 Installation Choices 55 Apache Binary Release 55 Building from Source 58 Run Modes 58 Standalone Mode 59 Distributed Mode 59 Configuration 63 hbase-site.xml and hbase-default.xml 64 hbase-env.sh 65 regionserver 65 log4j.properties 65 Example Configuration 65 Client Configuration 67 Deployment 68 Script-Based 68 Apache Whirr 69 Puppet and Chef 70 Operating a Cluster 71 Running and Confirming Your Installation 71 Web-based UI Introduction 71 Shell Introduction 73 Stopping the Cluster 73 3. Client API: The Basics ................................................... 75 General Notes 75 CRUD Operations 76 Put Method 76 Get Method 95 Delete Method 105 Batch Operations 114 Row Locks 118 Scans 122 Introduction 122 The ResultScanner Class 124 Caching Versus Batching 127 Miscellaneous Features 133 The HTable Utility Methods 133 The Bytes Class 134 4. Client API: Advanced Features .......................................... 137 Filters 137 viii | Table of Contents Introduction to Filters 137 Comparison Filters 140 Dedicated Filters 147 Decorating Filters 155 FilterList 159 Custom Filters 160 Filters Summary 167 Counters 168 Introduction to Counters 168 Single Counters 171 Multiple Counters 172 Coprocessors 175 Introduction to Coprocessors 175 The Coprocessor Class 176 Coprocessor Loading 179 The RegionObserver Class 182 The MasterObserver Class 190 Endpoints 193 HTablePool 199 Connection Handling 203 5. Client API: Administrative Features ...................................... 207 Schema Definition 207 Tables 207 Table Properties 210 Column Families 212 HBaseAdmin 218 Basic Operations 219 Table Operations 220 Schema Operations 228 Cluster Operations 230 Cluster Status Information 233 6. Available Clients ...................................................... 241 Introduction to REST, Thrift, and Avro 241 Interactive Clients 244 Native Java 244 REST 244 Thrift 251 Avro 255 Other Clients 256 Batch Clients 257 MapReduce 257 Table of Contents | ix Hive 258 Pig 263 Cascading 267 Shell 268 Basics 269 Commands 271 Scripting 274 Web-based UI 277 Master UI 277 Region Server UI 283 Shared Pages 283 7. MapReduce Integration ................................................ 289 Framework 289 MapReduce Introduction 289 Classes 290 Supporting Classes 293 MapReduce Locality 293 Table Splits 294 MapReduce over HBase 295 Preparation 295 Data Sink 301 Data Source 306 Data Source and Sink 308 Custom Processing 311 8. Architecture ......................................................... 315 Seek Versus Transfer 315 B+ Trees 315 Log-Structured Merge-Trees 316 Storage 319 Overview 319 Write Path 320 Files 321 HFile Format 329 KeyValue Format 332 Write-Ahead Log 333 Overview 333 HLog Class 335 HLogKey Class 336 WALEdit Class 336 LogSyncer Class 337 LogRoller Class 338 x | Table of Contents Replay 338 Durability 341 Read Path 342 Region Lookups 345 The Region Life Cycle 348 ZooKeeper 348 Replication 351 Life of a Log Edit 352 Internals 353 9. Advanced Usage ...................................................... 357 Key Design 357 Concepts 357 Tall-Narrow Versus Flat-Wide Tables 359 Partial Key Scans 360 Pagination 362 Time Series Data 363 Time-Ordered Relations 367 Advanced Schemas 369 Secondary Indexes 370 Search Integration 373 Transactions 376 Bloom Filters 377 Versioning 381 Implicit Versioning 381 Custom Versioning 384 10. Cluster Monitoring .................................................... 387 Introduction 387 The Metrics Framework 388 Contexts, Records, and Metrics 389 Master Metrics 394 Region Server Metrics 394 RPC Metrics 396 JVM Metrics 397 Info Metrics 399 Ganglia 400 Installation 401 Usage 405 JMX 408 JConsole 410 JMX Remote API 413 Nagios 417 Table of Contents | xi 11. Performance Tuning ................................................... 419 Garbage Collection Tuning 419 Memstore-Local Allocation Buffer 422 Compression 424 Available Codecs 424 Verifying Installation 426 Enabling Compression 427 Optimizing Splits and Compactions 429 Managed Splitting 429 Region Hotspotting 430 Presplitting Regions 430 Load Balancing 432 Merging Regions 433 Client API: Best Practices 434 Configuration 436 Load Tests 439 Performance Evaluation 439 YCSB 440 12. Cluster Administration ................................................. 445 Operational Tasks 445 Node Decommissioning 445 Rolling Restarts 447 Adding Servers 447 Data Tasks 452 Import and Export Tools 452 CopyTable Tool 457 Bulk Import 459 Replication 462 Additional Tasks 464 Coexisting Clusters 464 Required Ports 466 Changing Logging Levels 466 Troubleshooting 467 HBase Fsck 467 Analyzing the Logs 468 Common Issues 471 A. HBase Configuration Properties ......................................... 475 B. Road Map ........................................................... 489 xii | Table of Contents C. Upgrade from Previous Releases ........................................ 491 D. Distributions ......................................................... 493 E. Hush SQL Schema ..................................................... 495 F. HBase Versus Bigtable ................................................. 497 Index ..................................................................... 501 Table of Contents | xiii Foreword The HBase story begins in 2006, when the San Francisco-based startup Powerset was trying to build a natural language search engine for the Web. Their indexing pipeline was an involved multistep process that produced an index about two orders of mag- nitude larger, on average, than your standard term-based index. The datastore that they’d built on top of the then nascent Amazon Web Services to hold the index inter- mediaries and the webcrawl was buckling under the load (Ring. Ring. “Hello! This is AWS. Whatever you are running, please turn it off!”). They were looking for an alter- native. The Google BigTable paper* had just been published. Chad Walters, Powerset’s head of engineering at the time, reflects back on the experience as follows: Building an open source system to run on top of Hadoop’s Distributed Filesystem (HDFS) in much the same way that BigTable ran on top of the Google File System seemed like a good approach because: 1) it was a proven scalable architecture; 2) we could leverage existing work on Hadoop’s HDFS; and 3) we could both contribute to and get additional leverage from the growing Hadoop ecosystem. After the publication of the Google BigTable paper, there were on-again, off-again dis- cussions around what a BigTable-like system on top of Hadoop might look. Then, in early 2007, out of the blue, Mike Cafarela dropped a tarball of thirty odd Java files into the Hadoop issue tracker: “I’ve written some code for HBase, a BigTable-like file store. It’s not perfect, but it’s ready for other people to play with and examine.” Mike had been working with Doug Cutting on Nutch, an open source search engine. He’d done similar drive-by code dumps there to add features such as a Google File System