• Articles
  • Tutorials
  • Interview Questions

Operations using Java API

List of Java API Operations

1. Create table using Java API
createTable() method is used to create a table using java API.
 Example:- 

import java.io.IOException;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.conf.Configuration;
public class tablecreate {
public static void main(String[] args) throws IOException {
Configuration c = HBaseConfiguration.create(); // Instantiate configuration class
HBaseAdmin ad = new HBaseAdmin(c);      // Instantiate HbaseAdmin class
// Instantiate table descriptor class
HTableDescriptor tdescriptor = new TableDescriptor(TableName.valueOf("student"));
tdescriptor.addFamily(new HColumnDescriptor("id"));  // Add column families to tdescriptor
ad.createTable(tdescriptor);   // Execute table using ad
System.out.println(" Table is created "); // It will print “Table is created”
}
}

Compile and execute the program. Then after this it will show the output i.e.
Table is created

2. Disable the table using java API
isTableDisabled()  method is used to check that table is disabled or not. If table is disabled then it will return true otherwise false.If table is not diabled then disableTable() method is used.
Example:- 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class TableDisable{
public static void main(String args[]) throws IOException, MasterNotRunningException, {
Configuration c= HBaseConfiguration.create();      // Instantiate configuration class
HBaseAdmin ad = new HBaseAdmin(c);      // Instantiate HBaseAdmin class
Boolean boolean = admin.isTableDisabled("student");      // Verifying weather the table is disabled
System.out.println(boolean);
if(!boolean){
ad.disableTable("student");      // Disabe the table using HBaseAdmin object
System.out.println("Table is disabled"); // It will print “Table is disabled”
}
}
}

Compile and execute the program. Then after this it will show the output i.e.
Table is disabled

3. Listening of table using Java API
For this listTables() method is used.
 Example:-

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class TablesList {
public static void main(String args[])throws IOException, MasterNotRunningException {
Configuration c = HBaseConfiguration.create();      // Instantiate a configuration class
HBaseAdmin ad = new HBaseAdmin(c);   // Instantiate HBaseAdmin class
HTableDescriptor[] tDescriptor = ad.listTables(); //  Give list of tables HBaseAdmin Object
for (int k=0; k<tDescriptor.length;i++ ){
System.out.println(tDescriptor[k].getNameAsString());      // Print all the table names.
}
}
}

Compile and execute the program. Then after this it will show the output i.e.
User
Student

Learn Java

4. Enable a Table Using Java API
isTableEnabled()  method is used to check that table is enabled or not. If table is enabled then it will return true otherwise false.If table is not enabled then enableTable() method is used.
Example:- 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.*;
public class TableEnable{
public static void main(String args[]) throws IOException, MasterNotRunningException,{
Configuration c = HBaseConfiguration.create();      // Instantiate configuration class
HBaseAdmin ad = new HBaseAdmin(c);   // Instantiate HBaseAdmin class
Boolean boolean = ad.isTableEnabled("student");      //check whether the table is enabled
System.out.println(boolean);
if(!boolean){
ad.enableTable("student");
System.out.println("Table is Enabled");     // Enable the table using HBaseAdmin object
}
}
}

Compile and execute the program. Then after this it will show the output i.e.
Table is Enabled

5. Exit HBase Using Java API 
For this shutdown() method  is used.
Example:-

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class ShutDownHbase{
public static void main(String args[])throws IOException {
Configuration c = HBaseConfiguration.create();      // Instantiate configuration class
HBaseAdmin ad = new HBaseAdmin(c); // Instantiate HBaseAdmin class
System.out.println("Shut down HBase");   // Shut down HBase
ad.shutdown();
}
}

Compile and execute the program. Then after this it will show the output i.e.
Shut down HBase

6. Add Column Family Using Java API
For this, addColumn() method is used.
 Example:-

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class ColumnAdd{
public static void main(String args[]) throws IOException, MasterNotRunningException{
Configuration c = HBaseConfiguration.create();      // Instantiate configuration class.
HBaseAdmin ad = new HBaseAdmin(c); // Instantiate HBaseAdmin class.
HColumnDescriptor cDescriptor = new HColumnDescriptor("id"); //Instantiate columnDescriptor
ad.addColumn("student", cDescriptor);      // Add column family
System.out.println("coloumn is  added which name is id");
}
}

Compile and execute the program. Then after this it will show the output i.e.
coloumn is  added which name is id

7. Deleting a Column Family Using Java API 
For this, deleteColumn() method is used.
Example:- 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class ColoumnDelete{
public static void main(String args[])throws IOException, MasterNotRunningException,{
Configuration c = HBaseConfiguration.create();      // Instantiate configuration class.
HBaseAdmin ad = new HBaseAdmin(c);      // Instantiate HBaseAdmin class.
ad.deleteColumn("student","id");  // Deleting a column family
System.out.println("coloumn  is deleted which name is id");
}
}

Compile and execute the program. Then after this it will show the output i.e.
coloumn is  deleted which name is id

