potter

少时诵诗书所所

HBase学习之路 (八)HBase的API操作

HBase API操作表和数据

package HomeWork1;

import java.io.IOException;
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.TableName;
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.util.Bytes;

import iter.HBaseDemoInterface;
import util.HBasePrintUtil;

public class Work1 implements HBaseDemoInterface{

	private static final String connectkey = "hbase.zookeeper.quorum";
	private static final String connectvalue = "potter2:2181,potter3:2181,potter4:2181,potter5:2181,";
	static Configuration conf = null;
	private static Admin admin = null;
	private static Connection con = null;
	static{
		conf = HBaseConfiguration.create();
		conf.set(connectkey, connectvalue);
		try {
			con = ConnectionFactory.createConnection(conf);
			admin = con.getAdmin();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// 查询所有表
	@Override
	public void getAllTables() throws Exception {
		
		HTableDescriptor[] listTables = admin.listTables();
		for(HTableDescriptor htd : listTables){
			
			System.out.print(htd.getTableName()+"\t");
		}
	}

	// 创建表,传参,表名和列簇的名字
	@Override
	public void createTable(String tableName, String[] family) throws Exception {
		TableName tn = TableName.valueOf(tableName);
		HTableDescriptor htd = new HTableDescriptor(tn);
		
		for (int i = 0; i < family.length; i++) {
			HColumnDescriptor cf1 = new HColumnDescriptor(family[i]);
			System.out.println(cf1+"888888888888888");
			htd.addFamily(cf1);
		}
			
		if (admin.tableExists(tn)) {
			System.out.println("表存在");
			System.exit(0);
		}else {
			admin.createTable(htd);
			System.out.println("创建成功");
		}
	}

	// 创建表,传参:封装好的多个列簇
	@Override
	public void createTable(HTableDescriptor htds) throws Exception {
		//获得表的名字
		String tablename = htds.getNameAsString();
		System.out.println(tablename);
		admin.createTable(htds);
	}

	// 创建表,传参,表名和封装好的多个列簇
	@Override
	public void createTable(String tableName, HColumnDescriptor hcds) throws Exception {
		
		HTableDescriptor ht = new HTableDescriptor(TableName.valueOf(tableName));
		
		ht.addFamily(hcds);
		admin.createTable(ht);
		
	}

	// 查看表的列簇属性
	@Override
	public void descTable(String tableName) throws Exception {
		
		HTableDescriptor ht = new HTableDescriptor(TableName.valueOf(tableName));
		HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf(tableName));
		System.out.println(tableDescriptor);
//		String string = tableDescriptor.toString();
//		System.out.println(string+"222222222222");
		
		
		/*Scan scan = new Scan();
		TableName tn = TableName.valueOf(tableName);
		ResultScanner rs = null;
		HTable table = new HTable(conf, tn);
		
		rs = table.getScanner(scan);
		for(Result r : rs){
			for(KeyValue kv : r.list()){
				
				System.out.println("行键 rowkey"+ "\t" +Bytes.toString(kv.getRow()));
				System.out.println("列簇" +"\t"+ Bytes.toString(kv.getFamily()));
				System.out.println("key" +"\t"+ Bytes.toString(kv.getQualifier()));
				System.out.println("value"+"\t"+Bytes.toString(kv.getValue()));
				System.out.println("时间戳" + "\t"+kv.getTimestamp());
				
			}
		}
		rs.close();*/
	}
			

	// 判断表存在不存在
	@Override
	public boolean existTable(String tableName) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		if (admin.tableExists(tn)) {
			System.out.println("表存在");
			return true;
		}else {
			System.out.println("表不存在");
			return false;
		}
	}

	// disable表
	@Override
	public void disableTable(String tableName) throws Exception {

		TableName tn = TableName.valueOf(tableName);
		
		boolean tableEnabled = admin.isTableEnabled(tn);
		if (tableEnabled) {
			admin.disableTable(tn);
		}
		
	}

	// drop表
	@Override
	public void dropTable(String tableName) throws Exception {
		TableName tn = TableName.valueOf(tableName);
//		disableTable();
		admin.deleteTable(tn);
		if (admin.tableExists(tn)) {
			System.out.println("删除表失败");
		}else {
			System.out.println("删除表成功");
		}
	}

