`

java操作hbase

阅读更多

HBase提供了Java API对其进行管理,包括对表的管理、数据的操作等。 HBaseAdmin —— 对表的创建、删除、显示以及修改等。 HTable —— 通过HTable的实例来访问表并进行数据的操作。

package test;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class OperateTable {
	
	public static Configuration conf;
	
	static{
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum","master");
		conf.set("hbase.zookeeper.property.clientPort","2181");
	}
	
	public static void main(String[] args) throws Exception {
		
//		createTable("hanyiting");
//		insertData("hanyiting");
//		queryAll("hanyiting");
//		queryByCondition("hanyiting");
//		listAllTables();
		listTableFamily("test");
		
	}
	
	public static void createTable(String tableName){
		System.out.println("start create table ......");
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			if(admin.tableExists(tableName)){
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
				System.out.println(tableName + " is exist,delete ......");
			}
			
			HTableDescriptor desc = new HTableDescriptor(tableName);
			desc.addFamily(new HColumnDescriptor("column1"));
			desc.addFamily(new HColumnDescriptor("column2"));
			desc.addFamily(new HColumnDescriptor("column3"));
			admin.createTable(desc);
			admin.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("end create table ......");
	}
	
	public static void insertData(String tableName){
		System.out.println("start insert data ......");
		HTable table;
		try {
			table = new HTable(conf,tableName);
			Put put = new Put("112233bbcc".getBytes());
			put.add("column1".getBytes(),null,"aaa".getBytes());
			put.add("column2".getBytes(),null,"bbb".getBytes());
			put.add("column3".getBytes(),null,"ccc".getBytes());
		
			table.put(put);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void dropTable(String tableName){
		
		HBaseAdmin admin;
		try {
			admin = new HBaseAdmin(conf);
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void deleteRow(String tableName,String rowkey){
		try{
			HTable table = new HTable(conf, tableName);
			List list = new ArrayList();
			Delete d1 = new Delete(rowkey.getBytes());
			list.add(d1);
			
			table.delete(list);
			System.out.println("删除行成功!");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void queryAll(String tableName){
		
		try {
			HTable table = new HTable(conf, tableName);
			ResultScanner rs = table.getScanner(new Scan());
			for(Result r : rs){
				System.out.println("获得到rowkey:"+new String(r.getRow()));
				for(KeyValue keyvalue : r.raw()){
					System.out.println("列族:"+new String(keyvalue.getFamily())+"列:"+new String(keyvalue.getKey()
							+ "====值:"+new String(keyvalue.getValue())));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void queryByCondition(String tableName){
		try{
			
			HTable table = new HTable(conf, tableName);
			
			List<Filter> filters = new ArrayList<Filter>();
			Filter filter = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa"));
			filters.add(filter);
			
			FilterList filterList = new FilterList(filters);
			Scan scan = new Scan();
			scan.setFilter(filterList);
			ResultScanner rs = table.getScanner(scan);
			
			for(Result r : rs){
				System.out.println("获得到rowkey:"+new String(r.getRow()));
				for(KeyValue keyvalue : r.raw()){
					System.out.println("列族:"+new String(keyvalue.getFamily())+"列:"+new String(keyvalue.getKey()
							+ "====值:"+new String(keyvalue.getValue())));
				}
			}
			rs.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void listAllTables(){
		HBaseAdmin admin;
		try {
			admin = new HBaseAdmin(conf);
			TableName[] listTableNames = admin.listTableNames();  //表名
			System.out.println(listTableNames.toString());
			HTableDescriptor[] listTables = admin.listTables();  //整个表的结构
			System.out.println(listTables.toString());
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void listTableFamily(String tableName){
		try {
			HTable table = new HTable(conf, tableName);
			HTableDescriptor tableDescriptor = table.getTableDescriptor();
			Collection<HColumnDescriptor> families = tableDescriptor.getFamilies();
			for(HColumnDescriptor cdesc : families){
				System.out.println(cdesc.getNameAsString());
			}
			System.out.println(tableDescriptor);
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics