Hbase之JAVA API
=================================================================================================
一. 创建表格
public static void CreateTable() throws IOException {
Configuration configuration = HBaseConfiguration.create();//实例化配置信息对象
configuration.set("hbase.zookeeper.quorum", "niit");//明确目标地址
HBaseAdmin admin = new HBaseAdmin(configuration);//实例化hbase对象
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);//列族对象1
descriptor.addFamily(CF1);//将列族对象1创建在表格对象中
System.out.println("please input the CF2 name");
String cf2= sc.next();
HColumnDescriptor CF2 = new HColumnDescriptor(cf2);//列族对象2
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));//选定put的表格
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));
//table.put(put);//时间戳不同
//System.out.println("inserted record eid01 to table employee done");
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!");
//System.out.println("inserted record eid02 to table employee done");
}
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);//实例化hbase对象
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);//列族对象1
descriptor.addFamily(CF1);//将列族对象1创建在表格对象中
System.out.println("please input the CF2 name");
String cf12= scanner.next();
HColumnDescriptor CF2 = new HColumnDescriptor(cf12);//列族对象2
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);//实例化hbase对象
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"));
//get.addColumn(Bytes.toBytes("personal"),Bytes.toBytes("name"));
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();
}
}
五. 扫描表格内容(这里格式输出后设置的和hbase shell里一样【也许】,可自行更改)
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");
}
十.退出hbase shell
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");
}
总代码
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 {
// CreateTable();
//PutData();
//GetData();
//DeleteData();
//dropTable();
//Scan();
//put();
//ListTables();
//disableTable();
//ExitHbase_Shell();
}
//创建表格
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");
}
}
//插入数据1 一个一个插
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));
//table.put(put);//时间戳不同
//System.out.println("inserted record eid01 to table employee done");
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!");
//System.out.println("inserted record eid02 to table employee done");
}
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!");
}
}
//插入数据2 循环
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"));
//get.addColumn(Bytes.toBytes("personal"),Bytes.toBytes("name"));
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");
}
//退出hbase shell
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");
}
}