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

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

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

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

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

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

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

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

Hbase CTA

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

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

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

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

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

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

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

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

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}

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.