Hbase之JAVA API
一. 创建表格
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. HColumnDescriptor ;
import org. apache. hadoop. hbase. HTableDescriptor ;
import org. apache. hadoop. hbase. TableName ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import java. io. IOException ;
import java. util. Scanner ;
public class CreateTable {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
HBaseAdmin admin = new HBaseAdmin ( conf) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTableDescriptor htable = new HTableDescriptor ( TableName . valueOf ( tb_name) ) ;
HColumnDescriptor cf1 = new HColumnDescriptor ( "Edu_Info" ) ;
HColumnDescriptor cf2 = new HColumnDescriptor ( "Per_Info" ) ;
htable. addFamily ( cf1) ;
htable. addFamily ( cf2) ;
admin. createTable ( htable) ;
System . out. println ( "Table Created Successfully" ) ;
admin. close ( ) ;
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. TableName ;
import org. apache. hadoop. hbase. client. * ;
import org. apache. hadoop. hbase. security. User ;
import org. apache. hadoop. hbase. security. access. Permission ;
import java. io. IOException ;
import java. util. Scanner ;
public class Create_Table {
public static void main ( String [ ] args) throws IOException {
Configuration con = new Configuration ( ) ;
con. set ( "hbase.zookeeper.quorum" , "niit01" ) ;
System . setProperty ( "HADOOP_USER_NAME" , "root" ) ;
Connection conn = ConnectionFactory . createConnection ( con) ;
Admin admin = conn. getAdmin ( ) ;
System . out. println ( "Enter the table name:" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name= sc. next ( ) ;
TableDescriptorBuilder tdb = TableDescriptorBuilder . newBuilder ( TableName . valueOf ( tb_name) ) ;
tdb. setColumnFamily ( ColumnFamilyDescriptorBuilder . of ( "Per_Info" ) ) . build ( ) ;
tdb. setColumnFamily ( ColumnFamilyDescriptorBuilder . of ( "Edu_Info" ) ) . build ( ) ;
admin. createTable ( tdb. build ( ) ) ;
System . out. println ( "Table created successfully" ) ;
admin. close ( ) ;
}
}
二. 插入数据
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. client. Put ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class InsertData {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
System . out. println ( "Enter the row key" ) ;
String rk = sc. next ( ) ;
Put p = new Put ( Bytes . toBytes ( rk) ) ;
p. add ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) , Bytes . toBytes ( "John" ) ) ;
p. add ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "address" ) , Bytes . toBytes ( "Hainan" ) ) ;
p. add ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "age" ) , Bytes . toBytes ( "20" ) ) ;
htable. put ( p) ;
System . out. println ( "Data Inserted Successfully" ) ;
htable. close ( ) ;
}
}
三. 循环插数据
public static void put ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
HTable table = new HTable ( configuration, TableName . valueOf ( tb_name) ) ;
List < Put > puts = new ArrayList < > ( ) ;
System . out. println ( "please input the row number" ) ;
Scanner r = new Scanner ( System . in) ;
int ro = r. nextInt ( ) ;
for ( int i = 1 ; i <= ro; i++ ) {
byte [ ] row = Bytes . toBytes ( "row" + i) ;
Put put = new Put ( row) ;
byte [ ] columnFamily = Bytes . toBytes ( "data" ) ;
byte [ ] qualifier = Bytes . toBytes ( String . valueOf ( i) ) ;
byte [ ] value = Bytes . toBytes ( "value" + i) ;
put. addColumn ( columnFamily, qualifier, value) ;
puts. add ( put) ;
}
table. put ( puts) ;
System . out. println ( "ok" ) ;
}
四. 得到数据
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. Get ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. client. Result ;
import org. apache. hadoop. hbase. client. Scan ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class ReadData_GET {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
System . out. println ( "Enter the row key" ) ;
String rk = sc. next ( ) ;
Get g = new Get ( Bytes . toBytes ( rk) ) ;
Result result = htable. get ( g) ;
byte [ ] address = result. getValue ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "address" ) ) ;
byte [ ] degree = result. getValue ( Bytes . toBytes ( "Edu_Info" ) , Bytes . toBytes ( "degree" ) ) ;
System . out. println ( "Address : " + Bytes . toString ( address) ) ;
System . out. println ( "Degree : " + Bytes . toString ( degree) ) ;
}
}
五. 扫描表格内容(这里格式输出后设置的和hbase shell里一样【也许】,可自行更改)
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. client. Result ;
import org. apache. hadoop. hbase. client. ResultScanner ;
import org. apache. hadoop. hbase. client. Scan ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class ReadData_SCAN {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niit01" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
Scan scan = new Scan ( ) ;
scan. addColumn ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) ) ;
scan. addColumn ( Bytes . toBytes ( "Edu_Info" ) , Bytes . toBytes ( "degree" ) ) ;
ResultScanner rs = htable. getScanner ( scan) ;
for ( Result result = rs. next ( ) ; result!= null ; result= rs. next ( ) )
{
byte [ ] name = result. getValue ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) ) ;
byte [ ] degree = result. getValue ( Bytes . toBytes ( "Edu_Info" ) , Bytes . toBytes ( "degree" ) ) ;
System . out. println ( "Name : " + Bytes . toString ( name) ) ;
System . out. println ( "Degree : " + Bytes . toString ( degree) ) ;
}
htable. close ( ) ;
}
}
五. 显示所有表格
public static void ListTables ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
HTableDescriptor [ ] descriptors = admin. listTables ( ) ;
for ( int x= 0 ; x< descriptors. length; x++ ) {
System . out. println ( descriptors[ x] . getNameAsString ( ) ) ;
}
}
六. 删除表格
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. TableName ;
import org. apache. hadoop. hbase. client. Admin ;
import org. apache. hadoop. hbase. client. Connection ;
import org. apache. hadoop. hbase. client. ConnectionFactory ;
import java. io. IOException ;
import java. util. Scanner ;
public class Drop_table {
public static void main ( String [ ] args) throws IOException {
Configuration con = new Configuration ( ) ;
con. set ( "hbase.zookeeper.quorum" , "niit01" ) ;
System . setProperty ( "HADOOP_USER_NAME" , "root" ) ;
Connection conn= ConnectionFactory . createConnection ( con) ;
Admin admin = conn. getAdmin ( ) ;
System . out. println ( "Enter the table Name:" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
Boolean isDisabled = admin. isTableDisabled ( TableName . valueOf ( tb_name) ) ;
if ( isDisabled== false ) {
admin. disableTable ( TableName . valueOf ( tb_name) ) ;
admin. deleteTable ( TableName . valueOf ( tb_name) ) ;
}
else
admin. deleteTable ( TableName . valueOf ( tb_name) ) ;
System . out. println ( "Table Dropped successfuly" ) ;
admin. close ( ) ;
conn. close ( ) ;
}
}
public static void dropTable ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
if ( admin. tableExists ( TableName . valueOf ( tb_name) ) ) {
admin. disableTable ( tb_name) ;
admin. deleteTable ( tb_name) ;
System . out. println ( "table " + tb_name + " delete successfully!" ) ;
} else {
System . out. println ( "table " + tb_name + " not exist!" ) ;
}
}
七.表格失效
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import java. io. IOException ;
import java. util. Scanner ;
public class Disable_Table {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
HBaseAdmin admin = new HBaseAdmin ( conf) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
Boolean isDisabled = admin. isTableDisabled ( tb_name) ;
if ( isDisabled== false )
System . out. println ( "Table is enabled" ) ;
else
System . out. println ( "Table is disabled" ) ;
if ( ! isDisabled)
{
admin. disableTable ( tb_name) ; ;
System . out. println ( "Now Table is disabled" ) ;
}
}
}
八. 表格还原
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import java. io. IOException ;
import java. util. Scanner ;
public class Enble_Table {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
HBaseAdmin admin = new HBaseAdmin ( conf) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
Boolean isEnabled = admin. isTableEnabled ( tb_name) ;
if ( isEnabled== true )
System . out. println ( tb_name+ ": Table is already Enabled" ) ;
else
System . out. println ( tb_name+ ": Table is Disabled" ) ;
if ( ! isEnabled)
{
admin. enableTable ( tb_name) ;
System . out. println ( tb_name+ ": Table is now Enabled" ) ;
}
admin. close ( ) ;
}
}
九.删除数据
public static void DeleteData ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HTable table = new HTable ( configuration, "company" ) ;
Delete delete = new Delete ( Bytes . toBytes ( "004" ) ) ;
delete. deleteColumn ( Bytes . toBytes ( "address" ) , Bytes . toBytes ( "country" ) ) ;
delete. deleteColumn ( Bytes . toBytes ( "employee" ) , Bytes . toBytes ( "city" ) ) ;
table. delete ( delete) ;
System . out. println ( "company:name for row 004 deleted" ) ;
}
十.退出hbase shell
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. Delete ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class DeleteData {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
System . out. println ( "Enter the row key" ) ;
String rk = sc. next ( ) ;
Delete del = new Delete ( Bytes . toBytes ( rk) ) ;
del. deleteColumn ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) ) ;
htable. delete ( del) ;
System . out. println ( "Data deleted Successfully" ) ;
htable. close ( ) ;
}
}
总代码(新)
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. HColumnDescriptor ;
import org. apache. hadoop. hbase. HTableDescriptor ;
import org. apache. hadoop. hbase. TableName ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import java. io. IOException ;
import java. util. Scanner ;
public class CreateTable {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
HBaseAdmin admin = new HBaseAdmin ( conf) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTableDescriptor htable = new HTableDescriptor ( TableName . valueOf ( tb_name) ) ;
HColumnDescriptor cf1 = new HColumnDescriptor ( "Edu_Info" ) ;
HColumnDescriptor cf2 = new HColumnDescriptor ( "Per_Info" ) ;
htable. addFamily ( cf1) ;
htable. addFamily ( cf2) ;
admin. createTable ( htable) ;
System . out. println ( "Table Created Successfully" ) ;
admin. close ( ) ;
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. Delete ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class DeleteData {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
System . out. println ( "Enter the row key" ) ;
String rk = sc. next ( ) ;
Delete del = new Delete ( Bytes . toBytes ( rk) ) ;
del. deleteColumn ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) ) ;
htable. delete ( del) ;
System . out. println ( "Data deleted Successfully" ) ;
htable. close ( ) ;
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import java. io. IOException ;
import java. util. Scanner ;
public class Disable_Table {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
HBaseAdmin admin = new HBaseAdmin ( conf) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
Boolean isDisabled = admin. isTableDisabled ( tb_name) ;
if ( isDisabled== false )
System . out. println ( "Table is enabled" ) ;
else
System . out. println ( "Table is disabled" ) ;
if ( ! isDisabled)
{
admin. disableTable ( tb_name) ; ;
System . out. println ( "Now Table is disabled" ) ;
}
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import java. io. IOException ;
import java. util. Scanner ;
public class Enble_Table {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
HBaseAdmin admin = new HBaseAdmin ( conf) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
Boolean isEnabled = admin. isTableEnabled ( tb_name) ;
if ( isEnabled== true )
System . out. println ( tb_name+ ": Table is already Enabled" ) ;
else
System . out. println ( tb_name+ ": Table is Disabled" ) ;
if ( ! isEnabled)
{
admin. enableTable ( tb_name) ;
System . out. println ( tb_name+ ": Table is now Enabled" ) ;
}
admin. close ( ) ;
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HBaseAdmin ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. client. Put ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class InsertData {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
System . out. println ( "Enter the row key" ) ;
String rk = sc. next ( ) ;
Put p = new Put ( Bytes . toBytes ( rk) ) ;
p. add ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) , Bytes . toBytes ( "John" ) ) ;
p. add ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "address" ) , Bytes . toBytes ( "Hainan" ) ) ;
p. add ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "age" ) , Bytes . toBytes ( "20" ) ) ;
htable. put ( p) ;
System . out. println ( "Data Inserted Successfully" ) ;
htable. close ( ) ;
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. Get ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. client. Result ;
import org. apache. hadoop. hbase. client. Scan ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class ReadData_GET {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
System . out. println ( "Enter the row key" ) ;
String rk = sc. next ( ) ;
Get g = new Get ( Bytes . toBytes ( rk) ) ;
Result result = htable. get ( g) ;
byte [ ] address = result. getValue ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "address" ) ) ;
byte [ ] degree = result. getValue ( Bytes . toBytes ( "Edu_Info" ) , Bytes . toBytes ( "degree" ) ) ;
System . out. println ( "Address : " + Bytes . toString ( address) ) ;
System . out. println ( "Degree : " + Bytes . toString ( degree) ) ;
}
}
package com. hbase ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. client. HTable ;
import org. apache. hadoop. hbase. client. Result ;
import org. apache. hadoop. hbase. client. ResultScanner ;
import org. apache. hadoop. hbase. client. Scan ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. Scanner ;
public class ReadData_SCAN {
public static void main ( String [ ] args) throws IOException {
Configuration conf = new Configuration ( ) ;
conf. set ( "hbase.zookeeper.quorum" , "niitchina" ) ;
Scanner sc = new Scanner ( System . in) ;
System . out. println ( "Enter the table name" ) ;
String tb_name = sc. next ( ) ;
HTable htable = new HTable ( conf, tb_name) ;
Scan scan = new Scan ( ) ;
scan. addColumn ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) ) ;
scan. addColumn ( Bytes . toBytes ( "Edu_Info" ) , Bytes . toBytes ( "degree" ) ) ;
ResultScanner rs = htable. getScanner ( scan) ;
for ( Result result = rs. next ( ) ; result!= null ; result= rs. next ( ) )
{
byte [ ] name = result. getValue ( Bytes . toBytes ( "Per_Info" ) , Bytes . toBytes ( "name" ) ) ;
byte [ ] degree = result. getValue ( Bytes . toBytes ( "Edu_Info" ) , Bytes . toBytes ( "degree" ) ) ;
System . out. println ( "Name : " + Bytes . toString ( name) ) ;
System . out. println ( "Degree : " + Bytes . toString ( degree) ) ;
}
htable. close ( ) ;
}
}
总代码(旧)
package com. demo ;
import com. google. inject. internal. asm. $AnnotationVisitor ;
import org. apache. commons. lang. time. StopWatch ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. hbase. * ;
import org. apache. hadoop. hbase. client. * ;
import org. apache. hadoop. hbase. filter. FirstKeyOnlyFilter ;
import org. apache. hadoop. hbase. util. Bytes ;
import java. io. IOException ;
import java. util. ArrayList ;
import java. util. List ;
import java. util. Scanner ;
public class HbaseTest {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main ( String [ ] args) throws IOException {
}
public static void CreateTable ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
HTableDescriptor descriptor = new HTableDescriptor ( TableName . valueOf ( tb_name) ) ;
System . out. println ( "please input the CF1 name" ) ;
String cf1= sc. next ( ) ;
HColumnDescriptor CF1 = new HColumnDescriptor ( cf1) ;
descriptor. addFamily ( CF1) ;
System . out. println ( "please input the CF2 name" ) ;
String cf2= sc. next ( ) ;
HColumnDescriptor CF2 = new HColumnDescriptor ( cf2) ;
descriptor. addFamily ( CF2) ;
if ( admin. tableExists ( TableName . valueOf ( tb_name) ) ) {
System . out. println ( "Table: " + tb_name+ " is exist ! please change the name and try again" ) ;
}
else {
admin. createTable ( descriptor) ;
System . out. println ( "Table: " + tb_name + " successfully created" ) ;
}
}
public static void PutData ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
HTable table = new HTable ( configuration, TableName . valueOf ( tb_name) ) ;
if ( admin. tableExists ( TableName . valueOf ( tb_name) ) ) {
System . out. println ( "please input the rowkey1" ) ;
String r01 = sc. next ( ) ;
Put put1 = new Put ( Bytes . toBytes ( r01) ) ;
System . out. println ( "please input the cf1" ) ;
String cf01 = sc. next ( ) ;
System . out. println ( "please input the c1" ) ;
String c01 = sc. next ( ) ;
System . out. println ( "please input the v1" ) ;
String v01 = sc. next ( ) ;
System . out. println ( "please input the c2" ) ;
String c02 = sc. next ( ) ;
System . out. println ( "please input the v2" ) ;
String v02 = sc. next ( ) ;
System . out. println ( "please input the cf2" ) ;
String cf02 = sc. next ( ) ;
System . out. println ( "please input the c3" ) ;
String c03 = sc. next ( ) ;
System . out. println ( "please input the v3" ) ;
String v03 = sc. next ( ) ;
System . out. println ( "please input the c4" ) ;
String c04 = sc. next ( ) ;
System . out. println ( "please input the v4" ) ;
String v04 = sc. next ( ) ;
put1. add ( Bytes . toBytes ( cf01) , Bytes . toBytes ( c01) , Bytes . toBytes ( v01) ) ;
put1. add ( Bytes . toBytes ( cf01) , Bytes . toBytes ( c02) , Bytes . toBytes ( v02) ) ;
put1. add ( Bytes . toBytes ( cf02) , Bytes . toBytes ( c03) , Bytes . toBytes ( v03) ) ;
put1. add ( Bytes . toBytes ( cf02) , Bytes . toBytes ( c04) , Bytes . toBytes ( v04) ) ;
System . out. println ( "please input the rowkey2" ) ;
String r02 = sc. next ( ) ;
Put put2 = new Put ( Bytes . toBytes ( r02) ) ;
System . out. println ( "please input the v1" ) ;
String v001 = sc. next ( ) ;
System . out. println ( "please input the v2" ) ;
String v002 = sc. next ( ) ;
System . out. println ( "please input the v3" ) ;
String v003 = sc. next ( ) ;
System . out. println ( "please input the v4" ) ;
String v004 = sc. next ( ) ;
put2. add ( Bytes . toBytes ( cf01) , Bytes . toBytes ( c01) , Bytes . toBytes ( v001) ) ;
put2. add ( Bytes . toBytes ( cf01) , Bytes . toBytes ( c02) , Bytes . toBytes ( v002) ) ;
put2. add ( Bytes . toBytes ( cf02) , Bytes . toBytes ( c03) , Bytes . toBytes ( v003) ) ;
put2. add ( Bytes . toBytes ( cf02) , Bytes . toBytes ( c04) , Bytes . toBytes ( v004) ) ;
List < Put > putList = new ArrayList < Put > ( ) ;
putList. add ( put1) ;
putList. add ( put2) ;
table. put ( putList) ;
System . out. println ( "Insert List of Put object successfully!" ) ;
}
else {
System . out. println ( "Table not exist will create now....................." ) ;
Configuration configuration1 = HBaseConfiguration . create ( ) ;
configuration1. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin1 = new HBaseAdmin ( configuration1) ;
System . out. println ( "please input the new table name" ) ;
Scanner scanner = new Scanner ( System . in) ;
String tb_name_new = sc. next ( ) ;
HTableDescriptor descriptor = new HTableDescriptor ( TableName . valueOf ( tb_name_new) ) ;
System . out. println ( "please input the CF1 name" ) ;
String cf11= scanner. next ( ) ;
HColumnDescriptor CF1 = new HColumnDescriptor ( cf11) ;
descriptor. addFamily ( CF1) ;
System . out. println ( "please input the CF2 name" ) ;
String cf12= scanner. next ( ) ;
HColumnDescriptor CF2 = new HColumnDescriptor ( cf12) ;
descriptor. addFamily ( CF2) ;
admin. createTable ( descriptor) ;
System . out. println ( "Table: " + tb_name_new + " successfully created" ) ;
System . out. println ( "Table exist now put data now....................." ) ;
System . out. println ( "please input the rowkey1" ) ;
String r11 = scanner. next ( ) ;
Put put1= new Put ( Bytes . toBytes ( r11) ) ;
System . out. println ( "please input the column1" ) ;
String c11 = scanner. next ( ) ;
System . out. println ( "please input the value1" ) ;
String v11 = scanner. next ( ) ;
System . out. println ( "please input the column2" ) ;
String c12 = scanner. next ( ) ;
System . out. println ( "please input the value2" ) ;
String v12 = scanner. next ( ) ;
System . out. println ( "please input the column3" ) ;
String c13 = scanner. next ( ) ;
System . out. println ( "please input the value3" ) ;
String v13 = scanner. next ( ) ;
System . out. println ( "please input the column4" ) ;
String c14 = scanner. next ( ) ;
System . out. println ( "please input the value4" ) ;
String v14 = scanner. next ( ) ;
put1. add ( Bytes . toBytes ( cf11) , Bytes . toBytes ( c11) , Bytes . toBytes ( v11) ) ;
put1. add ( Bytes . toBytes ( cf11) , Bytes . toBytes ( c12) , Bytes . toBytes ( v12) ) ;
put1. add ( Bytes . toBytes ( cf12) , Bytes . toBytes ( c13) , Bytes . toBytes ( v13) ) ;
put1. add ( Bytes . toBytes ( cf12) , Bytes . toBytes ( c14) , Bytes . toBytes ( v14) ) ;
System . out. println ( "please input the rowkey2" ) ;
String r2 = scanner. next ( ) ;
Put put2 = new Put ( Bytes . toBytes ( r2) ) ;
System . out. println ( "please input the value1" ) ;
String v21 = scanner. next ( ) ;
System . out. println ( "please input the value2" ) ;
String v22 = scanner. next ( ) ;
System . out. println ( "please input the value3" ) ;
String v23 = scanner. next ( ) ;
System . out. println ( "please input the value4" ) ;
String v24 = scanner. next ( ) ;
put2. add ( Bytes . toBytes ( cf11) , Bytes . toBytes ( c11) , Bytes . toBytes ( v21) ) ;
put2. add ( Bytes . toBytes ( cf11) , Bytes . toBytes ( c12) , Bytes . toBytes ( v22) ) ;
put2. add ( Bytes . toBytes ( cf12) , Bytes . toBytes ( c13) , Bytes . toBytes ( v23) ) ;
put2. add ( Bytes . toBytes ( cf12) , Bytes . toBytes ( c14) , Bytes . toBytes ( v24) ) ;
List < Put > putList = new ArrayList < Put > ( ) ;
putList. add ( put1) ;
putList. add ( put2) ;
table. put ( putList) ;
System . out. println ( "Insert List of Put object successfully!" ) ;
}
}
public static void put ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
HTable table = new HTable ( configuration, TableName . valueOf ( tb_name) ) ;
List < Put > puts = new ArrayList < > ( ) ;
System . out. println ( "please input the row number" ) ;
Scanner r = new Scanner ( System . in) ;
int ro = r. nextInt ( ) ;
for ( int i = 1 ; i <= ro; i++ ) {
byte [ ] row = Bytes . toBytes ( "row" + i) ;
Put put = new Put ( row) ;
byte [ ] columnFamily = Bytes . toBytes ( "data" ) ;
byte [ ] qualifier = Bytes . toBytes ( String . valueOf ( i) ) ;
byte [ ] value = Bytes . toBytes ( "value" + i) ;
put. addColumn ( columnFamily, qualifier, value) ;
puts. add ( put) ;
}
table. put ( puts) ;
System . out. println ( "ok" ) ;
}
public static void GetData ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HTable table = new HTable ( configuration, "employee" ) ;
Get get = new Get ( Bytes . toBytes ( "eid01" ) ) ;
Result result = table. get ( get) ;
for ( KeyValue keyvalue : result. raw ( ) ) {
System . out. println ( "Row Key -->" + new String ( keyvalue. getRow ( ) ) ) ;
System . out. println ( "Column Family -->" + new String ( keyvalue. getFamily ( ) ) ) ;
System . out. println ( "Qualifier -->" + new String ( keyvalue. getQualifier ( ) ) ) ;
System . out. println ( "TimeStamp -->" + keyvalue. getTimestamp ( ) ) ;
System . out. println ( "Value -->" + new String ( keyvalue. getValue ( ) ) ) ;
System . out. println ( ) ;
}
}
public static void Scan ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
long rowCount = 0 ;
HTable table = new HTable ( configuration, TableName . valueOf ( tb_name) ) ;
if ( admin. tableExists ( TableName . valueOf ( tb_name) ) ) {
Scan scan = new Scan ( ) ;
Scan scan1 = new Scan ( ) ;
ResultScanner results = table. getScanner ( scan) ;
scan1. setFilter ( new FirstKeyOnlyFilter ( ) ) ;
ResultScanner resultScanner = table. getScanner ( scan1) ;
for ( Result result : resultScanner) {
rowCount += result. size ( ) ;
}
System . out. println ( "----------------------------------------------Get all records-------------------------------------------\n" ) ;
System . out. println ( "ROW COLUMN+CELL" ) ;
for ( Result result : results) {
for ( KeyValue keyValue : result. raw ( ) ) {
System . out. print ( new String ( keyValue. getRow ( ) ) + ", " ) ;
System . out. print ( "column=" + new String ( keyValue. getFamily ( ) ) ) ;
System . out. print ( ":" + new String ( keyValue. getQualifier ( ) ) + ", " ) ;
System . out. print ( "timeStamp=" + keyValue. getTimestamp ( ) + ", " ) ;
System . out. print ( "value=" + new String ( keyValue. getValue ( ) ) ) ;
System . out. println ( ) ;
}
}
System . out. println ( rowCount+ " row(s) " ) ;
System . out. println ( "------------------------------------------------finish--------------------------------------------------" ) ;
}
else {
System . out. println ( "Table: " + tb_name+ " is not exist ! please change the name and try again" ) ;
}
}
public static void ListTables ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
HTableDescriptor [ ] descriptors = admin. listTables ( ) ;
for ( int x= 0 ; x< descriptors. length; x++ ) {
System . out. println ( descriptors[ x] . getNameAsString ( ) ) ;
}
}
public static void dropTable ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
System . out. println ( "please input the table name" ) ;
Scanner sc = new Scanner ( System . in) ;
String tb_name = sc. next ( ) ;
if ( admin. tableExists ( TableName . valueOf ( tb_name) ) ) {
admin. disableTable ( tb_name) ;
admin. deleteTable ( tb_name) ;
System . out. println ( "table " + tb_name + " delete successfully!" ) ;
} else {
System . out. println ( "table " + tb_name + " not exist!" ) ;
}
}
public static void disableTable ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
try {
System . out. println ( "please enter the table you want disable" ) ;
Scanner scan = new Scanner ( System . in) ;
String tb_name = scan. next ( ) ;
admin. disableTable ( tb_name) ;
System . out. println ( tb_name + " is disable now" ) ;
}
catch ( IOException e) {
e. printStackTrace ( ) ;
System . out. println ( "table is already disabled please try other table" ) ;
}
}
public static void enableTable ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
try {
System . out. println ( "please enter the table you want enable" ) ;
Scanner scan = new Scanner ( System . in) ;
String tb_name = scan. next ( ) ;
admin. enableTable ( tb_name) ;
System . out. println ( tb_name + " is enable now" ) ;
}
catch ( IOException e) {
e. printStackTrace ( ) ;
System . out. println ( "table is already enabled please try other table" ) ;
}
}
public static void DeleteData ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ; /
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HTable table = new HTable ( configuration, "company" ) ;
Delete delete = new Delete ( Bytes . toBytes ( "004" ) ) ;
delete. deleteColumn ( Bytes . toBytes ( "address" ) , Bytes . toBytes ( "country" ) ) ;
delete. deleteColumn ( Bytes . toBytes ( "employee" ) , Bytes . toBytes ( "city" ) ) ;
table. delete ( delete) ;
System . out. println ( "company:name for row 004 deleted" ) ;
}
public static void ExitHbase_Shell ( ) throws IOException {
Configuration configuration = HBaseConfiguration . create ( ) ;
configuration. set ( "hbase.zookeeper.quorum" , "niit" ) ;
HBaseAdmin admin = new HBaseAdmin ( configuration) ;
admin. shutdown ( ) ;
System . out. println ( "shut down now" ) ;
}
}