	// 修改表(增加和删除)
	@SuppressWarnings("deprecation")
	@Override
	public void modifyTable(String tableName) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);

		HTableDescriptor td = new HTableDescriptor(tn);
		td.setName(Bytes.toBytes("sss"));
		
	}

	// 修改表(增加和删除)
	@Override
	public void modifyTable(String tableName, String[] addColumn, String[] removeColumn) throws Exception {
		TableName tn = TableName.valueOf(tableName);
//		Put put = new Put("rk04".getBytes());
		Table table = con.getTable(tn);
	
		Put put = new Put("rk04".getBytes());
//		HTableDescriptor htd = new HTableDescriptor(tn);
		for (int i = 0; i < addColumn.length; i++) {
			HColumnDescriptor cf = new HColumnDescriptor(addColumn[i]);
			
			put.addColumn(cf.getName(), "xxxx".getBytes(), "yy".getBytes());
		}
		
		
		Delete delete = new Delete("rk03".getBytes());
		for (int i = 0; i < removeColumn.length; i++) {
			HColumnDescriptor cf = new HColumnDescriptor(removeColumn[i]);
			delete.addColumn(cf.getName(), removeColumn[i].getBytes());
			table.delete(delete);
		}
		
		
	}
	// 修改表(增加和删除)
	@Override
	public void modifyTable(String tableName, HColumnDescriptor hcds) throws Exception {
		TableName tn = TableName.valueOf(tableName);
		
		
		
	}
	// 添加或者修改数据
	@SuppressWarnings("deprecation")
	@Override
	public void addData(String tableName, String rowKey, String[] column, 
			String[] value) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		//设置rowkey
		Put put = new Put(Bytes.toBytes(rowKey));
		//HTable负责跟记录相关的操作如增删改查等
		Table table = con.getTable(tn);
		//获取所有的列簇
		HColumnDescriptor[] columnfamily = table.getTableDescriptor().getColumnFamilies();
		System.out.println(columnfamily+"222222222");
		for (int i = 0; i < columnfamily.length; i++) {
			//获取列簇名
			String familyname = columnfamily[i].getNameAsString();
			System.out.println(familyname+"555555");
			//base_info列簇put数据
			if (familyname.equals("base_info")) {
				for (int j = 0; j < column.length; j++) {
					System.out.println(column[j]+"000000000000");
					put.add(Bytes.toBytes(familyname),
							Bytes.toBytes(column[j]),Bytes.toBytes(value[j]));
					System.out.println("成功");
				}
			}
			
		}
		table.put(put);
		
	}
	// 添加或者修改数据
	@SuppressWarnings("deprecation")
	@Override
	public void putData(String tableName, String rowKey, String familyName, String columnName,
			String value) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Put put = new Put(Bytes.toBytes(rowKey));
		Table table = con.getTable(tn);
		put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName),
				Bytes.toBytes(value));
//		put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
		table.put(put);
		
	}
	// 添加或者修改数据
	@Override
	public void putData(String tableName, String rowKey, String familyName, String columnName, 
			String value,long timestamp) throws Exception {
		TableName tn = TableName.valueOf(tableName);
		Put put = new Put(Bytes.toBytes(rowKey));
		Table table = con.getTable(tn);
		put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), timestamp, Bytes.toBytes(value));
		table.put(put);
		System.out.println("成功");
	}
	// 添加或者修改数据
	@Override
	public void putData(Put put) throws Exception {
		
		Table table = con.getTable(TableName.valueOf("user_info"));
		put.addColumn("base_info".getBytes(), "rrrr".getBytes(), "ppp".getBytes());
		table.put(put);
		System.out.println("成功");
	}
	// 添加或者修改数据
	@Override
	public void putData(List<Put> putList) throws Exception {
		
		Table table = con.getTable(TableName.valueOf("user_info"));
		Put put1 = new Put("rk07".getBytes());
		put1.addColumn("base_info".getBytes(), "kk1".getBytes(), "pp1".getBytes());
		Put put2 = new Put("rk077".getBytes());
		put2.addColumn("base_info".getBytes(), "kk2".getBytes(), "pp2".getBytes());
		
		putList.add(put1);
		putList.add(put2);
		table.put(putList);
		System.out.println("成功");
		
	}
	// 根据rowkey查询数据
	@Override
	public Result getResult(String tableName, String rowKey) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		Get get = new Get(Bytes.toBytes(rowKey));
//		System.out.println(get+"0000");
		Result result = table.get(get);
//		System.out.println(result+"2222222222222");
		HBasePrintUtil.printResult(result);
		
		
//		Cell[] rawCells = result.rawCells();
		
//		for(Cell cell : rawCells){
//			System.out.println(cell.toString());
//			System.out.println(cell.getF);
//		}
		
