0% found this document useful (0 votes)
5 views9 pages

Hbase Commands

The document provides a comprehensive guide on using HBase, including commands for creating, modifying, and deleting tables and data. It includes Java code examples for creating tables, inserting data, retrieving data, and deleting data from HBase. Key operations such as altering tables and managing namespaces are also covered.

Uploaded by

vaishnavi kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Hbase Commands

The document provides a comprehensive guide on using HBase, including commands for creating, modifying, and deleting tables and data. It includes Java code examples for creating tables, inserting data, retrieving data, and deleting data from HBase. Key operations such as altering tables and managing namespaces are also covered.

Uploaded by

vaishnavi kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

2.
3.
4. version
5. table_help
6. whoami
7. create ‘mytable’,’colfam’
8. list—---------gives all the tables that are available
9. describe ‘mytable’
10. disable ‘mytable’
11. enable ‘mytable’
12. drop ‘mytable’ —--------table can be dropped only after
disabling
13. alter table
a. alter ‘table1’, {NAME => ‘COLFAM2’}//Adding column
family
b. alter ‘table1’,{NAME => ‘COLFAM2’ , METHOD =>
‘delete’} // DELETING A COLUMN FAMILY
c. alter ‘table1’,{NAME => ‘COLFAM2’ , VERSIONS =>
2}//CHANGING THE VERSIONS OF THE COLUMN
FAMILY
d. alter 'student', READONLY //for making table read-only
e. alter 'student', {NAME => 'semsester', VERSIONS => 5}
f. alter 'student', MAXFILESIZE='65165'
g. alter_status 'student' ///how many regions of the table
have been altered
h. create_namespace 'mynamespace' //creates a name
space(logical grouping) of the tables in hbase
i. create 'mynamespace:table_one', 'colfam1' //create a
table inside a namespace
j. describe_namespace 'mynamespace' //describe
namespace
k. list_namespace //list all namespaces

14. Creating tables and adding data to it


a. create 'person','data' //create table
b. put 'person',1,'data:name','Sahil' //put data into table
c. put 'person',1,'data:city','washington'
d. put 'person',1,'data:id','10'
e. scan 'person' // to print the data of person table
f. get 'person','1' //get data from a particular row
g. get 'person','1',{COLUMN => 'data:id'} //fetch data of
particular column in a column family
h. get 'person','1',{COLUMN => ['data:id','data:name']}
//fetch data from multiple columns in a column family
Creating a table using Hbase Java API
Steps:
1. Specify the table name and column families
2. Connect to HBase
3. Create the table

Methodology
1. Use HTableDescriptor Class to specify the tablename and
column families.
2. Use Connection Class to connect to the database.
3. Use HBaseAdmin class to create table
4. Use addFamily Method to add column families
5. Specify column families with HColumnDescriptor
6. Configuration object created with HBaseConfiguration
7. Connection object can provide an instance of HBaseAdmin
8. HbaseAdmin Class contain all methods for all operations
(create, delete table)

Java Code for Creating table


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
public class createTable{
public static void main(String[] args) throws IOException{
Configuration conf = HBaseConfiguration.create();
Connection connection =
ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
HTableDescriptor tableName = new
HTableDescriptor(TableName.valueOf("secondtable"));
tableName.addFamily(new HColumnDescriptor("colfam1"));
tableName.addFamily(new HColumnDescriptor("colfam2"));
if(!admin.tableExists(tableName.getTableName()))
{
System.out.print("Creating table");
admin.createTable(tableName);
System.out.println("Done");
}
}
}
Inserting data into table
4 step process
1. Specify row id, column and value //put class
2. Connect to HBase // connection class
3. Get a table object to represent our table //connection
object can provide an instance of HTable
4. Insert the Row

Methodology
1. Use Put class to specify data that will be inserted/updated
2. Put is used to insert/update a specific row id
3. All data that is passed to HBase must be in the form of byte
arrays
4. Row id, column family names, column names values all
must be in the form of byte arrays
5. Byte class is a helper class provided by HBase to convert
java primitive to byte arrays
6. Use add column to specify column family, column and value
7. In shell one can insert 1 cell with 1 put

Java code for inserting data into table

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class insertData{


public static void main(String[] args) throws IOException {
Configuration conf=HBaseConfiguration.create();
Connection connection =
ConnectionFactory.createConnection(conf);

Table table =
connection.getTable(TableName.valueOf("mytable"));

Put put = new Put(Bytes.toBytes("1"));

put.addColumn(Bytes.toBytes("colfam1"),
Bytes.toBytes("Channel"),Bytes.toBytes("ABC"));
put.addColumn(Bytes.toBytes("colfam1"),
Bytes.toBytes("Creator"),Bytes.toBytes("Sam"));
put.addColumn(Bytes.toBytes("colfam1"),
Bytes.toBytes("Country"),Bytes.toBytes("USA"));

table.put(put);

}
}

Java code for Retrieving the data from table


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class retr{

public static void main(String[] args) throws IOException {


Configuration conf=HBaseConfiguration.create();
Connection connection =
ConnectionFactory.createConnection(conf);

Table table =
connection.getTable(TableName.valueOf("mytable"));

// Instantiating Get Class


Get g=new Get(Bytes.toBytes("1"));

//Reading the data


Result result=table.get(g);

//Reading values from Result class object


byte []
value=result.getValue(Bytes.toBytes("colfam1"),Bytes.toBytes("C
hannel"));
byte []
value1=result.getValue(Bytes.toBytes("colfam1"),Bytes.toBytes("
Creator"));

//printing the values

String ch=Bytes.toString(value);
String ch1=Bytes.toString(value1);
System.out.println("channel: " + ch + " creator: " +
ch1);
}
}

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class del{

public static void main(String[] args) throws IOException {


Configuration conf=HBaseConfiguration.create();
Connection connection =
ConnectionFactory.createConnection(conf);

Table table =
connection.getTable(TableName.valueOf("mytable"));

Delete delete=new Delete(Bytes.toBytes("1"));


delete.addColumn(Bytes.toBytes("colfam1"),
Bytes.toBytes("Channel"));

table.delete(delete);

table.close();

}
}

You might also like