HBase – Security ”; Previous Next We can grant and revoke permissions to users in HBase. There are three commands for security purpose: grant, revoke, and user_permission. grant The grant command grants specific rights such as read, write, execute, and admin on a table to a certain user. The syntax of grant command is as follows: hbase> grant <user> <permissions> [<table> [<column family> [<column; qualifier>]] We can grant zero or more privileges to a user from the set of RWXCA, where R – represents read privilege. W – represents write privilege. X – represents execute privilege. C – represents create privilege. A – represents admin privilege. Given below is an example that grants all privileges to a user named ‘Tutorialspoint’. hbase(main):018:0> grant ”Tutorialspoint”, ”RWXCA” revoke The revoke command is used to revoke a user”s access rights of a table. Its syntax is as follows: hbase> revoke <user> The following code revokes all the permissions from the user named ‘Tutorialspoint’. hbase(main):006:0> revoke ”Tutorialspoint” user_permission This command is used to list all the permissions for a particular table. The syntax of user_permission is as follows: hbase>user_permission ‘tablename’ The following code lists all the user permissions of ‘emp’ table. hbase(main):013:0> user_permission ”emp” Print Page Previous Next Advertisements ”;
Category: hbase
HBase – Quick Guide
HBase – Quick Guide ”; Previous Next HBase – Overview Since 1970, RDBMS is the solution for data storage and maintenance related problems. After the advent of big data, companies realized the benefit of processing big data and started opting for solutions like Hadoop. Hadoop uses distributed file system for storing big data, and MapReduce to process it. Hadoop excels in storing and processing of huge data of various formats such as arbitrary, semi-, or even unstructured. Limitations of Hadoop Hadoop can perform only batch processing, and data will be accessed only in a sequential manner. That means one has to search the entire dataset even for the simplest of jobs. A huge dataset when processed results in another huge data set, which should also be processed sequentially. At this point, a new solution is needed to access any point of data in a single unit of time (random access). Hadoop Random Access Databases Applications such as HBase, Cassandra, couchDB, Dynamo, and MongoDB are some of the databases that store huge amounts of data and access the data in a random manner. What is HBase? HBase is a distributed column-oriented database built on top of the Hadoop file system. It is an open-source project and is horizontally scalable. HBase is a data model that is similar to Google’s big table designed to provide quick random access to huge amounts of structured data. It leverages the fault tolerance provided by the Hadoop File System (HDFS). It is a part of the Hadoop ecosystem that provides random real-time read/write access to data in the Hadoop File System. One can store the data in HDFS either directly or through HBase. Data consumer reads/accesses the data in HDFS randomly using HBase. HBase sits on top of the Hadoop File System and provides read and write access. HBase and HDFS HDFS HBase HDFS is a distributed file system suitable for storing large files. HBase is a database built on top of the HDFS. HDFS does not support fast individual record lookups. HBase provides fast lookups for larger tables. It provides high latency batch processing; no concept of batch processing. It provides low latency access to single rows from billions of records (Random access). It provides only sequential access of data. HBase internally uses Hash tables and provides random access, and it stores the data in indexed HDFS files for faster lookups. Storage Mechanism in HBase HBase is a column-oriented database and the tables in it are sorted by row. The table schema defines only column families, which are the key value pairs. A table have multiple column families and each column family can have any number of columns. Subsequent column values are stored contiguously on the disk. Each cell value of the table has a timestamp. In short, in an HBase: Table is a collection of rows. Row is a collection of column families. Column family is a collection of columns. Column is a collection of key value pairs. Given below is an example schema of table in HBase. Rowid Column Family Column Family Column Family Column Family col1 col2 col3 col1 col2 col3 col1 col2 col3 col1 col2 col3 1 2 3 Column Oriented and Row Oriented Column-oriented databases are those that store data tables as sections of columns of data, rather than as rows of data. Shortly, they will have column families. Row-Oriented Database Column-Oriented Database It is suitable for Online Transaction Process (OLTP). It is suitable for Online Analytical Processing (OLAP). Such databases are designed for small number of rows and columns. Column-oriented databases are designed for huge tables. The following image shows column families in a column-oriented database: HBase and RDBMS HBase RDBMS HBase is schema-less, it doesn”t have the concept of fixed columns schema; defines only column families. An RDBMS is governed by its schema, which describes the whole structure of tables. It is built for wide tables. HBase is horizontally scalable. It is thin and built for small tables. Hard to scale. No transactions are there in HBase. RDBMS is transactional. It has de-normalized data. It will have normalized data. It is good for semi-structured as well as structured data. It is good for structured data. Features of HBase HBase is linearly scalable. It has automatic failure support. It provides consistent read and writes. It integrates with Hadoop, both as a source and a destination. It has easy java API for client. It provides data replication across clusters. Where to Use HBase Apache HBase is used to have random, real-time read/write access to Big Data. It hosts very large tables on top of clusters of commodity hardware. Apache HBase is a non-relational database modeled after Google”s Bigtable. Bigtable acts up on Google File System, likewise Apache HBase works on top of Hadoop and HDFS. Applications of HBase It is used whenever there is a need to write heavy applications. HBase is used whenever we need to provide fast random access to available data. Companies such as Facebook, Twitter, Yahoo, and Adobe use HBase internally. HBase History Year Event Nov 2006 Google released the paper on BigTable. Feb 2007 Initial HBase prototype was created as a Hadoop contribution. Oct 2007 The first usable HBase along with Hadoop 0.15.0 was released. Jan 2008 HBase became the sub project of Hadoop. Oct 2008 HBase 0.18.1 was released. Jan 2009 HBase 0.19.0 was released. Sept 2009 HBase 0.20.0 was released. May 2010 HBase became Apache top-level project. HBase – Architecture In HBase, tables are split into regions and are served by the region servers. Regions are vertically divided by column families into “Stores”. Stores are saved as files in HDFS. Shown below is the architecture of HBase. Note: The term ‘store’ is used for regions to explain the storage structure. HBase has three major components: the client library, a master server, and region servers. Region servers can be added or removed as per requirement. MasterServer The master server – Assigns regions to the region servers and takes the help
HBase – Exists
HBase – Exists ”; Previous Next Existence of Table using HBase Shell You can verify the existence of a table using the exists command. The following example shows how to use this command. hbase(main):024:0> exists ”emp” Table emp does exist 0 row(s) in 0.0750 seconds ================================================================== hbase(main):015:0> exists ”student” Table student does not exist 0 row(s) in 0.0480 seconds Verifying the Existence of Table Using Java API You can verify the existence of a table in HBase using the tableExists() method of the HBaseAdmin class. Follow the steps given below to verify the existence of a table in HBase. Step 1 Instantiate the HBaseAdimn class // Instantiating configuration object Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); Step 2 Verify the existence of the table using the tableExists( ) method. Given below is the java program to test the existence of a table in HBase using java API. import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.HBaseAdmin; public class TableExists{ public static void main(String args[])throws IOException{ // Instantiating configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); // Verifying the existance of the table boolean bool = admin.tableExists(“emp”); System.out.println( bool); } } Compile and execute the above program as shown below. $javac TableExists.java $java TableExists The following should be the output: true Print Page Previous Next Advertisements ”;
HBase – Delete Data
HBase – Delete Data ”; Previous Next Deleting a Specific Cell in a Table Using the delete command, you can delete a specific cell in a table. The syntax of delete command is as follows: delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’ Example Here is an example to delete a specific cell. Here we are deleting the salary. hbase(main):006:0> delete ”emp”, ”1”, ”personal data:city”, 1417521848375 0 row(s) in 0.0060 seconds Deleting All Cells in a Table Using the “deleteall” command, you can delete all the cells in a row. Given below is the syntax of deleteall command. deleteall ‘<table name>’, ‘<row>’, Example Here is an example of “deleteall” command, where we are deleting all the cells of row1 of emp table. hbase(main):007:0> deleteall ”emp”,”1” 0 row(s) in 0.0240 seconds Verify the table using the scan command. A snapshot of the table after deleting the table is given below. hbase(main):022:0> scan ”emp” ROW COLUMN + CELL 2 column = personal data:city, timestamp = 1417524574905, value = chennai 2 column = personal data:name, timestamp = 1417524556125, value = ravi 2 column = professional data:designation, timestamp = 1417524204, value = sr:engg 2 column = professional data:salary, timestamp = 1417524604221, value = 30000 3 column = personal data:city, timestamp = 1417524681780, value = delhi 3 column = personal data:name, timestamp = 1417524672067, value = rajesh 3 column = professional data:designation, timestamp = 1417523187, value = jr:engg 3 column = professional data:salary, timestamp = 1417524702514, value = 25000 Deleting Data Using Java API You can delete data from an HBase table using the delete() method of the HTable class. Follow the steps given below to delete data from a table. Step 1: Instantiate the Configuration Class Configuration class adds HBase configuration files to its object. You can create a configuration object using the create() method of the the HbaseConfiguration class as shown below. Configuration conf = HbaseConfiguration.create(); Step 2: Instantiate the HTable Class You have a class called HTable, an implementation of Table in HBase. This class is used to communicate with a single HBase table. While instantiating this class, it accepts the configuration object and the table name as parameters. You can instantiate the HTable class as shown below. HTable hTable = new HTable(conf, tableName); Step 3: Instantiate the Delete Class Instantiate the Delete class by passing the rowid of the row that is to be deleted, in byte array format. You can also pass timestamp and Rowlock to this constructor. Delete delete = new Delete(toBytes(“row1”)); Step 4: Select the Data to be Deleted You can delete the data using the delete methods of the Delete class. This class has various delete methods. Choose the columns or column families to be deleted using those methods. Take a look at the following examples that show the usage of Delete class methods. delete.deleteColumn(Bytes.toBytes(“personal”), Bytes.toBytes(“name”)); delete.deleteFamily(Bytes.toBytes(“professional”)); Step 5: Delete the Data Delete the selected data by passing the delete instance to the delete() method of the HTable class as shown below. table.delete(delete); Step 6: Close the HTableInstance After deleting the data, close the HTable Instance. table.close(); Given below is the complete program to delete data from the HBase table. import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; public class DeleteData { public static void main(String[] args) throws IOException { // Instantiating Configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(conf, “employee”); // Instantiating Delete class Delete delete = new Delete(Bytes.toBytes(“row1”)); delete.deleteColumn(Bytes.toBytes(“personal”), Bytes.toBytes(“name”)); delete.deleteFamily(Bytes.toBytes(“professional”)); // deleting the data table.delete(delete); // closing the HTable object table.close(); System.out.println(“data deleted…..”); } } Compile and execute the above program as shown below. $javac Deletedata.java $java DeleteData The following should be the output: data deleted Print Page Previous Next Advertisements ”;
HBase – Installation
HBase – Installation ”; Previous Next This chapter explains how HBase is installed and initially configured. Java and Hadoop are required to proceed with HBase, so you have to download and install java and Hadoop in your system. Pre-Installation Setup Before installing Hadoop into Linux environment, we need to set up Linux using ssh (Secure Shell). Follow the steps given below for setting up the Linux environment. Creating a User First of all, it is recommended to create a separate user for Hadoop to isolate the Hadoop file system from the Unix file system. Follow the steps given below to create a user. Open the root using the command “su”. Create a user from the root account using the command “useradd username”. Now you can open an existing user account using the command “su username”. Open the Linux terminal and type the following commands to create a user. $ su password: # useradd hadoop # passwd hadoop New passwd: Retype new passwd SSH Setup and Key Generation SSH setup is required to perform different operations on the cluster such as start, stop, and distributed daemon shell operations. To authenticate different users of Hadoop, it is required to provide public/private key pair for a Hadoop user and share it with different users. The following commands are used to generate a key value pair using SSH. Copy the public keys form id_rsa.pub to authorized_keys, and provide owner, read and write permissions to authorized_keys file respectively. $ ssh-keygen -t rsa $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys $ chmod 0600 ~/.ssh/authorized_keys Verify ssh ssh localhost Installing Java Java is the main prerequisite for Hadoop and HBase. First of all, you should verify the existence of java in your system using “java -version”. The syntax of java version command is given below. $ java -version If everything works fine, it will give you the following output. java version “1.7.0_71” Java(TM) SE Runtime Environment (build 1.7.0_71-b13) Java HotSpot(TM) Client VM (build 25.0-b02, mixed mode) If java is not installed in your system, then follow the steps given below for installing java. Step 1 Download java (JDK <latest version> – X64.tar.gz) by visiting the following link Oracle Java. Then jdk-7u71-linux-x64.tar.gz will be downloaded into your system. Step 2 Generally you will find the downloaded java file in Downloads folder. Verify it and extract the jdk-7u71-linux-x64.gz file using the following commands. $ cd Downloads/ $ ls jdk-7u71-linux-x64.gz $ tar zxf jdk-7u71-linux-x64.gz $ ls jdk1.7.0_71 jdk-7u71-linux-x64.gz Step 3 To make java available to all the users, you have to move it to the location “/usr/local/”. Open root and type the following commands. $ su password: # mv jdk1.7.0_71 /usr/local/ # exit Step 4 For setting up PATH and JAVA_HOME variables, add the following commands to ~/.bashrc file. export JAVA_HOME=/usr/local/jdk1.7.0_71 export PATH= $PATH:$JAVA_HOME/bin Now apply all the changes into the current running system. $ source ~/.bashrc Step 5 Use the following commands to configure java alternatives: # alternatives –install /usr/bin/java java usr/local/java/bin/java 2 # alternatives –install /usr/bin/javac javac usr/local/java/bin/javac 2 # alternatives –install /usr/bin/jar jar usr/local/java/bin/jar 2 # alternatives –set java usr/local/java/bin/java # alternatives –set javac usr/local/java/bin/javac # alternatives –set jar usr/local/java/bin/jar Now verify the java -version command from the terminal as explained above. Downloading Hadoop After installing java, you have to install Hadoop. First of all, verify the existence of Hadoop using “ Hadoop version ” command as shown below. hadoop version If everything works fine, it will give you the following output. Hadoop 2.6.0 Compiled by jenkins on 2014-11-13T21:10Z Compiled with protoc 2.5.0 From source with checksum 18e43357c8f927c0695f1e9522859d6a This command was run using /home/hadoop/hadoop/share/hadoop/common/hadoop-common-2.6.0.jar If your system is unable to locate Hadoop, then download Hadoop in your system. Follow the commands given below to do so. Download and extract hadoop-2.6.0 from Apache Software Foundation using the following commands. $ su password: # cd /usr/local # wget http://mirrors.advancedhosters.com/apache/hadoop/common/hadoop- 2.6.0/hadoop-2.6.0-src.tar.gz # tar xzf hadoop-2.6.0-src.tar.gz # mv hadoop-2.6.0/* hadoop/ # exit Installing Hadoop Install Hadoop in any of the required mode. Here, we are demonstrating HBase functionalities in pseudo distributed mode, therefore install Hadoop in pseudo distributed mode. The following steps are used for installing Hadoop 2.4.1. Step 1 – Setting up Hadoop You can set Hadoop environment variables by appending the following commands to ~/.bashrc file. export HADOOP_HOME=/usr/local/hadoop export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export HADOOP_HDFS_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin export HADOOP_INSTALL=$HADOOP_HOME Now apply all the changes into the current running system. $ source ~/.bashrc Step 2 – Hadoop Configuration You can find all the Hadoop configuration files in the location “$HADOOP_HOME/etc/hadoop”. You need to make changes in those configuration files according to your Hadoop infrastructure. $ cd $HADOOP_HOME/etc/hadoop In order to develop Hadoop programs in java, you have to reset the java environment variable in hadoop-env.sh file by replacing JAVA_HOME value with the location of java in your system. export JAVA_HOME=/usr/local/jdk1.7.0_71 You will have to edit the following files to configure Hadoop. core-site.xml The core-site.xml file contains information such as the port number used for Hadoop instance, memory allocated for file system, memory limit for storing data, and the size of Read/Write buffers. Open core-site.xml and add the following properties in between the <configuration> and </configuration> tags. <configuration> <property> <name>fs.default.name</name> <value>hdfs://localhost:9000</value> </property> </configuration> hdfs-site.xml The hdfs-site.xml file contains information such as the value of replication data, namenode path, and datanode path of your local file systems, where you want to store the Hadoop infrastructure. Let us assume the following data. dfs.replication (data replication value) = 1 (In the below given path /hadoop/ is the user name. hadoopinfra/hdfs/namenode is the directory created by hdfs file system.) namenode path = //home/hadoop/hadoopinfra/hdfs/namenode (hadoopinfra/hdfs/datanode is the directory created by hdfs file system.) datanode path = //home/hadoop/hadoopinfra/hdfs/datanode Open this file and add the following properties in between the <configuration>, </configuration> tags. <configuration> <property> <name>dfs.replication</name > <value>1</value> </property> <property> <name>dfs.name.dir</name> <value>file:///home/hadoop/hadoopinfra/hdfs/namenode</value> </property> <property> <name>dfs.data.dir</name> <value>file:///home/hadoop/hadoopinfra/hdfs/datanode</value> </property> </configuration> Note: In the above file, all the property values are user-defined and you can make changes according to your Hadoop infrastructure. yarn-site.xml This file is
HBase – Client API
HBase – Client API ”; Previous Next This chapter describes the java client API for HBase that is used to perform CRUD operations on HBase tables. HBase is written in Java and has a Java Native API. Therefore it provides programmatic access to Data Manipulation Language (DML). Class HBase Configuration Adds HBase configuration files to a Configuration. This class belongs to the org.apache.hadoop.hbase package. Methods and description S.No. Methods and Description 1 static org.apache.hadoop.conf.Configuration create() This method creates a Configuration with HBase resources. Class HTable HTable is an HBase internal class that represents an HBase table. It is an implementation of table that is used to communicate with a single HBase table. This class belongs to the org.apache.hadoop.hbase.client class. Constructors S.No. Constructors and Description 1 HTable() 2 HTable(TableName tableName, ClusterConnection connection, ExecutorService pool) Using this constructor, you can create an object to access an HBase table. Methods and description S.No. Methods and Description 1 void close() Releases all the resources of the HTable. 2 void delete(Delete delete) Deletes the specified cells/row. 3 boolean exists(Get get) Using this method, you can test the existence of columns in the table, as specified by Get. 4 Result get(Get get) Retrieves certain cells from a given row. 5 org.apache.hadoop.conf.Configuration getConfiguration() Returns the Configuration object used by this instance. 6 TableName getName() Returns the table name instance of this table. 7 HTableDescriptor getTableDescriptor() Returns the table descriptor for this table. 8 byte[] getTableName() Returns the name of this table. 9 void put(Put put) Using this method, you can insert data into the table. Class Put This class is used to perform Put operations for a single row. It belongs to the org.apache.hadoop.hbase.client package. Constructors S.No. Constructors and Description 1 Put(byte[] row) Using this constructor, you can create a Put operation for the specified row. 2 Put(byte[] rowArray, int rowOffset, int rowLength) Using this constructor, you can make a copy of the passed-in row key to keep local. 3 Put(byte[] rowArray, int rowOffset, int rowLength, long ts) Using this constructor, you can make a copy of the passed-in row key to keep local. 4 Put(byte[] row, long ts) Using this constructor, we can create a Put operation for the specified row, using a given timestamp. Methods S.No. Methods and Description 1 Put add(byte[] family, byte[] qualifier, byte[] value) Adds the specified column and value to this Put operation. 2 Put add(byte[] family, byte[] qualifier, long ts, byte[] value) Adds the specified column and value, with the specified timestamp as its version to this Put operation. 3 Put add(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) Adds the specified column and value, with the specified timestamp as its version to this Put operation. 4 Put add(byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value) Adds the specified column and value, with the specified timestamp as its version to this Put operation. Class Get This class is used to perform Get operations on a single row. This class belongs to the org.apache.hadoop.hbase.client package. Constructor S.No. Constructor and Description 1 Get(byte[] row) Using this constructor, you can create a Get operation for the specified row. 2 Get(Get get) Methods S.No. Methods and Description 1 Get addColumn(byte[] family, byte[] qualifier) Retrieves the column from the specific family with the specified qualifier. 2 Get addFamily(byte[] family) Retrieves all columns from the specified family. Class Delete This class is used to perform Delete operations on a single row. To delete an entire row, instantiate a Delete object with the row to delete. This class belongs to the org.apache.hadoop.hbase.client package. Constructor S.No. Constructor and Description 1 Delete(byte[] row) Creates a Delete operation for the specified row. 2 Delete(byte[] rowArray, int rowOffset, int rowLength) Creates a Delete operation for the specified row and timestamp. 3 Delete(byte[] rowArray, int rowOffset, int rowLength, long ts) Creates a Delete operation for the specified row and timestamp. 4 Delete(byte[] row, long timestamp) Creates a Delete operation for the specified row and timestamp. Methods S.No. Methods and Description 1 Delete addColumn(byte[] family, byte[] qualifier) Deletes the latest version of the specified column. 2 Delete addColumns(byte[] family, byte[] qualifier, long timestamp) Deletes all versions of the specified column with a timestamp less than or equal to the specified timestamp. 3 Delete addFamily(byte[] family) Deletes all versions of all columns of the specified family. 4 Delete addFamily(byte[] family, long timestamp) Deletes all columns of the specified family with a timestamp less than or equal to the specified timestamp. Class Result This class is used to get a single row result of a Get or a Scan query. Constructors S.No. Constructors 1 Result() Using this constructor, you can create an empty Result with no KeyValue payload; returns null if you call raw Cells(). Methods S.No. Methods and Description 1 byte[] getValue(byte[] family, byte[] qualifier) This method is used to get the latest version of the specified column. 2 byte[] getRow() This method is used to retrieve the row key that corresponds to the row from which this Result was created. Print Page Previous Next Advertisements ”;
HBase – Drop a Table
HBase – Drop a Table ”; Previous Next Dropping a Table using HBase Shell Using the drop command, you can delete a table. Before dropping a table, you have to disable it. hbase(main):018:0> disable ”emp” 0 row(s) in 1.4580 seconds hbase(main):019:0> drop ”emp” 0 row(s) in 0.3060 seconds Verify whether the table is deleted using the exists command. hbase(main):020:07gt; exists ”emp” Table emp does not exist 0 row(s) in 0.0730 seconds drop_all This command is used to drop the tables matching the “regex” given in the command. Its syntax is as follows: hbase> drop_all ‘t.*’ Note: Before dropping a table, you must disable it. Example Assume there are tables named raja, rajani, rajendra, rajesh, and raju. hbase(main):017:0> list TABLE raja rajani rajendra rajesh raju 9 row(s) in 0.0270 seconds All these tables start with the letters raj. First of all, let us disable all these tables using the disable_all command as shown below. hbase(main):002:0> disable_all ”raj.*” raja rajani rajendra rajesh raju Disable the above 5 tables (y/n)? y 5 tables successfully disabled Now you can delete all of them using the drop_all command as given below. hbase(main):018:0> drop_all ”raj.*” raja rajani rajendra rajesh raju Drop the above 5 tables (y/n)? y 5 tables successfully dropped Deleting a Table Using Java API You can delete a table using the deleteTable() method in the HBaseAdmin class. Follow the steps given below to delete a table using java API. Step 1 Instantiate the HBaseAdmin class. // creating a configuration object Configuration conf = HBaseConfiguration.create(); // Creating HBaseAdmin object HBaseAdmin admin = new HBaseAdmin(conf); Step 2 Disable the table using the disableTable() method of the HBaseAdmin class. admin.disableTable(“emp1”); Step 3 Now delete the table using the deleteTable() method of the HBaseAdmin class. admin.deleteTable(“emp12”); Given below is the complete java program to delete a table in HBase. import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.client.HBaseAdmin; public class DeleteTable { public static void main(String[] args) throws IOException { // Instantiating configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); // disabling table named emp admin.disableTable(“emp12”); // Deleting emp admin.deleteTable(“emp12”); System.out.println(“Table deleted”); } } Compile and execute the above program as shown below. $javac DeleteTable.java $java DeleteTable The following should be the output: Table deleted Print Page Previous Next Advertisements ”;
HBase – Count & Truncate
HBase – Count & Truncate ”; Previous Next count You can count the number of rows of a table using the count command. Its syntax is as follows: count ‘<table name>’ After deleting the first row, emp table will have two rows. Verify it as shown below. hbase(main):023:0> count ”emp” 2 row(s) in 0.090 seconds ⇒ 2 truncate This command disables drops and recreates a table. The syntax of truncate is as follows: hbase> truncate ”table name” Example Given below is the example of truncate command. Here we have truncated the emp table. hbase(main):011:0> truncate ”emp” Truncating ”one” table (it may take a while): – Disabling table… – Truncating table… 0 row(s) in 1.5950 seconds After truncating the table, use the scan command to verify. You will get a table with zero rows. hbase(main):017:0> scan ‘emp’ ROW COLUMN + CELL 0 row(s) in 0.3110 seconds Print Page Previous Next Advertisements ”;
HBase – Admin API
HBase – Admin API ”; Previous Next HBase is written in java, therefore it provides java API to communicate with HBase. Java API is the fastest way to communicate with HBase. Given below is the referenced java Admin API that covers the tasks used to manage tables. Class HBaseAdmin HBaseAdmin is a class representing the Admin. This class belongs to the org.apache.hadoop.hbase.client package. Using this class, you can perform the tasks of an administrator. You can get the instance of Admin using Connection.getAdmin() method. Methods and Description S.No. Methods and Description 1 void createTable(HTableDescriptor desc) Creates a new table. 2 void createTable(HTableDescriptor desc, byte[][] splitKeys) Creates a new table with an initial set of empty regions defined by the specified split keys. 3 void deleteColumn(byte[] tableName, String columnName) Deletes a column from a table. 4 void deleteColumn(String tableName, String columnName) Delete a column from a table. 5 void deleteTable(String tableName) Deletes a table. Class Descriptor This class contains the details about an HBase table such as: the descriptors of all the column families, if the table is a catalog table, if the table is read only, the maximum size of the mem store, when the region split should occur, co-processors associated with it, etc. Constructors S.No. Constructor and summary 1 HTableDescriptor(TableName name) Constructs a table descriptor specifying a TableName object. Methods and Description S.No. Methods and Description 1 HTableDescriptor addFamily(HColumnDescriptor family) Adds a column family to the given descriptor Print Page Previous Next Advertisements ”;
HBase – Disabling a Table
HBase – Disabling a Table ”; Previous Next Disabling a Table using HBase Shell To delete a table or change its settings, you need to first disable the table using the disable command. You can re-enable it using the enable command. Given below is the syntax to disable a table: disable ‘emp’ Example Given below is an example that shows how to disable a table. hbase(main):025:0> disable ”emp” 0 row(s) in 1.2760 seconds Verification After disabling the table, you can still sense its existence through list and exists commands. You cannot scan it. It will give you the following error. hbase(main):028:0> scan ”emp” ROW COLUMN + CELL ERROR: emp is disabled. is_disabled This command is used to find whether a table is disabled. Its syntax is as follows. hbase> is_disabled ”table name” The following example verifies whether the table named emp is disabled. If it is disabled, it will return true and if not, it will return false. hbase(main):031:0> is_disabled ”emp” true 0 row(s) in 0.0440 seconds disable_all This command is used to disable all the tables matching the given regex. The syntax for disable_all command is given below. hbase> disable_all ”r.*” Suppose there are 5 tables in HBase, namely raja, rajani, rajendra, rajesh, and raju. The following code will disable all the tables starting with raj. hbase(main):002:07> disable_all ”raj.*” raja rajani rajendra rajesh raju Disable the above 5 tables (y/n)? y 5 tables successfully disabled Disable a Table Using Java API To verify whether a table is disabled, isTableDisabled() method is used and to disable a table, disableTable() method is used. These methods belong to the HBaseAdmin class. Follow the steps given below to disable a table. Step 1 Instantiate HBaseAdmin class as shown below. // Creating configuration object Configuration conf = HBaseConfiguration.create(); // Creating HBaseAdmin object HBaseAdmin admin = new HBaseAdmin(conf); Step 2 Verify whether the table is disabled using isTableDisabled() method as shown below. Boolean b = admin.isTableDisabled(“emp”); Step 3 If the table is not disabled, disable it as shown below. if(!b){ admin.disableTable(“emp”); System.out.println(“Table disabled”); } Given below is the complete program to verify whether the table is disabled; if not, how to disable it. import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.client.HBaseAdmin; public class DisableTable{ public static void main(String args[]) throws MasterNotRunningException, IOException{ // Instantiating configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); // Verifying weather the table is disabled Boolean bool = admin.isTableDisabled(“emp”); System.out.println(bool); // Disabling the table using HBaseAdmin object if(!bool){ admin.disableTable(“emp”); System.out.println(“Table disabled”); } } } Compile and execute the above program as shown below. $javac DisableTable.java $java DsiableTable The following should be the output: false Table disabled Print Page Previous Next Advertisements ”;