創建鏈接:java
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(); } } }
建立表:apache
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(); } } } }
列出全部表:oop
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(); } }
添加數據:spa
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(); } }
刪除數據:code
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獲取全部數據:blog
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())); } } } }