设为首页 加入收藏

TOP

Java 访问Hbase数据库
2018-12-07 02:01:09 】 浏览:10
Tags:Java 访问 Hbase 数据库
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wonderful_life_mrchi/article/details/77556118

须知

Hbase数据最终是在hdfs上的,具体来说应该是在hdfs上一个叫做/hbase的目录下。具体结构如下:


所以java访问Hbase其实就是访问hdfs,所以环境搭建跟hadoop开发环境使用没什么不同。

实战部分

1.导入需要的jar包,此处在java项目引入Hbase解压后lib目录下所有jar包即可。里面提供了访问Hbase的api接口,也有与hadoop集成的客户端jar包。

2.将hbase-site.xml拷贝到classpath根目录下,这样,hbase在创建configuration对象会自动加载,hbase源码此部分:

 public static Configuration addHbaseResources(Configuration conf)
    {
        conf.addResource("hbase-default.xml");
        conf.addResource("hbase-site.xml");
        checkDefaultsVersion(conf);
        checkForClusterFreeMemoryLimit(conf);
        return conf;
    }

hbase-site.xml,注意里面主机名要跟core-site.xml一致,另外还需要在hosts文件加一行主机名和hbase服务器ip地址的映射。(windows下面的hosts文件在:C:\Windows\System32\drivers\etc)

具体文件hbase-site.xml参照内容如下:

<configuration>
<property>
  <name>hbase.rootdir</name>
    <value>hdfs://chidianwei-VirtualBox:9000/hbase</value>
    </property>
    <property>
      <name>hbase.cluster.distributed</name>
        <value>true</value>
        </property>
        <property>
          <name>hbase.zookeeper.quorum</name>
            <value>chidianwei-VirtualBox</value>
            </property>
            <property>
              <name>dfs.replication</name>
                <value>1</value>
                </property>
</configuration>

3.下面我们就可以写一个初始化表测试类,创建了两张表,word和stat。(其中的HbaseUtil类代码见最后一步)代码如下:

public class InitData {
    
    public static void main(String[] args) throws IOException {
        //创建一个word表,只有一个列族content
        HBaseUtil.createTable("word","content");
        
        //获取word表
        HTable htable = HBaseUtil.getHTable("word");
        htable.setAutoFlush(false);
        
        //创建测试数据
       List<Put> puts = new ArrayList<Put>();
       
       Put put1 = HBaseUtil.getPut("1","content",null,"The Apache Hadoop software library is a framework");
       Put put2 = HBaseUtil.getPut("2","content",null,"The common utilities that support the other Hadoop modules");
       Put put3 = HBaseUtil.getPut("3","content",null,"Hadoop by reading the documentation");
       Put put4 = HBaseUtil.getPut("4","content",null,"Hadoop from the release page");
       Put put5 = HBaseUtil.getPut("5","content",null,"Hadoop on the mailing list");
       
       puts.add(put1);
       puts.add(put2);
       puts.add(put3);
       puts.add(put4);
       puts.add(put5);
       
       //提交测试数据
      htable.put(puts);
      htable.flushCommits();
      htable.close();
        //创建stat表,只有一个列祖result
      HBaseUtil.createTable("stat","result");
    }
}
 

4.hbase工具类,里面涉及到java对hbase数据库创建表,删除表,常见的crud操作等等。具体参照代码:

package com.oracle.hbase.weifenbushi;

