温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Hbase的测试代码是怎样的

发布时间:2021-12-09 13:43:04 来源:亿速云 阅读:122 作者:iii 栏目:云计算

这篇文章主要介绍“Hbase的测试代码是怎样的”,在日常操作中,相信很多人在Hbase的测试代码是怎样的问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Hbase的测试代码是怎样的”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.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 HBaseTest {

	public static Configuration configuration;
	public static Connection conn = null;
	static {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.zookeeper.property.clientPort", "2181");
//		configuration.set("hbase.zookeeper.quorum", "bfct163");
//		configuration.set("hbase.master", "bfct162:60000");
		configuration.set("hbase.zookeeper.quorum", "192.168.17.163");
		configuration.set("hbase.master", "192.168.17.162:60000");
		
		try {
			conn = ConnectionFactory.createConnection(configuration);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		 createTable("wujintao");
//		 insertData("wujintao");
		 QueryAll("wujintao");
//		 QueryByCondition1("wujintao");
//		 QueryByCondition2("wujintao");
//		 QueryByCondition3("wujintao");
//		 deleteRow("wujintao","112233bbbcccc");
//		 deleteByCondition("wujintao","abcdef");
		 
//		 dropTable("wujintao");
	}

	/**
	 * 创建表
	 * 
	 * @param tableName
	 */
	public static void createTable(String tableName) {
		System.out.println("start create table ......");
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Admin hBaseAdmin = conn.getAdmin();
			if (hBaseAdmin.tableExists(TABLE)) {// 如果存在要创建的表,那么先删除,再创建
				hBaseAdmin.disableTable(TABLE);
				hBaseAdmin.deleteTable(TABLE);
				System.out.println(tableName + " is exist,detele....");
			}
			HTableDescriptor tableDescriptor = new HTableDescriptor(TABLE);
			tableDescriptor.addFamily(new HColumnDescriptor("column1"));
			tableDescriptor.addFamily(new HColumnDescriptor("column2"));
			tableDescriptor.addFamily(new HColumnDescriptor("column3"));
			hBaseAdmin.createTable(tableDescriptor);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("end create table ......");
	}

	/**
	 * 插入数据
	 * 
	 * @param tableName
	 */
	public static void insertData(String tableName){
		System.out.println("start insert data ......");
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Table table = conn.getTable(TABLE);
			
			Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
			put.addColumn("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列addColumn
			put.addColumn("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
			put.addColumn("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
			table.put(put);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("end insert data ......");
	}

	/**
	 * 删除一张表
	 * 
	 * @param tableName
	 */
	public static void dropTable(String tableName) {
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Admin admin = conn.getAdmin();
			
			admin.disableTable(TABLE);
			admin.deleteTable(TABLE);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 根据 rowkey删除一条记录
	 * 
	 * @param tablename
	 * @param rowkey
	 */
	public static void deleteRow(String tableName, String rowkey) {
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Table table = conn.getTable(TABLE);
			List<Delete> list = new ArrayList<Delete>();
			Delete d1 = new Delete(rowkey.getBytes());
			list.add(d1);

			table.delete(list);
			System.out.println("删除行成功!");

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 组合条件删除
	 * 
	 * @param tablename
	 * @param rowkey
	 */
	public static void deleteByCondition(String tablename, String rowkey) {
		// 目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作

	}

	/**
	 * 查询所有数据
	 * 
	 * @param tableName
	 */
	public static void QueryAll(String tableName) {
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Table table = conn.getTable(TABLE);
			ResultScanner rs = table.getScanner(new Scan());
			for (Result r : rs) {
				System.out.println("获得到rowkey:" + new String(r.getRow()));
				for (Cell cell : r.listCells()) {
					System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell)));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 单条件查询,根据rowkey查询唯一一条记录
	 * 
	 * @param tableName
	 */
	public static void QueryByCondition1(String tableName) {
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Table table = conn.getTable(TABLE);
			
			Get scan = new Get("112233bbbcccc".getBytes());// 根据rowkey查询
			Result r = table.get(scan);
			System.out.println("获得到rowkey:" + new String(r.getRow()));
			for (Cell cell : r.listCells()) {
				System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell)));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 单条件按查询,查询多条记录
	 * 
	 * @param tableName
	 */
	public static void QueryByCondition2(String tableName) {
		
		try {
			TableName TABLE = TableName.valueOf(tableName);
			Table table = conn.getTable(TABLE);
			Filter filter = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); // 当列column1的值为aaa时进行查询
			Scan s = new Scan();
			s.setFilter(filter);
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs) {
				System.out.println("获得到rowkey:" + new String(r.getRow()));
				for (Cell cell : r.listCells()) {
					System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell)));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 组合条件查询
	 * 
	 * @param tableName
	 */
	public static void QueryByCondition3(String tableName) {

		try {
			TableName TABLE = TableName.valueOf(tableName);
			Table table = conn.getTable(TABLE);

			List<Filter> filters = new ArrayList<Filter>();

			Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa"));
			filters.add(filter1);

			Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("column2"), null, CompareOp.EQUAL, Bytes.toBytes("bbb"));
			filters.add(filter2);

			Filter filter3 = new SingleColumnValueFilter(Bytes.toBytes("column3"), null, CompareOp.EQUAL, Bytes.toBytes("ccc"));
			filters.add(filter3);

			FilterList filterList1 = new FilterList(filters);

			Scan scan = new Scan();
			scan.setFilter(filterList1);
			ResultScanner rs = table.getScanner(scan);
			for (Result r : rs) {
				System.out.println("获得到rowkey:" + new String(r.getRow()));
				for (Cell cell : r.listCells()) {
					System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell)));
				}
			}
			rs.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

到此,关于“Hbase的测试代码是怎样的”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI