HBASE-Java api

快捷键

 Alt + /   
Alt +shift+ l

pom

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

	<dependency>  
	    <groupId>jdk.tools</groupId>  
	    <artifactId>jdk.tools</artifactId>  
	    <version>1.7</version>  
	    <scope>system</scope>  
	    <systemPath>C:/Program Files/Java/jdk1.8.0_74/lib/tools.jar</systemPath>  
	</dependency>
 
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>jcl-over-slf4j</artifactId>
	    <version>1.7.5</version>
	</dependency>
	
	
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-api</artifactId>
	    <version>1.7.5</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-common</artifactId>
	    <version>2.6.0</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-hdfs</artifactId>
	    <version>2.6.0</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-client</artifactId>
	    <version>2.6.0</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
	<dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-common</artifactId>
	    <version>1.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
	<dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-client</artifactId>
	    <version>1.1.2</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
	<dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-server</artifactId>
	    <version>1.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
	<dependency>
	    <groupId>org.apache.hive</groupId>
	    <artifactId>hive-jdbc</artifactId>
	    <version>1.1.0</version>
	</dependency>

Constant

public class Constant {
	public static String ZK_ADDRESS="cdh1,cdh2,cdh3";
	public static String ZK_PORT="2181";

}

HBaseUnit


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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
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 com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

public class HBaseUnit {
	private static Log log = LogFactory.getLog(HBaseUnit.class);
	private Connection conn ;
	private static String zkString = Constant.ZK_ADDRESS;
	private static String zkPort = Constant.ZK_PORT;
	
	/**
	 * HBaseUnit初始化
	 * @param zookeeperQuorum
	 * @param clientPort
	 */
	private HBaseUnit(String zookeeperQuorum,String clientPort){
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.property.clientPort", clientPort);
		conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
		try {
			  conn = ConnectionFactory.createConnection(conf);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("create hbase connection error", e);
		}
	}
	
	/**
	 * 获取HBaseUnit对象
	 * @return
	 */
	public static HBaseUnit getHBaseUnit(){
		HBaseUnit hBaseUnit = new HBaseUnit(zkString,zkPort);
		return hBaseUnit ;
	}
	
