java操作hbase的增删改查

首先需要将hbase安装目录下lib文件夹中的jar文件全部加入到项目类路径下,另外还需要将hadoop相关jar包也加入。

这里需要用到的主要API介绍一下。

Configuration:HBase参数配置对象。

Connection:HBase连接对象,通过ConnectionFactory工厂方法创建,需要传入一个参数配置对象。

Admin:通过Admin实现创建表,删除表操作。

TableName:取代字符串形式的tableName。

Table:使用Table可以新增数据put(),查询数据getScanner()。

下面通过代码来演示如何做常规的操作:

1、初始化配置,构建一个HBase连接器对象。

private static Connection connection;

static{
	Configuration config = HBaseConfiguration.create();
	config.set("hbase.rootdir", "hdfs://server:9000/hbase");
	config.set("hbase.zookeeper.quorum", "server:2181");
	try {
		connection = ConnectionFactory.createConnection(config);
	} catch (IOException e) {
		e.printStackTrace();
	}	
}

2、建表。

public static boolean createTable(String tableName,String columnFamily){
	boolean success = false;
	Admin admin = null;
	TableName tn = TableName.valueOf(tableName);
	try {
		admin = connection.getAdmin();
		if(!admin.tableExists(tn)){
			HTableDescriptor desc = new HTableDescriptor(tn);
			desc.addFamily(new HColumnDescriptor(columnFamily.getBytes()));
			admin.createTable(desc);
			System.out.println("table ["+tableName+"] create ok.");
			success = true;			
		}else{
			System.out.println("table ["+tableName+"] already exists.");
			success = false ;
		}
	} catch (Exception e) {
		success = false ;
	}
	return success;
}

3、插入数据。

public static boolean putData(String tableName,String rowKey,String columnFamily,String qualifier,Object value){
	boolean success = false;
	Admin admin = null;
	TableName table = TableName.valueOf(tableName);
	try {
		admin = connection.getAdmin();
		if(admin.tableExists(table)){
			Table t = connection.getTable(table);
			Put put = new Put(rowKey.getBytes());
			put.addColumn(columnFamily.getBytes(), qualifier.getBytes(), value.toString().getBytes());
			t.put(put);
			success = true;
		}else{
			System.out.println("table ["+tableName+"] does not exist.");
			success = false;
		}
	} catch (Exception e) {
		success = false;
	}
	return success;
}

4、查看数据。 

@SuppressWarnings("deprecation")
public static void getValueByTable(String tableName){
	TableName tn = TableName.valueOf(tableName.getBytes());
	try{
		Table table = connection.getTable(tn);
		ResultScanner rs = table.getScanner(new Scan());
		for(Result r:rs){
			System.out.println("row:"+new String(r.getRow()));
			for(KeyValue kv:r.raw()){
				System.out.println("column family:"+new String(kv.getFamilyArray())
				+" qualifier "+new String(kv.getQualifierArray())
				+" value "+new String(kv.getValueArray()));
			}
		}
	}catch(Exception e){
		e.printStackTrace();
	}
}

5、删除表。

public static boolean dropTable(String tableName){
	boolean success = false;
	Admin admin = null;
	try {
		admin = connection.getAdmin();
		TableName table = TableName.valueOf(tableName);
		if(admin.tableExists(table)){
			admin.disableTable(table);
			admin.deleteTable(table);
			success = true;
		}
	} catch (Exception e) {
		success = false;
	}finally{
		IOUtils.closeQuietly(admin);
	}
	return success;
}

6、以上几段代码展示了,如何初始化hbase连接,创建表,添加数据,查看数据,删除表的操作,下面通过运行,查看效果。这里代码在windows的eclipse ide编码的,为了演示,我们通过ant构建插件sshexec来将代码直接提交到hbase服务器上运行。这里给出完整的构建脚本build.xml。

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="hbase" default="sshexec">
    <property name="main.class" value="com.xxx.hbase.test.HBaseTest"/>
    <property name="src.dir" value="${basedir}/src" />
    <property name="java.lib.dir" value="${basedir}/lib"/>
    <property name="localpath.dir" value="${basedir}"/>
    <property name="dest.dir" value="${basedir}/dest"/>
    <property name="classes.dir" value="${basedir}/classes"/>
    <property name="third.lib.dir" value="${basedir}/lib"/>
    <property name="remote.host" value="10.119.9.149"/>
    <property name="remote.username" value="root"/>
    <property name="remote.password" value="root"/>
    <property name="remote.home" value="~"/>
    
    <path id="compile.classpath">
        <fileset dir="${java.lib.dir}">          
            <include name="tools.jar"/>
        </fileset>
        <fileset dir="${third.lib.dir}">          
            <include name="*.jar"/>
        </fileset>
    </path>
    
    <path id="run.classpath">
        <path refid="compile.classpath"/>
        <pathelement location="${classes.dir}" />
    </path>
    
    <target name="clean">
        <delete dir="${classes.dir}"/>
        <delete dir="${dest.dir}"/>
        <echo level="info">清理完毕</echo>
    </target>
    <target name="init" depends="clean">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${dest.dir}"/>
    </target>
    
    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.8" target="1.8"
        includeAntRuntime="false" debug="false" verbose="false" encoding="UTF-8">
            <classpath refid="compile.classpath"/>
        </javac>   
    </target>
    
    <target name="build" depends="compile">
        <jar jarfile="${dest.dir}/hbasetest.jar">
            <fileset dir="${classes.dir}" includes="**/*.*"/>
        </jar>
    </target>
    
    <target name="ssh" depends="build">
        <scp  file="${dest.dir}/hbasetest.jar" 
         todir="${remote.username}@${remote.host}:${remote.home}" 
         password="${remote.password}" trust="true"/>    
    </target>
    
    <target name="sshexec" depends="ssh">
        <sshexec host="${remote.host}" username="${remote.username}" 
           password="${remote.password}" trust="true" 
           command="hadoop jar ${remote.home}/hbasetest.jar ${main.class}"/>     
    </target>
