• hbase基本操作


    public class Demo {
    	private Configuration conf;
    	private Connection conn;
    	
    	@Before
    	public void prepare() throws Exception {
    		conf = HBaseConfiguration.create();
    		conf.set("hbase.zookeeper.quorum", "m6,m7,m8");
    		conn = ConnectionFactory.createConnection(conf);
    	}
    
    	/**
    	 *  创建表
    	 */
    	@Test
    	public void createTable() throws Exception {
    		Admin admin  = conn.getAdmin();
    		TableName tableName = TableName.valueOf("t_person");
    		HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);				 
    		// 添加列簇
    		HColumnDescriptor baseInfo = new HColumnDescriptor("base_info");
    		hTableDescriptor.addFamily(baseInfo);		
    		admin.createTable(hTableDescriptor);
    		admin.close();
    		conn.close();
    	}
    	
    	/**
    	 * 删除表
    	 * @throws Exception 
    	 */
    	@Test
    	public void dropTable() throws Exception {
    		Admin admin  = conn.getAdmin();
    		admin.disableTable(TableName.valueOf("t_person"));
    		admin.deleteTable(TableName.valueOf("t_person"));
    		admin.close();
    	}
    	
    	
    	/**
    	 * 插入数据
    	 * @throws Exception 
    	 */
    	@Test
    	public void put() throws Exception {
    		TableName tableName = TableName.valueOf("t_person");
    		// HTablePool pool = new HTablePool(conf, 10);
    		// HTable table = (HTable) pool.getTable("t_person");
    		Table table = conn.getTable(tableName);
    		// rowkey 行健
    		Put put = new Put(Bytes.toBytes("p_0001"));
    		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
    		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes(18));
    		table.put(put);
    		table.close();
    	}
    	
    	/**
    	 * 获取数据
    	 * @throws IOException 
    	 */
    	@Test
    	public void get() throws IOException {
    		TableName tableName = TableName.valueOf("t_person");
    		Table table = conn.getTable(tableName);
    		Get get = new Get(Bytes.toBytes("p_0001"));
    		Result result = table.get(get);
    		for (Cell cell : result.listCells()) {
    			
    			System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
    			System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
    			System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
    		}
    	}
    	
    	@Test
    	public void scan() throws IOException {
    		TableName tableName = TableName.valueOf("t_person");
    		Table table = conn.getTable(tableName);
    		Scan scan = new Scan();
    		// 根据Qualifier的开头字符进行过滤
    		ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("z")); 
    		scan.setFilter(filter);
    		ResultScanner scanner = table.getScanner(scan);
    		for (Result result : scanner) {
    			for (Cell cell : result.listCells()) {
    				System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
    				System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
    			}			
    		}
    	}
    }
    

      

  • 相关阅读:
    【RAC】 RAC For W2K8R2 安装--RDBMS软件的安装(五)
    【RAC】 RAC For W2K8R2 安装--grid的安装(四)
    【RAC】 RAC For W2K8R2 安装--共享磁盘的配置(三)
    【RAC】 RAC For W2K8R2 安装--操作系统环境配置 (二)
    【RAC】 RAC For W2K8R2 安装--总体规划 (一)
    【OGG】 RAC环境下管理OGG的高可用 (五)
    【OGG】RAC环境下配置OGG单向同步 (四)
    【OGG】OGG简单配置双向复制(三)
    【OGG】OGG的单向复制配置-支持DDL(二)
    【OGG】OGG的单向DML复制配置(一)
  • 原文地址:https://www.cnblogs.com/heml/p/6047916.html
Copyright © 2020-2023  润新知