hbase基础java操作
package util; 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://localhost:9000/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)); } }
package util;
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://localhost:9000/hbase");try {connection = ConnectionFactory.createConnection(configuration);admin = connection.getAdmin();}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}//关闭链接public static void close() {try {if(admin!=null) {admin.close();}if(null!=connection) {connection.close();}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}
//**********命名空间操作**********//创建namespacepublic static void createNamespace(String namespace) throws IOException {getConnection();NamespaceDescriptor nDescriptor = NamespaceDescriptor.create(namespace).build();admin.createNamespace(nDescriptor);close();}//查询所有的namespacepublic 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;}//删除namespacepublic static boolean dropNamespace(String namespace) {getConnection();try {admin.deleteNamespace(namespace);close();return true;} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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 blocke.printStackTrace();return null;}}//**********显示内容**********//显示scanpublic 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 blocke.printStackTrace();return null;}}//显示resultpublic 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 blocke.printStackTrace();}}//迭代器resultpublic 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 blocke.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));}}