hbase的java客户端

博客介绍了使用HBase的Java客户端进行数据操作的方法。通过创建Maven项目,在pom.xml添加HBase插件,编写Java代码,创建学生类和HBaseDemo类。最后启动HBase集群并运行Java程序进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hbase提供了java客户端去操作数据,我们通过创建一个maven项目来实现简单的数据操作。

  • pom.xml添加hbase插件

    <dependency>
    	<groupId>org.apache.hbase</groupId>
    	<artifactId>hbase-client</artifactId>
    	<version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>1.3.1</version>
    </dependency>
        <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-common</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    
  • java代码

    • 创建一个学生类
    public class Student{
    	private Integer id;
    	private String name;
    	private String age;
    	
    	create getter and setter ...
    	
    	create toString ...
    	
    }
    
    • 创建一个HBaseDemo类
    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.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 org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.guoyu.hbase.entry.Student;
    
    public class HBaseDemo {
    
    	private static Logger logger = LoggerFactory.getLogger(HBaseDemo.class);
    	private static Admin admin;
    
    	public static void main(String[] args) throws IOException {
    		//System.out.println("hhhh");
    //		boolean flag = createTable("dbtest:user_table",new String[] {"information","contact"});
    //		if(flag) {
    //			System.out.println("创建成功");
    //		}
    		
    		//插入和更新数据
    //		Student stu = new Student();
    //		stu.setId(1);
    //		stu.setName("xiaoxin");
    //		stu.setAge("18");
    //		insertData("dbtest:student",stu);
    //		System.out.println("插入成功");
    		
    		Student stu = getDataByRowKey("dbtest:student","1");
    		System.out.println(stu.toString());
    		
    		String name = getCellData("dbtest:student","1","info","name");
    		System.out.println(name);
    		logger.info("student查询成功");
    		
    		List<Student> students = getAllData("dbtest:student");
    		students.forEach(System.out::println);
    		//deleteByRowKey("dbtest:student","student-1");
    		//logger.info("删除成功");
    		
    		deleteTable("dbtest:user_table");
    		logger.info("删除表成功");
    		
    	}
    
    	// 创建HBase的连接
    	public static Connection initHBase() throws IOException {
    
    		Configuration conf = HBaseConfiguration.create();
    		conf.set("hbase.zookeeper.property.clientPort", "2181");
    		conf.set("hbase.zookeeper.quorum", "hadoop2,hadoop3,hadoop4");
    		conf.set("hbase.master", "hadoop2:60000");
    		Connection connection = ConnectionFactory.createConnection();
    		return connection;
    
    	}
    
    	// 创建表
    	public static boolean createTable(String tableName, String[] cols) throws IOException {
    		TableName table = TableName.valueOf(tableName);
    		admin = initHBase().getAdmin();
    
    		if (admin.tableExists(table)) {
    			System.out.println("表已存在!");
    			return false;
    		} else {
    			HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
    			for (String col : cols) {
    				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
    				hTableDescriptor.addFamily(hColumnDescriptor);
    			}
    			admin.createTable(hTableDescriptor);
    			return true;
    		}
    	}
    	
    	//更新或插入数据
    	public static void insertData(String tableName, Student stu) throws IOException {
    		TableName table = TableName.valueOf(tableName);
    		Put put = new Put((stu.getId()+"").getBytes());
    		put.addColumn("info".getBytes(), "name".getBytes(), stu.getName().getBytes());
    		put.addColumn("info".getBytes(), "age".getBytes(), stu.getAge().getBytes());
    		Table tab = initHBase().getTable(table);
    		tab.put(put);
    		System.out.println("插入成功");
    	}
    	
    	//根据rowKey进行查询
    	public static Student getDataByRowKey(String tableName, String rowKey) throws IOException {
    		Table table = initHBase().getTable(TableName.valueOf(tableName));
    		Get get = new Get(rowKey.getBytes());
    		Student stu = new Student();
    		stu.setId(Integer.valueOf(rowKey));
    		if(!get.isCheckExistenceOnly()) {
    			Result result = table.get(get);
    			for(Cell cell : result.rawCells()) {
    				String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
    				String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
    				if(colName.equals("name")){
                        stu.setName(value);
                    }
                    if(colName.equals("age")){
                        stu.setAge(value);
                    }
    
    			}
    		}
    		return stu;
    	}
    	
    	//查询制定单cell内容
    	public static String getCellData(String tableName,String rowKey,String family,String col) {
    		try {
    			Table table = initHBase().getTable(TableName.valueOf(tableName));
                String result = null;
                Get get = new Get(rowKey.getBytes());
                if(!get.isCheckExistenceOnly()){
                    get.addColumn(Bytes.toBytes(family),Bytes.toBytes(col));
                    Result res = table.get(get);
                    byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col));
                    return result = Bytes.toString(resByte);
                }else{
                    return result = "查询结果不存在";
                }
    
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    		return "出现异常";
    	}
    	
    	public static List<Student> getAllData(String tableName){
    		Table table = null;
    		List<Student> list = new ArrayList<Student>();
    		try {
    			table = initHBase().getTable(TableName.valueOf(tableName));
    			ResultScanner results = table.getScanner(new Scan());
    			Student stu = null;
    			for(Result result : results) {
    				String id = new String(result.getRow());
    				System.out.println("用户名:"+new String(result.getRow()));
    				stu = new Student();
    				for(Cell cell : result.rawCells()) {
    					String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
    					String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
    					String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                        stu.setId(Integer.valueOf(row));
                        if(colName.equals("name")){
                             stu.setName(value);
                        }
                        if(colName.equals("age")){
                             stu.setAge(value);
                        }
                        
    				}
    				list.add(stu);
    			}
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    		return list;
    	}
    	
    	//删除指定cell数据
    	public static void deleteByRowKey(String tableName, String rowKey) throws IOException {
    		Table table = initHBase().getTable(TableName.valueOf(tableName));
    		Delete delete = new Delete(Bytes.toBytes(rowKey));
    		table.delete(delete);
    	}
    	
    	//删除表
    	public static void deleteTable(String tableName) {
    		TableName table = TableName.valueOf(tableName);
    		try {
    			admin = initHBase().getAdmin();
    			admin.disableTable(table);
    			admin.deleteTable(table);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		
    	}
    	
    	
    }
    

    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值