8. Check the existence of a table using java API-
This can be done by tableExists() method.
 Example:- 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class TableExists{
public static void main(String args[])throws IOException{
Configuration c = HBaseConfiguration.create();      // Instantiate configuration class
HBaseAdmin ad = new HBaseAdmin(c);      // Instantiate HBaseAdmin class
Boolean boolean = ad.tableExists("student");       // Cheking the existance of the table
System.out.println( boolean);
}
}

Compile and execute the program. Then after this it will show the output i.e.
If table is exists the it return true otherwise false

9. Drop a table using Java API:-
For this first we need to disable the table then delete the table using  deleteTable() method.
Example:-

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class TableDelete {
public static void main(String[] args) throws IOException {
Configuration c = HBaseConfiguration.create();      // Instantiate configuration class
HBaseAdmin ad = new HBaseAdmin(conf);      // Instantiate HBaseAdmin class
ad.disableTable("student");       // disable the table
ad.deleteTable("student");  // Delete the table
System.out.println("Table is deleted which name is student");
}
}

Compile and execute the program. Then after this it will show the output i.e.
Table is deleted which name is student

10. Insert data using Java API
Put() method is used to save the data whereas add() method is used to add the data.
Example:- 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class DataInsert{
public static void main(String[] args) throws IOException {
Configuration c = HBaseConfiguration.create(); // Instantiate Configuration class
HTable hTable = new HTable(c, "student");       // Instantiate HTable class
Put P1 = new Put(Bytes.toBytes("row1"));  // Instantiate put Class
// accepts column family name, row name  and its value
P1.add(Bytes.toBytes("college"),   Bytes.toBytes("id"),Bytes.toBytes("20"));
P1.add(Bytes.toBytes("college"),Bytes.toBytes("name"),Bytes.toBytes("rishi"));
hTable.put(P1);
System.out.println("Data is inserted");       // Save the put Instance to the HTable.
hTable.close();      // close HTable
}
}

Compile and execute the program. Then after this it will show the output i.e.
Data is inserted

11. Delete Data Using Java API
This can be done by using delete() method.
Example:- 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class DataDelete {
public static void main(String[] args) throws IOException {
Configuration c = HBaseConfiguration.create();// Instantiate Configuration class
HTable table = new HTable(c, "student");       // Instantiate HTable class
Delete delete = new Delete(Bytes.toBytes("row1"));       // Instantiating Delete class
delete.deleteColumn(Bytes.toBytes("college"), Bytes.toBytes("id"));
table.delete(delete);        // delete the data
table.close();        // close the HTable object
System.out.println("Data  is deleted");
}
}

Compile and execute the program. Then after this it will show the output i.e.
Data is deleted

12. Read Data using Java API
This can be done by using get() method.
Example:- 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class DataRead{
public static void main(String[] args) throws IOException {
Configuration c = HBaseConfiguration.create();      // Instantiate Configuration class
HTable table = new HTable(c, "student"); // Instantiate HTable class
Get g = new Get(Bytes.toBytes("row1"));        // Instantiate Get class
Result result = table.get(g);      // Read the data
// Read values from Result class object
byte [] value = result.getValue(Bytes.toBytes("college"),Bytes.toBytes("name"));
String name = Bytes.toString(value);      // Print the values
System.out.println("name: " + name);
}
}

Compile and execute the program. Then after this it will show the output i.e.
name: rishi

13. Update Data using Java API
This can be done using put() method.
Example:-

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class DataUpdate{
public static void main(String[] args) throws IOException {
Configuration c = HBaseConfiguration.create();      // Instantiate Configuration class
HTable hTable = new HTable(c, "student");       // Instantiate HTable class
Put P1 = new Put(Bytes.toBytes("row1"));   // Instantiate Put class
P1.add(Bytes.toBytes("college"),  Bytes.toBytes("name"),Bytes.toBytes("raj")); //update the data
hTable.put(P1);      // Save the put Instance to the HTable.
System.out.println("Data is updated");
hTable.close();      // close HTable
}
}

Compile and execute the program. Then after this it will show the output i.e.
Data is updated

14. Scan Using Java API
Example:-

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
public class tableScan{
public static void main(String args[]) throws IOException{
Configuration c = HBaseConfiguration.create();        // Instantiate Configuration class
HTable table = new HTable(c, "student");        // Instantiate HTable class
Scan scan = new Scan();      // Instantiate the Scan class
scan.addColumn(Bytes.toBytes("college"), Bytes.toBytes("name"));  // Scan the required columns
ResultScanner scanner = table.getScanner(scan);      // Get scan result
// Reading values from scan result
for (Result result = scanner.next(); result != null; result = scanner.next())
System.out.println("Result Found: " + result);
scanner.close();      //close the scanner
}
}

Compile and execute the program. Then after this it will show the output i.e.
Result Found:
keyvalues={row1/college::name/15162756234/Put/vlen=5/mvcc=0}

Course Schedule

Name Date Details
No Sql Course 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Data Engineer

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.