</project>

7、先运行创建表的任务。

运行截图:

生成表:

 

8、再运行添加数据方法:

 

运行构建成功截图:

 

验证数据是否添加:

9、运行查看数据:

构建结果:

10、删除表,调用删除表api之前,需要先调用禁用表api方法。

构建截图:

验证是否删除:

最后,附上完整的java代码部分:

package com.xxx.hbase.test;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
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.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;

public class HBaseTest {
	
	private static Connection connection;

	static{
		Configuration config = HBaseConfiguration.create();
		config.set("hbase.rootdir", "hdfs://server:9000/hbase");
		config.set("hbase.zookeeper.quorum", "server:2181");
		try {
			connection = ConnectionFactory.createConnection(config);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public static boolean createTable(String tableName,String columnFamily){
		boolean success = false;
		Admin admin = null;
		TableName tn = TableName.valueOf(tableName);
		try {
			admin = connection.getAdmin();
			if(!admin.tableExists(tn)){
				HTableDescriptor desc = new HTableDescriptor(tn);
				desc.addFamily(new HColumnDescriptor(columnFamily.getBytes()));
				admin.createTable(desc);
				System.out.println("table ["+tableName+"] create ok.");
				success = true;			
			}else{
				System.out.println("table ["+tableName+"] already exists.");
				success = false ;
			}
		} catch (Exception e) {
			success = false ;
		}
		return success;
	}
	
	public static boolean dropTable(String tableName){
		boolean success = false;
		Admin admin = null;
		try {
			admin = connection.getAdmin();
			TableName table = TableName.valueOf(tableName);
			if(admin.tableExists(table)){
				admin.disableTable(table);
				admin.deleteTable(table);
				success = true;
			}
		} catch (Exception e) {
			success = false;
		}finally{
			IOUtils.closeQuietly(admin);
		}
		return success;
	}
	
	public static boolean putData(String tableName,String rowKey,
        String columnFamily,String qualifier,Object value){
		boolean success = false;
		Admin admin = null;
		TableName table = TableName.valueOf(tableName);
		try {
			admin = connection.getAdmin();
			if(admin.tableExists(table)){
				Table t = connection.getTable(table);
				Put put = new Put(rowKey.getBytes());
				put.addColumn(columnFamily.getBytes(), 
                 qualifier.getBytes(), value.toString().getBytes());
				t.put(put);
				success = true;
			}else{
				System.out.println("table ["+tableName+"] does not exist.");
				success = false;
			}
		} catch (Exception e) {
			success = false;
		}
		return success;
	}
	
	@SuppressWarnings("deprecation")
	public static void getValueByTable(String tableName){
		TableName tn = TableName.valueOf(tableName.getBytes());
		try{
			Table table = connection.getTable(tn);
			ResultScanner rs = table.getScanner(new Scan());
			for(Result r:rs){
				System.out.println("row:"+new String(r.getRow()));
				for(KeyValue kv:r.raw()){
					System.out.println("column family:"
                    +new String(kv.getFamilyArray())
					+" qualifier "+new String(kv.getQualifierArray())
					+" value "+new String(kv.getValueArray()));
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception{
		//createTable("stu", "info");
		//putData("stu", "1", "info", "id",1);
		//putData("stu", "1", "info", "name","xxx");
		//getValueByTable("stu");
		dropTable("stu");
	}
	
}

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java可以通过HBaseJava API来实现对HBase数据的增删改查操作。以下是示例代码: 1. 首先需要创建一个HBase配置对象`Configuration`并设置相关参数: ```java Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "server1,server2,server3"); conf.set("hbase.zookeeper.property.clientPort", "2181"); ``` 2. 获取HBase的客户端对象`Connection`: ```java Connection connection = ConnectionFactory.createConnection(conf); ``` 3. 获取操作某个表的HBase表对象`Table`: ```java TableName tableName = TableName.valueOf("table_name"); Table table = connection.getTable(tableName); ``` 4. 插入数据: ```java Put put = new Put(Bytes.toBytes("row_key")); put.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier"), Bytes.toBytes("value")); table.put(put); ``` 5. 查询数据: ```java Get get = new Get(Bytes.toBytes("row_key")); Result result = table.get(get); byte[] value = result.getValue(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier")); String valueStr = Bytes.toString(value); ``` 6. 删除数据: ```java Delete delete = new Delete(Bytes.toBytes("row_key")); delete.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier")); table.delete(delete); ``` 7. 最后需要关闭`Table`和`Connection`对象: ```java table.close(); connection.close(); ``` 以上是Java调用HBase进行数据增删改查的简单示例代码,需要注意的是,实际应用中还需要进行一些异常处理等操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值