j將配置填入idea 的配置文件當中 等待idea 直接下載安裝即可
zookeeper move 配置 :版本不同 里面的zookeeper的版本要根據自己的版本來更改
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.8</version> </dependency> </dependencies>
Hbase 配置
<dependencies> <!-- hbase依賴--> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.2.6</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.2.6</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>2.2.6</version> </dependency> </dependencies>
IDEA 對HBASE進行的增刪改查操作:
1.個人筆記所寫:
package com.hadoop.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.protobuf.generated.ClientProtos; import org.apache.hadoop.hbase.util.Bytes; import org.checkerframework.checker.units.qual.C; import org.omg.CORBA.StringHolder; import java.io.IOException; /** * DDL: *1.判斷表是否存在 * 2.創建命名空間 * 3.創建命名空間 * 4.刪除表 * * DML: * 5.插入數據 * 6.查數據(get) * 7.查數據 (scan) * 8.刪除數據 */ public class HbaseAPI { private static Connection connection =null; private static Admin admin=null; static { try { // 獲取賦值信息 Configuration configuration=HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104"); //創建連接對象 connection=ConnectionFactory.createConnection(configuration); //創建Admin 對象 admin =connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } // 1 .判斷表是否存在 public static boolean isTableExist(String tablename) throws IOException { //獲取配置信息 ///HBaseConfiguration configuration =new HBaseConfiguration(); Configuration configuration= HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104"); configuration.set("hbase.zookeeper.property.clientPort", "2181"); //獲取管理員對象 Connection connection = ConnectionFactory.createConnection(configuration); Admin admin = connection.getAdmin(); //HBaseAdmin admin = new HBaseAdmin(configuration); //判斷表是否存在 boolean exists = admin.tableExists(TableName.valueOf(tablename)); //返回結果 return exists; } // 2. 創建表 可變形參 public static void creatTable(String tablename,String... cfs) throws IOException { //判斷是否存在列族信息 if(cfs.length <=0){ System.out.println("請設置列族信息!"); return; } //表存在 if (isTableExist(tablename)){ System.out.println(tablename+"已經存在!"); return; } //創建表描述器 HTableDescriptor hTableDescriptor= new HTableDescriptor(TableName.valueOf(tablename)); //循環添加列族信息 for (String cf:cfs){ //創建列族信息 HColumnDescriptor hColumnDescriptor= new HColumnDescriptor(cf); //添加具體列族信息 hTableDescriptor.addFamily(hColumnDescriptor); } //創建表 admin.createTable(hTableDescriptor); } // 3. 刪除表 public static void dropTable(String tableName) throws IOException { //表是否存在 if(!isTableExist(tableName)){ System.out.println(tableName+"表不存在!"); return; } //讓表下線 admin.disableTable(TableName.valueOf(tableName)); //刪除表 admin.deleteTable(TableName.valueOf(tableName)); System.out.println("刪除表成功。"); } // 4. 創建命名空間 public static void createNameSpace(String ns){ //創建命名空間描述器 NamespaceDescriptor namespaceDescriptor =NamespaceDescriptor.create(ns).build(); //創建命名空間 try { admin.createNamespace(namespaceDescriptor); System.out.println("命名空間創建成功。"); }catch (NamespaceNotFoundException e){ System.out.println(ns+"命名空間已存在!"); } catch (IOException e) { e.printStackTrace(); } // System.out.println("雖然存在,仍舊到這里了"); } //5. 向表插入數據 public static void putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException { // 獲取表對象 Table table = connection.getTable(TableName.valueOf(tableName)); // 創建put 對象 Put put = new Put(Bytes.toBytes(rowKey)); //給put 對象賦值 put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value)); //批量添加 // put.addColumn(Bytes.toBytes(cf),Bytes.toBytes("sex"),Bytes.toBytes("male")); //插入數據 table.put(put); //關閉連接 table.close(); } //6.獲取數據 (get) public static void getData(String tableName, String rowKey, String cf,String cn) throws IOException { //獲取表對象 Table table= connection.getTable(TableName.valueOf(tableName)); //創建Get對象 Get get = new Get(Bytes.toBytes(rowKey)); //2.1 獲取指定列族 //get.addFamily(Bytes.toBytes(cf)); //2.2 獲取指定列和族 // get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn)); //2.3 獲取數據的版本數 // get.getMaxVersions(); //獲取數據 Result result = table.get(get); //解析result 並打印 for (Cell cell : result.rawCells()){ //打印數據 System.out.println("CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+ ",CN:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+ ",Value:"+Bytes.toString(CellUtil.cloneValue(cell))); } //關閉表連接 table.close(); } //7 獲取數據 scan public static void scanTable(String tableName) throws IOException { //獲取表對象 Table table= connection.getTable(TableName.valueOf(tableName)); //構建Scan 對象 // Scan scan = new Scan(); // 左閉右開 Scan scan = new Scan(Bytes.toBytes("1001"),Bytes.toBytes("1002")); //掃描表 ResultScanner scanner = table.getScanner(scan); //解析resultScanner for (Result result :scanner){ //解析 result 並打印 for(Cell cell : result.rawCells()){ //打印數據 System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))+",CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+ ",CN:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+ ",Value:"+Bytes.toString(CellUtil.cloneValue(cell))); } } //關閉表連接 table.close(); } //8.刪除數據 public static void deleteData(String tableName,String rowKey,String cf,String cn) throws IOException { //獲取表對象 Table table = connection.getTable(TableName.valueOf(tableName)); //構建刪除對象 Delete delete = new Delete(Bytes.toBytes(rowKey)); //2.1設置刪除的列 // delete.addColumn(); // delete.addColumns(); //刪除操作 table.delete(delete); //關閉連接 table.close(); } //關閉連接 public static void close() { if (admin!=null){ try { admin.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection!=null){ try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { //測試表是否存在 // System.out.println(isTableExist("XuQiuDemand")); //創建表測試 // creatTable("XuQiuDemand","infos"); //System.out.println(isTableExist("XuQiuDemand")); //刪除表操作 // dropTable("stu5"); //創建命名空間測試 // createNameSpace("cuixingyu"); //創建數據測試 String sjz="test"; putData("stu","1500","info2","names",sjz); //獲取單行數據 // getData("stu","1001","info2","phone"); // System.out.println(isTableExist("stu5")); //scanTable("stu"); //測試刪除 // deleteData("stu","1005","ss","ss");‘、+ close(); } }
2.大佬同學所寫
package com.hadoop.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; 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 HbaseUtil { private static ThreadLocal<Connection> connHolder= new ThreadLocal<Connection>(); /** * 創建connection * @throws IOException */ public static void makeHbaseConnection() throws IOException { Connection connection = connHolder.get(); if (connection == null){ Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104"); conf.set("hbase.zookeeper.property.clientPort", "2181"); connection = ConnectionFactory.createConnection(conf); connHolder.set(connection); } } /** * 關閉連接 * @throws IOException */ public static void closeHbseConn() throws IOException { Connection connection = connHolder.get(); if (connection != null){ connection.close(); connHolder.remove(); } } /** * 添加一行數據 * @param tableName 表名 * @param rowKey 行號 * @param family 列族 * @param column 列名 * @param value * @throws IOException */ public static void insertData(String tableName,String rowKey,String family,String column,String value) throws IOException { //獲取連接 Connection connection = connHolder.get(); //獲取表對象 Table table = connection.getTable(TableName.valueOf(tableName)); //獲取添加對象 Put put = new Put(Bytes.toBytes(rowKey)); //添加一列 put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value)); //添加 table.put(put); //關閉 table.close(); } /** * 創建表 * @param tableName * @param family * @throws IOException */ public static void createTable(String tableName,String family) throws IOException { Connection connection = connHolder.get(); //獲取admin Admin admin = connection.getAdmin(); //列族描述對象建造者 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)); //設置最大版本號 columnFamilyDescriptorBuilder.setMaxVersions(3); //列族描述對象 ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build(); //表描述對象建造者 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); //將列族對象添加進表描述 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); //創建表描述對象 TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); //創建表 admin.createTable(tableDescriptor); admin.close(); } /** * 創建表(多列族) * @param tableName * @param familys * @throws IOException */ public static void createTable(String tableName,String[] familys) throws IOException { Connection connection = connHolder.get(); //獲取admin Admin admin = connection.getAdmin(); //表描述對象建造者 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); for (String family : familys) { //列族描述對象建造者 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)); //設置最大版本號 columnFamilyDescriptorBuilder.setMaxVersions(3); //將列族對象添加進表描述 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); } //創建表描述對象 TableDescriptor tableDescriptor = tableDescriptorBuilder.build(); //創建表 admin.createTable(tableDescriptor); admin.close(); } /** * 根據行號查詢數據 * @param tableName * @param rowKey * @return * @throws IOException */ public static Result selectDataByRowkey(String tableName,String rowKey) throws IOException { Connection connection = connHolder.get(); //獲取表 Table table = connection.getTable(TableName.valueOf(tableName)); //獲取表描述對象 Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); return result; } /** * 獲取某一列族中某一列的某一行數據 * @param tableName * @param rowKey * @param family * @param column * @return * @throws IOException */ public static Result selectDataByCol(String tableName,String rowKey,String family,String column) throws IOException { Connection connection = connHolder.get(); //獲取表 Table table = connection.getTable(TableName.valueOf(tableName)); //獲取表描述對象 Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column)); Result result = table.get(get); return result; } /** * 打印result * @param result */ public static void showResult(Result result){ Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); } } /** * 刪除表 * @param tableName * @throws IOException */ public static void deleteTable(String tableName) throws IOException { Connection connection = connHolder.get(); Admin admin = connection.getAdmin(); TableName name = TableName.valueOf(tableName); if (admin.tableExists(name)){ admin.deleteTable(name); } } /** * 是否存在表 * @param tableName * @return * @throws IOException */ public static boolean tableExists(String tableName) throws IOException { Connection connection = connHolder.get(); Admin admin = connection.getAdmin(); return admin.tableExists(TableName.valueOf(tableName)); } }
另一位大佬所寫(比較全面):

package com.hadoop.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellScanner; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; 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 org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; public class DBUtil { public static Configuration configuration; public static Connection connection; public static Admin admin; //**********連接操作********** //建立鏈接 public static void getConnection() { configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } //關閉鏈接 public static void close() { try { if(admin!=null) { admin.close(); } if(null!=connection) { connection.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } //**********命名空間操作********** //創建namespace public static void createNamespace(String namespace) throws IOException { getConnection(); NamespaceDescriptor nDescriptor = NamespaceDescriptor.create(namespace).build(); admin.createNamespace(nDescriptor); close(); } //查詢所有的namespace public static ArrayList<String> listNamespace() throws IOException { getConnection(); NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors(); ArrayList<String> list = new ArrayList<>(); for(NamespaceDescriptor nd:namespaceDescriptors) { System.out.println(nd.getName()); list.add(nd.getName()); } close(); return list; } //獲取指定namespace中所有的表名 public static ArrayList<String> listTables(String namespace) throws IOException { getConnection(); HTableDescriptor[] tables = admin.listTableDescriptorsByNamespace(namespace); ArrayList<String> list = new ArrayList<>(); for(HTableDescriptor table:tables) { // System.out.println(table.getNameAsString()); list.add(table.getTableName().getNameAsString()); } close(); return list; } //刪除namespace public static boolean dropNamespace(String namespace) { getConnection(); try { admin.deleteNamespace(namespace); close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); close(); return false; } } //**********表操作********** //創建表 public static void createTable(String tablename,String[] colFamily) throws IOException { getConnection(); TableName tName = TableName.valueOf(tablename); if(admin.tableExists(tName)) { System.out.println("table exists"); }else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tName); for(String str:colFamily) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); } close(); } //獲取所有列族 public static ArrayList<String> listColFamilies(String tablename) throws TableNotFoundException, IOException{ getConnection(); HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf(tablename)); HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies(); ArrayList<String> list = new ArrayList<>(); for(HColumnDescriptor hcd:columnDescriptors) { System.out.println(hcd.getNameAsString()); list.add(hcd.getNameAsString()); } return list; } //刪除列族 public static boolean deleteColFamily(String tablename,String colname) { getConnection(); try { admin.deleteColumn(TableName.valueOf(tablename), Bytes.toBytes(colname)); close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); close(); return false; } } //刪除表 @SuppressWarnings("finally") public static boolean dropTable(String tablename) { getConnection(); try { HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(tablename)); if(admin.tableExists(TableName.valueOf(tablename))) { if(admin.isTableEnabled(TableName.valueOf(tablename))) { admin.disableTable(TableName.valueOf(tablename)); } admin.deleteTable(TableName.valueOf(tablename)); } close(); return true; } catch (TableNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { close(); return false; } } //添加數據(表名,行健,列族,列名,值)/如果存在則修改 public static boolean insertData(String tablename,String rowkey,String colFamily,String col,String val){ getConnection(); Table table; try { table = connection.getTable(TableName.valueOf(tablename)); Put put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val)); table.put(put); table.close(); close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); close(); return false; } } //刪除數據(表名,行健) public static boolean deleteData(String tablename,String rowkey){ getConnection(); Table table; try { table = connection.getTable(TableName.valueOf(tablename)); Delete delete = new Delete(Bytes.toBytes(rowkey)); table.delete(delete); table.close(); close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); close(); return false; } } //瀏覽數據 public static void getData(String tablename,String rowkey,String colFamily,String col) throws IOException { getConnection(); Table table = connection.getTable(TableName.valueOf(tablename)); Get get = new Get(Bytes.toBytes(rowkey)); get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col)); Result result = table.get(get); System.out.println(new String(result.getValue(colFamily.getBytes(), col==null?null:col.getBytes()))); table.close(); close(); } //掃描數據 public static Iterator<Result> scan(String tablename) { getConnection(); try { Table table = connection.getTable(TableName.valueOf(tablename)); Scan scan = new Scan(); // scan.setStartRow(Bytes.toBytes(startRow)); // scan.setStopRow(Bytes.toBytes(endrow)); ResultScanner scanner = table.getScanner(scan); Iterator<Result> iterator = scanner.iterator(); return iterator; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } //**********顯示內容********** //顯示scan public static Iterator<Result> showScan(Table table,Scan scan) { ResultScanner scanner; try { scanner = table.getScanner(scan); Iterator<Result> iterator = scanner.iterator(); return iterator; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } //顯示result public static void showResult(Result result) { CellScanner cellScanner = result.cellScanner(); try { while(cellScanner.advance()) { Cell cell = cellScanner.current(); String rowkey = new String(CellUtil.cloneRow(cell)); String colFamily = new String(CellUtil.cloneFamily(cell)); String qualifier = new String(CellUtil.cloneQualifier(cell)); String value = new String(CellUtil.cloneValue(cell)); System.out.println(rowkey+":"+colFamily+"."+qualifier+"="+value); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //迭代器result public static void showIterResult(Iterator<Result> iterator) { while(iterator.hasNext()) { Result result = iterator.next(); showResult(result); } } //**********過濾器********** //創建單列值過濾器 public static SingleColumnValueFilter singleColumnValueFilter(String family,String qualifier,CompareFilter.CompareOp compareOp,String value,boolean isNull) { SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(qualifier), compareOp, Bytes.toBytes(value)); filter.setFilterIfMissing(isNull); return filter; } //過濾器鏈 public static Iterator<Result> filterList(String tablename,String type,SingleColumnValueFilter[] lists) { FilterList filterList = null; if(type.equals("and")) { filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); }else if(type.equals("or")){ filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); } for(SingleColumnValueFilter filter:lists) { filterList.addFilter(filter); } Scan scan = new Scan(); scan.setFilter(filterList); getConnection(); try { Table table = connection.getTable(TableName.valueOf(tablename)); return showScan(table, scan); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public static void main(String[] args) throws IOException { // String[] colFamily = new String[] {"score","age"}; // String tablename = "people"; // createTable(tablename,colFamily); // insertData(tablename, "001", "score", "english", "60"); // insertData(tablename, "001", "score", "math", "90"); // insertData(tablename, "001", "age", "now", "20"); // insertData(tablename, "001", "age", "last", "19"); // getData(tablename, "001", "score", "english"); // insertData(tablename, "001", "score", "english", "90"); // getData(tablename, "001", "score", "english"); // deleteData(tablename, "001", "score", "english"); // createNamespace("zdm"); // dropNamespace("ns3"); // listNamespace(); // listTables("default"); // deleteColFamily("people", "age"); // listColFamilies("people"); // dropTable("people"); // listTables("default"); SingleColumnValueFilter s1 = singleColumnValueFilter("score", "math",CompareFilter.CompareOp.GREATER , "20", true); SingleColumnValueFilter s2 = singleColumnValueFilter("age", "",CompareFilter.CompareOp.EQUAL , "70", true); SingleColumnValueFilter[] filters = new SingleColumnValueFilter[] {s1,s2}; showIterResult(filterList("people", "and", filters)); } }