import java.io.IOException;
import java.util.Arrays;

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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseUtil {
    
    /**
     * 初始化HBase的配置文件
     * @return
     */
    public static Configuration getConfiguration(){
        Configuration conf = HBaseConfiguration.create();
        //和hbase-site.xml中配置的一致
        conf.set("hbase.zooker.quorum", "192.168.10.127");
//        conf.set("hbase.zooker.quorum", "oraclehadoop1");
        return conf;
    }
    
    /**
     * 实例化HBaseAdmin,HBaseAdmin用于对表的元素据进行操作
     * @return
     * @throws MasterNotRunningException
     * @throws ZooKeeperConnectionException
     */
    public static HBaseAdmin getHBaseAdmin() throws MasterNotRunningException, ZooKeeperConnectionException{
    	HBaseAdmin a=null;
    	try {
			a= new HBaseAdmin(getConfiguration());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return a;
    }
    
    /**
     * 创建表
     * @param tableName            表名
     * @param columnFamilies    列族
     * @throws IOException
     */
    public static void createTable(String tableName,String...columnFamilies) throws IOException {
        HTableDescriptor htd = new HTableDescriptor(tableName.getBytes());//
        for(String fc : columnFamilies) {
            htd.addFamily(new HColumnDescriptor(fc));
        }
        getHBaseAdmin().createTable(htd);
    }
    
    /**
     * 获取HTableDescriptor
     * @param tableName
     * @return
     * @throws IOException
     */
    public static HTableDescriptor getHTableDescriptor(byte[] tableName) throws IOException{
        return getHBaseAdmin().getTableDescriptor(tableName); 
    }
    
    /**
     * 获取表
     * @param tableName 表名
     * @return
     * @throws IOException
     */
    public static HTable getHTable(String tableName) throws IOException{
        return new HTable(getConfiguration(),tableName);
    }
    
    /**
     * 获取Put,Put是插入一行数据的封装格式
     * @param tableName
     * @param row
     * @param columnFamily
     * @param qualifier
     * @param value
     * @return
     * @throws IOException
     */
    public static Put getPut(String row,String columnFamily,String qualifier,String value) throws IOException{
        Put put = new Put(row.getBytes());
        if(qualifier==null||"".equals(qualifier)) {
            put.add(columnFamily.getBytes(), null, value.getBytes());
        }else {
            put.add(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes());
        }
        return put;
    }
    
    /**
     * 查询某一行的数据
     * @param tableName    表名
     * @param row        行键
     * @return
     * @throws IOException
     */
    public static Result getResult(String tableName,String row) throws IOException {
        Get get = new Get(row.getBytes());
        HTable htable  = getHTable(tableName);
        Result result = htable.get(get);
        htable.close();
        return result;
        
    }
    
    /**
     * 条件查询
     * @param tableName        表名
     * @param columnFamily    列族
     * @param queryCondition    查询条件值
     * @param begin                查询的起始行
     * @param end                查询的终止行
     * @return
     * @throws IOException
     */
    public static ResultScanner getResultScanner(String tableName,String columnFamily,String queryCondition,String begin,String end) throws IOException{
        
        Scan scan = new Scan();
        //设置起始行
        scan.setStartRow(Bytes.toBytes(begin));
        //设置终止行
        scan.setStopRow(Bytes.toBytes(end));
        
        //指定要查询的列族
        scan.addColumn(Bytes.toBytes(columnFamily),null);
        //查询列族中值等于queryCondition的记录
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),null,CompareOp.EQUAL,Bytes.toBytes(queryCondition));
        //Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(columnFamily),null,CompareOp.EQUAL,Bytes.toBytes("chuliuxiang"));
        
        FilterList filterList = new FilterList(Operator.MUST_PASS_ONE,Arrays.asList(filter1));
        
        scan.setFilter(filterList);
        HTable htable  = getHTable(tableName);
        
        ResultScanner rs = htable.getScanner(scan);
        htable.close();
        return rs;
    }
    
    public static void main(String[] args) {
		try {
			Result result = getResult("word", "1");
			System.out.println(new String(result.list().get(0).getValue()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
}

总结:

以上就是java对hbase伪分布式的访问,对集群的访问也是一样,api是不会变的,变化的是配置文件,对应的quorum是zookeeper集群,配置变为: conf.set("hbase.zookeeper.quorum", "weekend05:2181,weekend06:2181,weekend07:2181");

ok,希望对大家会有帮助。



编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇笔记:HBase 协处理器总结 下一篇遍历HBase表中的数据(把hbase中..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }