Hadoop_Java操作Hbase
发布日期:2021-05-07 00:19:17 浏览次数:21 分类:技术文章

本文共 5774 字,大约阅读时间需要 19 分钟。

Hadoop_Java操作Hbase

package com.lius.hadoop.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.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.Durability;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 org.apache.log4j.Logger;/** * java操作Hbase * @author Administrator * */public class operationHbase {	public static Logger log = Logger.getLogger(operationHbase.class);	public  static Configuration conf = null;	private static Connection connection = null;	//初始化配置	static {		conf = HBaseConfiguration.create();		conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");		conf.set("hbase.zookeeper.property.clientPort", "2181");	}		public static void main(String[] args) throws IOException {				init();		//创建表//		createTable(TableName.valueOf("test"));		//插入数据//		insertData("marry","test");		//删除一张表		dropTable("t1");		//根据rowkey删除一条记录//		deleteRow("xiaoming","test");		//查询所有数据//		QueryAll("test");		//单条件查询,根据rowkey查询唯一一条记录//		QueryByCondition1("marry","test");				connection.close();	}	/**	 * 删除一张表	 * @param string	 * @throws IOException 	 */	private static void dropTable(String tableName) throws IOException {		// TODO Auto-generated method stub		Admin admin = connection.getAdmin();		TableName tabName = TableName.valueOf(tableName);		admin.disableTable(tabName);		admin.deleteTable(tabName);	}	/**	 * 根据rowkey删除一条记录	 * @param string	 * @param string2	 * @throws IOException 	 */	private static void deleteRow(String rowkey, String tableName) throws IOException {		// TODO Auto-generated method stub		Table table = connection.getTable(TableName.valueOf(tableName));		List
lists = new ArrayList(); Delete delete = new Delete(rowkey.getBytes()); lists.add(delete); table.delete(lists); table.close(); } /** * 单条件查询,根据rowkey查询唯一一条记录 * @param string * @param string2 * @throws IOException */ private static void QueryByCondition1(String rowkey, String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); Get scan = new Get(rowkey.getBytes()); Result r = table.get(scan); List
cells = r.listCells(); for(Cell cell:cells) { System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)))); } table.close(); } /** * 查询所有数据 * @param string * @throws IOException */ private static void QueryAll(String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); ResultScanner rs = table.getScanner(new Scan()); for(Result r:rs) { System.out.println("获得的rowkey:"+new String(r.getRow())); List
cells = r.listCells(); for(Cell cell:cells) { System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s ", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)))); } } table.close(); } private static void init() throws IOException { // TODO Auto-generated method stub connection = ConnectionFactory.createConnection(conf); } /** * 插入数据 * @param rowkey * @param tableName * @throws IOException */ private static void insertData(String rowkey, String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); //create Table Put put = new Put(rowkey.getBytes()); //create Put put.addColumn(Bytes.toBytes("column2"),"screen_width".getBytes(),"1080".getBytes());//addColumn(Family,key,Value) put.addColumn("column2".getBytes(), "screen_height".getBytes(), "1920".getBytes()); put.addColumn("column2".getBytes(), "url".getBytes(), "www.baidu.com".getBytes()); put.addColumn("column2".getBytes(), "event_data".getBytes(), "12|16|13|17|12|16".getBytes()); put.setDurability(Durability.SYNC_WAL); table.put(put); table.close(); } /** * 创建表 * @param string * @throws IOException */ private static void createTable(TableName tableName) throws IOException { // TODO Auto-generated method stub log.info("start create table..."); Admin hbaseAdmin = connection.getAdmin(); if(hbaseAdmin.tableExists(tableName)) { hbaseAdmin.disableTable(tableName); hbaseAdmin.deleteTable(tableName); log.info(String.format("table %s is exists,delete...", tableName.toString())); }// TableDescriptor table = TableDescriptorBuilder.newBuilder(tableName).build();// table.getColumnFamilies(); HTableDescriptor hTable = new HTableDescriptor(tableName); hTable.addFamily(new HColumnDescriptor("column1")); hTable.addFamily(new HColumnDescriptor("column2")); hTable.addFamily(new HColumnDescriptor("column3")); hbaseAdmin.createTable(hTable); log.info(String.format("end create table %s", tableName.toString())); }}

 

上一篇:Hadoop_Scala操作Hbase
下一篇:Hadoop_MapReduce取分组后的最大值

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年03月28日 21时45分23秒