hbase的基本操作

关于在Linux系统下,对hbase数据库的增删改查,下列方法几乎全部包括了:

package com.HBase2.Dao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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;
import com.HBase2.Bean.Bean;



public class Dao {
	public static Connection connection;
	public static Configuration configuration;
	public static Admin admin;
	//**********连接操作**********
	//连接
	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();
		}
	}
	//***********命名空间************
	//创建namespace
	public static void createNamespace(String namespace) throws IOException {
		init();
		NamespaceDescriptor nDescriptor = NamespaceDescriptor.create(namespace).build();
		admin.createNamespace(nDescriptor);
	}
	//查询所有namespace
	public static ArrayList<String> listNamespace() throws IOException{
		init();
		NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
		ArrayList<String> list = new ArrayList<>();
		for(NamespaceDescriptor nd:namespaceDescriptors) {
			System.out.println(nd.getName());
			list.add(nd.getName());
		}
		return list;
	}
	//删除namespace
	public static boolean dropNamespace(String namespace) {
		init();
		try {
			admin.deleteNamespace(namespace);
			close();
			return true;
		}catch(IOException e) {
			e.printStackTrace();
			close();
			return false;
		}
	}
	//************表操作************
	//建表
	public static void createTable(String student,String []score) throws IOException {
		init();
		TableName tableName = TableName.valueOf(student);
		if(admin.tableExists(tableName)) {
			System.out.println("该表已存在");
		}
		else {
			HTableDescriptor hTableDescriptor = new HTableDescriptor(student);
			for(String str:score) {
				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
				hTableDescriptor.addFamily(hColumnDescriptor);
			}
			admin.createTable(hTableDescriptor);
		}
	}
	//增加
	public static boolean insertData(String tablename,String rowkey,String colFamily,String col,String val){
		init();
		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 void getData(String tableName,String rowKey,String colFamily,String col) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
		//获取的result数据是结果集,还需要格式化输出想要的数据才行
		Result result = table.get(get);
		System.out.println(new String(result.getValue(colFamily.getBytes(), col==null?null:col.getBytes())));
		table.close();
	}
	//获取所有列族
	public static ArrayList<String> listColFamilies(String tablename) throws TableNotFoundException, IOException{
	    init();
		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 void scanColumn(String tableName,String column) throws IOException
 {
	 init();
	 Table table = connection.getTable(TableName.valueOf(tableName));
	 Scan scan = new Scan();
	 scan.addFamily(Bytes.toBytes(column));
	 ResultScanner scanner = table.getScanner(scan);
	 for(Result result = scanner.next();result !=null;result = scanner.next())
	 {
		 showCell(result);	
	 }
	 table.close();
	 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("colFamily"+new String(CellUtil.cloneFamily(cell))+" ");
		 System.out.println("row name:"+new String(CellUtil.cloneQualifier(cell))+" ");
		 System.out.println("value"+new String(CellUtil.cloneValue(cell))+" ");
	 }
 }
	//扫描数据
			public static Iterator<Result> scan(String tablename) {
				init();
				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;
				}
			}
	//删除列族
			public static boolean deleteColFamily(String tablename,String colname) {
				init();
				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;
				}
			}
			//删除行
			public static void deleteRow(String tableName, String row) throws IOException {
				init();
				Table table = connection.getTable(TableName.valueOf(tableName));
				Delete delete = new Delete(row.getBytes());
				table.delete(delete);
				table.close();
				close();
			}
			//**********显示内容**********
			//显示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 Bean showResult(Result result) {
				CellScanner cellScanner = result.cellScanner();		
				Bean bean = new Bean();
				Map<String,String> map = new HashMap<String,String>();
				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);
						map.put(qualifier, value);
					}
					bean.setAddress(map.get("address"));
					bean.setArea(map.get("area"));
					bean.setBeginyear(map.get("beginyear"));
					bean.setCode(map.get("code"));
					bean.setEmail(map.get("email"));
					bean.setEndyear(map.get("endyear"));
					bean.setFax(map.get("fax"));
					bean.setField(map.get("field"));
					bean.setHezuoyixiang(map.get("hezuoyixing"));
					bean.setInstiution(map.get("instiution"));
					bean.setJsxqjjfs(map.get("jsxqjjfs"));
					bean.setKeyword(map.get("keyword"));
					bean.setLinkman(map.get("linkman"));
					bean.setMoney(map.get("money"));
					bean.setName(map.get("name"));
					bean.setPhone(map.get("phone"));
					bean.setProinstiution(map.get("proinstiution"));
					 bean.setRepresent(map.get("represent"));
					 bean.setRetype(map.get("retype"));
					 bean.setSubject(map.get("subject"));
					 bean.setSumdemand(map.get("sumdemand"));
					 bean.setTechdemand_coopera(map.get("techdemand_cooper"));
					 bean.setTechdemand_use(map.get("techdemand_use"));
					 bean.setUrl(map.get("url"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return bean;
			}
			//迭代器result
			public static ArrayList<Bean> showIterResult(Iterator<Result> iterator) {
			  ArrayList<Bean> list = new ArrayList<>();
			 
				while(iterator.hasNext()) {
					Result result = iterator.next();
					list.add(showResult(result));
				}
				for(int i=0;i<list.size();i++)
				{
				System.out.println(list.get(i).getAddress());					
				}
				return list;
			}
			//**********过滤器**********
			
			//创建单列值过滤器
			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);
				init();
				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 {
		// TODO Auto-generated method stub
		//String[] colFamily = new String[] {"score","age"};
//		String tablename = "people";
//		createTable(tablename,colFamily);
//		insertData("majortech","001","colFamily","name","石家庄铁道大学");
//		insertData("majortech","001","colFamily","address","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","url","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","email","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","represent","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","code","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","linkman","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","phone","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","fax","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","instiution","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","proinstiution","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","techdemand","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","sumdemand","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","retype","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","subject","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","field","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","techdemand_use","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","techdemand_cooper","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","jsxqjjfs","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","hezuoyixiang","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","money","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","beginyear","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","endyear","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","keyword","shijiazhaungtiedaodaxue");
//		insertData("majortech","001","colFamily","area","shijiazhaungtiedaodaxue");
//		getData("majortech", "001", "colFamily", "name");
//	insertData("student", "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("colFamily", "techdemand",CompareFilter.CompareOp.GREATER , "鐜嬪缓姘�", true);
//		SingleColumnValueFilter s2 = singleColumnValueFilter("colFamily", "area",CompareFilter.CompareOp.EQUAL , "130100", true);
//		SingleColumnValueFilter s3 = singleColumnValueFilter("colFamily", "name",CompareFilter.CompareOp.EQUAL , "鐭冲�搴勯搧閬撳ぇ瀛�", true);
//		SingleColumnValueFilter s4 = singleColumnValueFilter("colFamily", "keyword",CompareFilter.CompareOp.EQUAL , "鍗曚綅v鍔ㄤ笉鍔ㄥ氨鎴戠殑", true);
//		SingleColumnValueFilter[] filters = new SingleColumnValueFilter[] {s1,s2,s3,s4};
//		showIterResult(filterList("majortech", "or", filters));

//		showIterResult(scan("majortech"));
//		String student = "student";
//		String score[] = {"english","math","chinese"};
//		createTable(student,score);

//		insertData("student","001","english","English","90");
//		insertData("student","001","chinese","Chinese","80");
//		insertData("student","001","math","Math","90");
 //       scanColumn("student","english");
		
		
//     	insertData("student","001","english","English2","85");	 
//        	insertData("student","001","english","English","85");
//		deleteRow("student","001");
//		insertData("Student","scofield","score","English","45");
//		insertData("Student","scofield","score","Math","89");
//		insertData("Student","scofield","score","Computer","100");
		getData("Student", "scofield", "score", "English");
	}
}

  

原文地址:https://www.cnblogs.com/yangqqq/p/13947286.html