//		Scan scan = new Scan();
//		HTableDescriptor htd = new HTableDescriptor(tn);
		
		
		return result;
	}
	// 根据rowkey查询数据
	@Override
	public Result getResult(String tableName, String rowKey, String familyName) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addFamily(Bytes.toBytes(familyName));
		Result result = table.get(get);
		HBasePrintUtil.printResult(result);
		
		return null;
	}

	@Override
	public Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addFamily(Bytes.toBytes(familyName));
		get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
		
		Result result = table.get(get);
		HBasePrintUtil.printResult(result);
		
		return null;
	}
	// 查询指定version
	@Override
	public Result getResultByVersion(String tableName, String rowKey, String familyName, 
			String columnName, int versions) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn );
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addFamily(Bytes.toBytes(familyName));
		get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
		get.setMaxVersions(versions);
		Result result = table.get(get);
		HBasePrintUtil.printResult(result);
		return null;
	}
	// scan全表数据
	@Override
	public ResultScanner getResultScann(String tableName) throws Exception {
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		
		Scan scan = new Scan();
		ResultScanner scanner = table.getScanner(scan);
		HBasePrintUtil.printResultScanner(scanner);
		return null;
	}

	
	@Override
	public ResultScanner getResultScann(String tableName, Scan scan) throws Exception {
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		ResultScanner scanner = table.getScanner(scan);
		HBasePrintUtil.printResultScanner(scanner);
		return null;
	}
	// 删除数据(指定的列)
	@Override
	public void deleteColumn(String tableName, String rowKey) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		Delete delete = new Delete(Bytes.toBytes(rowKey));
		table.delete(delete);
		System.out.println("删除成功");
	}
	// 删除数据(指定的列)
	@Override
	public void deleteColumn(String tableName, String rowKey, String falilyName) throws Exception {
		
		TableName tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		Delete delete = new Delete(Bytes.toBytes(rowKey));
		delete.addFamily(Bytes.toBytes(falilyName));
		table.delete(delete);
		System.out.println("删除成功");
		
	}
	// 删除数据(指定的列)
	@Override
	public void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
		TableName  tn = TableName.valueOf(tableName);
		Table table = con.getTable(tn);
		Delete delete = new Delete(Bytes.toBytes(rowKey));
		delete.addColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
		table.delete(delete);
		System.out.println("删除成功");
	}


}

测试类:

package HomeWork1;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.yecht.Data.Str;

public class Test1 {

	public static void main(String[] args) throws Exception {
		
		
		
		Work1 work1 = new Work1();
		work1.getAllTables();
		
		/*String tableName = "blog";
		String[] family = {"article","author"};
		work1.createTable(tableName, family);*/
		
//		work1.descTable("user_info");
		
//		work1.existTable("aaa");
		
//		work1.disableTable("blog");
//		work1.dropTable("blog");
		
//		work1.modifyTable("stu");
		/*String[] addColumn = {"xxxx","xxxx"};
		String[] removeColumn = {"xxx"};
		work1.modifyTable("user_info", addColumn, removeColumn);*/
	
		/*String[] value = { "yyy","yyx"};
		String[] column = {"base_info","zzz"};
		work1.addData("user_info", "rk055", column,value);*/
				
		/*String rowKey = "rk022";
		String familyName = "extra_info";
		String columnName = "qqqqq";
		String value = "lllll";
		work1.putData("user_info", rowKey, familyName, columnName, value);
	*/
		
//		work1.putData("user_info", "rk08", "base_info", "ttt", "ooo", 999);
		
		
//		work1.putData(new Put("rk09".getBytes()));
		/*List<Put> pList = new ArrayList<>();
		work1.putData(pList);*/
	
		
//		work1.getResult("user_info", "zhangsan_20150701_0004");
		
//		work1.getResult("user_info", "zhangsan_20150701_0004", "base_info");
		
//		work1.getResult("user_info", "zhangsan_20150701_0004", "base_info","age");
		
		
//		work1.getResultByVersion("user_info", "zhangsan_20150701_0004", "base_info","age",4);
		
//		work1.getResultScann("user_info");
		
		/*Scan scan = new Scan();
		work1.getResultScann("user_info",scan);*/
		
//		work1.deleteColumn("user_info", "rk01");
		
//		work1.deleteColumn("user_info", "zhangsan_20150701_0001","base_info");
		
//		work1.deleteColumn("user_info", "rk07","base_info","kk1");
		
	}
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41851454/article/details/79954323
个人分类: HBase
上一篇HDFS读写文件流程
下一篇Scala使用(冒泡排序、单词计数、随机数)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