建立连接:
package Init;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class Init {
public static Configuration configuration;//Hbase配置信息
public static Connection connection;//Hbase连接
public static Admin admin;
/* public static void main (String [] agrs) throws IOException{
init();//建立连接
close();//关闭连接
}*/
public static void init()
{
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch(IOException e){
e.printStackTrace();
}
}
public static void close()
{
try{
if(admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
创建表:
import java.io.IOException;
import org.apache.hadoop.hbase.*;
import java.util.Scanner;
import Init .Init;
public class CreateTable {
public static void main (String [] args) throws IOException{
Scanner sc=new Scanner (System.in);
System.out.println("请输入表名:");
String tbname=sc.nextLine();//表名
System.out.println("请确定列族数:");
int n=sc.nextInt();//确定列族
String [] strArray = new String [n];
for(int i=0;i<n;i++)
{
System.out.println("请输入列族名:");
sc=new Scanner (System.in);//对sc对象初始化
String col=sc.nextLine();//列族属性
strArray[i]=col;
}
sc.close();
createTable(tbname,strArray);//建表
}
public static void createTable(String myTableName,String[] colFamily) throws IOException {
Init.init();//调用工具类
TableName tableName = TableName.valueOf(myTableName);
if(Init.admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
Init.admin.createTable(hTableDescriptor);
System.out.println("create table success");
}
Init.close();
}
}
修改表(列族名或者列名):
import java.io.IOException;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.Scanner;
import Init.Init;
public class AlterTable {
public static void main(String [] args) throws IOException
{
meau();
/*Scanner sc=new Scanner(System.in);
System.out.println("请输入创建数量");
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
sc=new Scanner(System.in);
System.out.println("请输入修改表名");
String tb_name=sc.nextLine();
System.out.println("请输入列族名");
String colFamily=sc.nextLine();
addColFamily(tb_name,colFamily);
}*/
//delColFamily("Score","course");
}
public static void meau() throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("请选择:1.增加列族!!!2.删除列族!!!");
int n=sc.nextInt();
switch(n)
{
case 1:
sc=new Scanner(System.in);
System.out.println("请输入创建数量");
int a=sc.nextInt();
for(int i=0;i<a;i++)
{
sc=new Scanner(System.in);
System.out.println("请输入修改表名");
String tb_name=sc.nextLine();
System.out.println("请输入列族名");
String colFamily=sc.nextLine();
addColFamily(tb_name,colFamily);
};
case 2:
sc=new Scanner(System.in);
System.out.println("请输入删除数量");
int b=sc.nextInt();
for(int i=0;i<b;i++)
{
sc=new Scanner(System.in);
System.out.println("请输入修改表名");
String tb_name=sc.nextLine();
System.out.println("请输入列族名");
String colFamily=sc.nextLine();
delColFamily(tb_name,colFamily);
};
}
}
public static void addColFamily(String tableName, String colFamily)throws IOException
{
Init.init();
TableName tablename=TableName.valueOf(tableName);
//判断表是否存在
if (Init.admin.tableExists(tablename)) {
try {
Init.admin.disableTable(tablename);
HTableDescriptor tb_des = Init.admin.getTableDescriptor (tablename ); //获取表的描述
HColumnDescriptor newcol = new HColumnDescriptor( colFamily );//创建列族添加对象;为添加对象赋值
tb_des.addFamily(newcol);//添加列族
Init.admin.modifyTable(tablename, tb_des);
Init. admin.enableTable(tablename);
System.out.println(colFamily+"创建成功");
Init.close();
System.out.println("是否继续修改:1.继续修改!!!2.退出!!!");
Scanner sc=new Scanner(System.in);
int n =sc. nextInt();
if(n==1)
{
meau();
}
else
{
System.out.println("谢谢使用");
}
}catch (Exception e) {
// TODO : handle exception
e.printStackTrace();
}
}
}
public static void delColFamily(String tableName, String colFamily)throws IOException
{
Init.init();
TableName tablename=TableName.valueOf(tableName);
//判断表是否存在
if (Init.admin.tableExists(tablename)) {
try {
Init.admin.disableTable(tablename);
HTableDescriptor tb_des = Init.admin.getTableDescriptor (tablename ); //获取表的描述
tb_des.removeFamily(Bytes. toBytes (colFamily ));
Init.admin.modifyTable(tablename, tb_des);
Init. admin.enableTable(tablename);
System.out.println(colFamily+"删除成功");
Init.close();
System.out.println("是否继续修改:1.继续修改!!!2.退出!!!");
Scanner sc=new Scanner(System.in);
int n =sc. nextInt();
if(n==1)
{
meau();
}
else
{
System.out.println("谢谢使用");
}
}catch (Exception e) {
// TODO : handle exception
e.printStackTrace();
}
}
}
}
列出所有表:
import java.io.IOException;
import org.apache.hadoop.hbase.HTableDescriptor;
import Init.Init;
public class ListTable {
public static void main(String [] args) throws IOException
{
listTable();
}
public static void listTable() throws IOException {
Init.init();
HTableDescriptor hTableDescriptors[] = Init.admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
Init. close();
}
}
添加数据:
import java.io.IOException;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.util.Scanner;
import Init.Init;
public class InsertData {
public static void main( String [] args ) throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("请确定添加数量:");
int n =sc.nextInt();//确定添加数量
for(int i= 0;i<n;i++)
{
sc=new Scanner(System.in);//初始化sc
System.out.println("请输入添加数据的表名:");
String tb_Name=sc.nextLine();//选择表
System.out.println("请输入行键:");
String tb_rowKey=sc.nextLine();
System.out.println("请输入列族:");
String tb_colFamily=sc.nextLine();
System.out.println("请输入列:");
String tb_col=sc.nextLine();
System.out.println("请输入数据:");
String tb_val=sc.nextLine();
insertData(tb_Name,tb_rowKey,tb_colFamily,tb_col,tb_val);//添加数据
}
sc.close();
}
public static void insertData(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
Init.init();
Table table = Init.connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());
table.put(put);
table.close();
System.out.println("数据添加成功!");
Init.close();
}
}
删除数据:
import java.io.IOException;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.util.Scanner;
import Init.Init;
public class DelData {
public static void main(String [] args) throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("请确定删除数量:");
int n =sc.nextInt();//确定添加数量
for(int i= 0;i<n;i++)
{
sc=new Scanner(System.in);//初始化sc
System.out.println("请输入删除数据的表名:");
String tb_Name=sc.nextLine();//选择表
System.out.println("请输入行键:");
String tb_rowKey=sc.nextLine();
System.out.println("请输入列族:");
String tb_colFamily=sc.nextLine();
System.out.println("请输入列:");
String tb_col=sc.nextLine();
deleteData(tb_Name,tb_rowKey,tb_colFamily,tb_col);//添加数据
}
sc.close();
}
public static void deleteData(String tableName,String rowKey,String colFamily,String col) throws IOException {
Init. init();
Table table = Init.connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
// 删除指定列族的所有数据
delete.addFamily(colFamily.getBytes());
//删除指定列的数据
delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
table.close();
System.out.println("删除成功");
Init.close();
}
}
Get获取单个数据:
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Scanner;
import Init.Init;
public class GetData {
public static void main(String [] args) throws IOException
{
Scanner sc=new Scanner (System.in);
System.out.println("请输入查找数量:");
int n = sc.nextInt();
for(int i=0;i<n;i++)
{
sc=new Scanner(System.in);//sc对象初始化
System.out.println("请输入查找表名:");
String tb_Name=sc.nextLine();
System.out.println("请输入行键:");
String tb_rowKey=sc.nextLine();
System.out.println("请输入列族:");
String tb_colFamily=sc.nextLine();
System.out.println("请输入列:");
String tb_col=sc.nextLine();
getData(tb_Name,tb_rowKey,tb_colFamily,tb_col);
}
sc.close();
}
public static void getData(String tableName,String rowKey,String colFamily,String col)throws IOException
{
Init.init();
Table table =Init. connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);//返回到结果集
// showCell(result);
Cell[] cells = result.rawCells();
for(Cell cell:cells)
{
System.out.println("行键:"+new String(CellUtil.cloneRow(cell))+" ");
// System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println(" 列族:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println(" 列:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("数据:"+new String(CellUtil.cloneValue(cell))+" ");
table.close();
Init. close();
}
}
}
//将结果集的值遍历输出
/*public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}*/
Scan获取所有数据:
import java.io.IOException;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
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.client.Table;
import java.util.Scanner;
import Init.Init;
public class ScanData {
public static void main(String [] args) throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入查询表的数量");
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
sc=new Scanner(System.in);
System.out.println("请输入查询的表名");
String tb_name=sc.nextLine();
scanData(tb_name);
}
sc.close();
}
public static void scanData(String tableName)throws IOException
{
Scan scan = new Scan();
//scan.addColumn(Bytes.toBytes(""),Bytes.toBytes("")); 添加列族;列的约束查询
Init.init();
Table table = Init.connection.getTable(TableName.valueOf(tableName));
ResultScanner resultScanner = table.getScanner(scan);
Result rs = resultScanner.next();
for (; rs != null;rs = resultScanner.next()){
for (KeyValue kv : rs.list()){
System.out.println("--------------");
System.out.println("行键:"+ new String(kv.getRow()));
System.out.println("列族: "+ new String(kv.getFamily()));
System.out.println("列 :" + new String(kv.getQualifier ()));
System.out.println("数值 :"+ new String(kv.getValue()));
}
}
}
}