top of page

Basic HBase Java Classes and Methods – Part 7: Delete from a 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 DeleteTable {

    private static byte[] PERSONAL_CF = Bytes.toBytes("personal");
    private static byte[] PROFESSIONAL_CF = Bytes.toBytes("professional");

    private static byte[] GENDER_COLUMN = Bytes.toBytes("gender");
    private static byte[] OCCUPATION_COLUMN = Bytes.toBytes("occupation");

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

        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf("employee"));

            /* Delete code here */

        } finally {
            connection.close();

            if (table != null) {
                table.close();
            }
        }
    }
}
We will create a Delete object, with a parameter of 1, which means we are deleting from the row with row key 1.  We will then add the columns we wish to delete using the addColumn method, and finally call the delete method on our Table object and pass in our Delete object as its parameter.
            Delete delete = new Delete(Bytes.toBytes("1"));

            delete.addColumn(PERSONAL_CF, GENDER_COLUMN);
            delete.addColumn(PROFESSIONAL_CF, OCCUPATION_COLUMN);

            table.delete(delete);
Putting it all together the complete code is as so:
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 DeleteTable {

    private static byte[] PERSONAL_CF = Bytes.toBytes("personal");
    private static byte[] PROFESSIONAL_CF = Bytes.toBytes("professional");

    private static byte[] GENDER_COLUMN = Bytes.toBytes("gender");
    private static byte[] OCCUPATION_COLUMN = Bytes.toBytes("occupation");

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

        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf("employee"));

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

            delete.addColumn(PERSONAL_CF, GENDER_COLUMN);
            delete.addColumn(PROFESSIONAL_CF, OCCUPATION_COLUMN);

            table.delete(delete);
        } finally {
            connection.close();

            if (table != null) {
                table.close();
            }
        }
    }
}
The last part of this series will show how to Disable and then Drop an HBase table using Java.  Next Basic HBase Java Classes and Methods – Part 8: Disable and Delete a Table

Recent Posts

See All

Comments


Hi, thanks for stopping by!

I'm a paragraph. Click here to add your own text and edit me. I’m a great place for you to tell a story and let your users know a little more about you.

Let the posts
come to you.

Thanks for submitting!

  • Facebook
  • Instagram
  • Twitter
  • Pinterest
bottom of page