0% found this document useful (0 votes)
2 views

CreateTable

The document provides Java code examples for performing basic operations in HBase, including creating a table, inserting data into a single row, retrieving data using the Get method, and deleting a specific column from a row. Each code snippet demonstrates the necessary imports, configuration, and method calls to interact with HBase. The examples focus on a table named 'mytable' and a column family 'colfam1'.

Uploaded by

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

CreateTable

The document provides Java code examples for performing basic operations in HBase, including creating a table, inserting data into a single row, retrieving data using the Get method, and deleting a specific column from a row. Each code snippet demonstrates the necessary imports, configuration, and method calls to interact with HBase. The examples focus on a table named 'mytable' and a column family 'colfam1'.

Uploaded by

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

HBASE

Q-1) To Create A 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("mytable"));

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");
}
}
}
HBASE
Q-2) Insert Column For A Single Row Id
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 tab2{


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);

}
}
HBASE
Q-3) Retrieving Data For A Single Row Id Using Get()
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 vg{

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("Chann
el"));
HBASE
byte []
value1=result.getValue(Bytes.toBytes("colfam1"),Bytes.toBytes("Creat
or"));

//printing the values

String ch=Bytes.toString(value);
String ch1=Bytes.toString(value1);

System.out.println("channel: " + ch + " creator: " + ch1);


}
}

Q-4) Delete Operation In Hbase Table Using Java API


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);
HBASE
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