转载

Java 操作Hbase 完整例子

发表于 2020-04-07 18:28:00 by月小升

开发工具:Eclipse,

三步

1.新建一个项目

2.把hbase安装下的lib的文件都拷贝进来

3.把lib目录下jar文件都引入

4.lib下的client-facing-thirdparty 目录下的jar也都引入

看图

Java 操作Hbase 完整例子
package com.yue;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
 
import java.io.IOException;
public class Test {
	public static Configuration configuration; // 管理Hbase的配置信息
    public static Connection connection; // 管理Hbase连接
    public static Admin admin; // 管理Hbase数据库的信息
 
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("shit");
		init();
        String colF[] ={"score"};
        createTable("stu41",colF);
        insertData("stu41","zhangsan","score","English","69");
        insertData("stu41","lisi","score","English","69");
        getData("stu41","zhangsan","score","English");
        close();
	}
 
	public static void init(){
		 configuration = HBaseConfiguration.create();
		 configuration.set("hbase.zookeeper.quorum", "localhost");
		try{
		 connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
		}catch(IOException e){
			e.printStackTrace();
		}
 
	 }
 
	 public static void createTable(String myTableName,String[] colFamily) throws IOException{
	        TableName tableName = TableName.valueOf(myTableName);
	        if(admin.tableExists(tableName)){
	            System.out.println("Table exists");
	        }else {
	            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
	            for(String str:colFamily){
	                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
	                hTableDescriptor.addFamily(hColumnDescriptor);
	            }
	            admin.createTable(hTableDescriptor);
	        }
	    }
 
	 // 添加单元格数据
	    /*
	    * @param tableName 表名
	    * @param rowKey 行键
	    * @param colFamily 列族
	    * @param col 列限定符
	    * @param val 数据
	    * @thorws Exception
	    * */
	    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException{
	        Table table = connection.getTable(TableName.valueOf(tableName));
	        Put put = new Put(rowKey.getBytes());
	        put.addColumn(colFamily.getBytes(),col.getBytes(),val.getBytes());
	        table.put(put);
	        table.close();
	    }
 
	  //浏览数据
	    /*
	    * @param tableName 表名
	    * @param rowKey 行
	    * @param colFamily 列族
	    * @param col 列限定符
	    * @throw IOException
	    * */
	    public static void getData(String tableName,String rowKey,String colFamily,String col) throws IOException{
	        Table table = connection.getTable(TableName.valueOf(tableName));
	        Get get = new Get(rowKey.getBytes());
	        get.addColumn(colFamily.getBytes(),col.getBytes());
	        Result result =table.get(get);
	        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
	        table.close();
	    }
 
 
	    // 操作数据库之后,关闭连接
	    public static void close(){
	        try{
	            if(admin!=null){
	                admin.close(); // 退出用户
	            }
	            if(null != connection){
	                connection.close(); // 关闭连接
	            }
	        }catch (IOException e){
	            e.printStackTrace();
	        }
	    }
 
 
	  //删除表
	    public static void deleteTable(String tableName){
 
	        try {
	            TableName tablename = TableName.valueOf(tableName);
	            admin = connection.getAdmin();
	            admin.disableTable(tablename);
	            admin.deleteTable(tablename);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
 
 
 
 
	 //End
 
 
}

单机模式

configuration = HBaseConfiguration.create();
		 configuration.set("hbase.zookeeper.quorum", "localhost");

集群模式

configuration.set("hbase.rootdir","hdfs://master:9000/hbase");//主节点
        configuration.set("hbase.zookeeper.quorum","master,slave1,slave2"); // 设置zookeeper节点
        configuration.set("hbase.zookeeper.property.clientPort","2181"); // 设置客户端节点

java开发Hbase简直不费劲,当时鼓捣那个php操作Hbase搞了好几天。

参考资料:

https://blog.csdn.net/a973685825/article/details/81303636

  • Hbase 使用场景及案例
  • php 操作Hbase函数手册
  • HBase写入性能测试VS单机文件
  • Hbase数据例子,及shell操作例子
  • Hbase 命令行 shell基础
  • php调用Hbase 通过thrift
  • 安装thrift
  • 单机Hbase安装及基本命令
首发地址:–

无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢

原文  https://java-er.com/blog/java-hbase-example/
正文到此结束
Loading...