	/**
	 * 获取表句柄
	 * @return
	 */
	public Table getTable(String tableName){
		Table table = null ;
		try {
			 table = conn.getTable(TableName.valueOf(tableName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("get hbase table error,tablename="+tableName, e);
		}
		return table ;
	}

	/**
	 * 创建hbase表
	 * @return
	 */
	public void createTable(String tableName,String family){
		
		try {
			Admin admin = conn.getAdmin();
			HTableDescriptor desc  = new HTableDescriptor(TableName.valueOf(tableName));
			desc.addFamily(new HColumnDescriptor(family));
			if (!admin.tableExists(TableName.valueOf(tableName))){
				admin.createTable(desc);
				System.out.println(tableName+"表创建成功");
				
			}else{
				System.out.println(tableName+"表已经存在");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection();
		}
	}
	
	/**
	 * 删除hbase表
	 * @return
	 */
	
	public void deleteTable(String tableName ){
		
		try {
			Admin admin = conn.getAdmin();
			TableName table =TableName.valueOf(tableName);
 
 
			if (admin.tableExists(TableName.valueOf(tableName))){
				admin.disableTable(table);
				admin.deleteTable(table);
				
			} 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
 
			closeConnection();
		}
	}
	
	/**
	 * 插入数据
	 * @return
	 */
	public void put(String tableName,String row,String columnFamily,String column,String value){
		Table  table =getTable(tableName);
		Put put  =  new Put(Bytes.toBytes(row));
		put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
		try {
 

			table.put(put);
 
 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeTable(table);
			closeConnection();
		}
	}
	/**
	 * 删除数据
	 * @return
	 */
	public void deleteRecord(String tableName,String row ){
		Table  table =getTable(tableName);
		Delete delete = new Delete(row.getBytes());
		try {
 

			table.delete(delete);
 
 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeTable(table);
			closeConnection();
		}
	}
	
	/**
	 * 查询单条记录
	 * @return
	 */
	public void getResultByRow(String tableName,String row ){
		Table  table =getTable(tableName);
		Get get = new Get(row.getBytes());
		try {
 

			Result result = table.get(get);
 
			for (Cell cell:result.listCells()){
				//column family 列簇
				System.out.println("family:"+Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
				//Qualifier 列名
				System.out.println("family:"+Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
				//value 值
				System.out.println("family:"+Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
				//timestamp				
				System.out.println("timestamp:"+cell.getTimestamp());
				System.out.println("***************");
				
			}
 
 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeTable(table);
			closeConnection();
		}
	}
	
	/**
	 * 浏览记录
	 * @return
	 */
	public void getResultByScan(String tableName ){
		Table  table =getTable(tableName);
		Scan scan = new Scan();
		try {
			ResultScanner rsa = table.getScanner(scan);

			for (Result result:rsa){
 
				for (Cell cell:result.listCells()){
					//column family 列簇
					System.out.println("family:"+Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
					//Qualifier 列名
					System.out.println("family:"+Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
					//value 值
					System.out.println("family:"+Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
					//timestamp				
					System.out.println("timestamp:"+cell.getTimestamp());
					System.out.println("***************");
					
				}
			}
 
 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeTable(table);
			closeConnection();
		}
	}
	
	/**
	 * 关闭table
	 * @return
	 */
	public void closeTable(Table table ){
		if (table!=null  ){
			try {
				table.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				log.error("close table    error,tableName="+table.getName(), e);
			}
		}
	}
	
	
	/**
	 * 关闭connect连接
	 * @return
	 */
	public void closeConnection(){
		if (conn!=null && !conn.isClosed()){
			try {
				conn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				log.error("close hbase connection error", e);
			}
		}
	}
	

}

HBaseManager


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

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Row;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
 
public class HBaseManager {
	
	public static HBaseUnit hbaseUnit = HBaseUnit.getHBaseUnit();

	public static void main(String[] args) {
 
		//创建表
//		hbaseUnit.createTable("user", "cf");
		
		//插入数据
 		insertData("user","102");
		
		
		//批量插入
//		batchInsertData("user");
		
		//根据rowkey查询数据
//		hbaseUnit.getResultByRow("user", "102");
		
		//扫描数据
//		hbaseUnit.getResultByScan("user");
		
		//删除一条记录
//		hbaseUnit.deleteRecord("user", "101");
		
		//drop 表
//		hbaseUnit.deleteTable("user");

	}
	/**
	 * 插入数据
	 * @param tablename
	 * @param row
	 */
	
	public static void insertData(String tablename,String row){
		Table table = hbaseUnit.getTable(tablename);
		Put put  =  new Put(Bytes.toBytes(row));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("xiaozhang"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("age"), Bytes.toBytes("28"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("birthday"), Bytes.toBytes("1979-09-18"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("company"), Bytes.toBytes("alibaba"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("contry"), Bytes.toBytes("China"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("province"), Bytes.toBytes("shandong"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("city"), Bytes.toBytes("qingdao"));
		try {
			table.put(put);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			hbaseUnit.closeTable(table);
			hbaseUnit.closeConnection();
		}
		
	}
	
	/**
	 * 批量插入
	 * @param tablename
	 */
	public static void batchInsertData(String tablename ){
		Table table = hbaseUnit.getTable(tablename);
		
		ArrayList<Row> batch = new ArrayList<Row>();
		
		Put put2  =  new Put(Bytes.toBytes("102")); 
		put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
		batch.add(put2);
		
		Put put3  =  new Put(Bytes.toBytes("103")); 
		put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));	
		batch.add(put3);
		
		Put put4  =  new Put(Bytes.toBytes("104")); 
		put4.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("wangwu"));	
		batch.add(put4);
 
		Object[] results = new Object[batch.size()];
		try {
			table.batch(batch,results);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			hbaseUnit.closeTable(table);
			hbaseUnit.closeConnection();
		}
		
		for (int i=0;i<results.length;i++){
			System.out.println("Result["+i+"]:"+results[i]);
		}
		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: HBase-Java API 是一种用于管理 HBase 表的编程接口。使用 HBase-Java API,开发人员可以编写 Java 代码来创建、删除、修改和查询 HBase 表。HBase-Java API 提供了一系列类和方法,可以让开发人员轻松地与 HBase 表进行交互。例如,可以使用 HBaseAdmin 类来管理 HBase 表,使用 HTable 类来访问 HBase 表中的数据,使用 Put 类来插入数据,使用 Get 类来获取数据,等等。总之,HBase-Java APIHBase 的重要组成部分,它为开发人员提供了强大的工具来管理和操作 HBase 表。 ### 回答2: HBase是一个分布式的列式存储数据库,在很多大数据应用中得到广泛的使用。它采用Hadoop作为其底层基础框架,同时提供了Java API供开发人员使用。HBaseJava API为开发人员提供了一个管理表的接口,使得开发人员可以对HBase数据库中的表进行创建、读取、修改和删除等基本操作。 首先,我们需要用Java API创建一个HBase数据库中的表。使用HBaseJava API创建表的流程如下: 1. 首先需要获取HBase Configuration对象,并设置HBase连接参数以连接HBase数据库。 2. 接下来,需要获取HBase Admin对象,以便在操作HBase数据库表之前检查表是否存在,如果表不存在,需要创建该表。 3. 通过HBaseJava API创建表时,需要指定表的表名、列族的名称以及版本数等属性。 4. 创建表时需要考虑表的region的分配问题,可以对表的region进行手动或自动分片,以此来控制HBase的负载均衡。 创建了HBase数据库中的表之后,我们可以使用Java API对表进行读写操作。在进行读写操作时,需要使用HBaseJava API提供的Get的方法来获取表中的数据、Scan方法来扫描整个表、以及Put方法来向表中插入数据。此外,在进行表操作时还需要设置一些常见的数据操作参数,例如版本数、时间戳等。 在使用HBaseJava API时,还需要注意一些性能优化的问题,例如何时启用缓存、何时触发分区策略以及如何优化HBase表的大小等。这些优化措施能够在HBase的性能以及数据读写时的延迟方面提供很好的支持和帮助。 总的来说,HBaseJava API提供的表管理接口为开发人员提供了非常方便和快捷的方式来操作HBase数据库中的表。通过使用这些API,开发人员可以创建、读取、修改和删除表的数据,并且能够充分应用HBase的分布式特性来优化数据管理和性能提升。 ### 回答3: HBase是一个开源、分布式、非关系型数据库,它可以存储和处理大规模结构化、半结构化和非结构化数据。HBase Java APIHBase的官方API,它提供了对HBase表的管理和操作功能,让开发人员可以通过Java代码轻松地连接到HBase数据库。 在使用HBase Java API管理表时,首先需要创建一个HBaseConfiguration对象,它包含了连接HBase数据库所需的配置信息,如Zookeeper地址、HBase根目录等。然后,可以使用HBaseAdmin类创建、删除、修改表,以及列族等操作。例如,创建一个表可以通过以下代码实现: ``` HBaseAdmin admin = new HBaseAdmin(HBaseConfiguration.create()); HTableDescriptor tableDescriptor = new HTableDescriptor("table_name"); HColumnDescriptor columnDescriptor = new HColumnDescriptor("column_family"); tableDescriptor.addFamily(columnDescriptor); admin.createTable(tableDescriptor); ``` 创建表时,需要先通过HTableDescriptor类定义表名称,然后通过HColumnDescriptor类定义列族名称。可以通过addFamily()方法将列族添加到表描述中,最后通过HBaseAdmin的createTable()方法创建表。 除了创建表之外,HBase Java API还提供了许多其他的操作,如获取表信息、获取所有表的列表、删除表等。同时,HBase Java API还提供了对数据的CRUD操作,包括put、get、scan、delete等方法,让开发人员可以方便地进行数据操作。 总之,HBase Java API是一个非常强大的工具,它可以使开发人员轻松地管理HBase数据库,并实现数据的高效存储和处理。但是,在使用HBase Java API时,需要了解HBase的基本知识和API的用法,才能更好地发挥其功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值