美文网首页
Java操作Hbase的基本操作

Java操作Hbase的基本操作

作者: analanxingde | 来源:发表于2019-04-11 20:39 被阅读0次

最基本的入门展示

package hbasetest;
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
public class HelloHBase {
    public static void main(String[] args) throws IOException, URISyntaxException {      
                //获取配置文件
        Configuration conf= HBaseConfiguration.create();
        conf.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
        conf.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
                //创建连接
        try(Connection connection=ConnectionFactory.createConnection(conf);
                Admin admin=connection.getAdmin()){
           //定义表名
            TableName tableName=TableName.valueOf("mytable6");
           //定义表
            HTableDescriptor table=new HTableDescriptor(tableName);
           //定义列族
            HColumnDescriptor mycf=new HColumnDescriptor("mycf");
           //执行创建表操作
            table.addFamily(new HColumnDescriptor(mycf));
            admin.createTable(table);
            admin.close();
            connection.close();
        
        }
}

基本的增删改查操作集合

package hbasetest;
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
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.io.compress.Compression.Algorithm;
//import org.apache.hadoop.hbase.client.Delete;
//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.Filter;
//import org.apache.hadoop.hbase.util.Bytes;
public class HelloHBase {
    /**
     * 检查mytable表是否存在,若存在则需要先删除
     * @param admin
     * @param table
     * @throws IOException
     */
    public static void createOrOvewrite(Admin admin, HTableDescriptor table)throws IOException{
        if(admin.tableExists(table.getTableName())) {
            admin.disableTable(table.getTableName());
            admin.deleteTable(table.getTableName());
        }
        admin.createTable(table);
    }
    
    /**
     * 建立mytable表
     * @param config
     * @throws IOException
     */
    public static void createSchemaTables (Configuration config) throws IOException{
        try(Connection connection=ConnectionFactory.createConnection(config);
                Admin admin=connection.getAdmin()){
            HTableDescriptor table=new HTableDescriptor(TableName.valueOf("mytable"));
            table.addFamily(new HColumnDescriptor("mycf").setCompressionType(Algorithm.NONE));
            System.out.print("Creating table.");
            createOrOvewrite(admin,table);
            System.out.println("Done.");
        }
    }
    
    public static void modifySchema(Configuration config) throws IOException{
        try(Connection connection=ConnectionFactory.createConnection(config);
                Admin admin=connection.getAdmin()){
            TableName tableName=TableName.valueOf("mytable");
            if (!admin.tableExists(tableName)) {
                System.out.println("Table does not exist.");
                System.exit(-1);
            }
            //往mytable 里面添加newcf列族
            HColumnDescriptor newColumn=new HColumnDescriptor("newcf");
            newColumn.setCompactionCompressionType(Algorithm.GZ);
            newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
            admin.addColumn(tableName, newColumn);
            
            //获取表的定义
            HTableDescriptor table=admin.getTableDescriptor(tableName);
            
            //更新mycf这个列族
            HColumnDescriptor mycf=new HColumnDescriptor("mycf");
            newColumn.setCompactionCompressionType(Algorithm.GZ);
            newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
            table.modifyFamily(mycf);
            admin.modifyTable(tableName, table);        
        }
    }
    
    /**
     * 删除表操作
     * @param config
     * @throws IOException
     */
    public static void deleteSchema (Configuration config) throws IOException{
        try(Connection connection=ConnectionFactory.createConnection(config);
                Admin admin=connection.getAdmin()){
            TableName tableName=TableName.valueOf("mytable");
            //停用(disable)mytable
            admin.disableTable(tableName);
            //删除掉mycf列族
            admin.deleteColumn(tableName, "mycf".getBytes("UTF-8"));
            //删除mytable表(先停用再删除)
            admin.deleteTable(tableName);
        }
    }
    
    
    public static void main(String[] args) throws IOException, URISyntaxException {      
        Configuration config= HBaseConfiguration.create();
        //添加必要的配置文件
        config.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
        config.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
        config.set("hbase.zookeeper.quorum","47.106.221.38");
        config.set("zookeeper.znode.parent", "/hbase");
        config.set("hbase.client.retries.number", "3");
        config.set("hbase.rpc.timeout", "2000");
        config.set("hbase.client.operation.timeout", "3000");
        config.set("hbase.client.scanner.timeout.period", "10000");
        //建表
        createSchemaTables(config);
        //改表
        modifySchema(config);
        //删表
        deleteSchema(config);
        }
}


相关文章

网友评论

      本文标题:Java操作Hbase的基本操作

      本文链接:https://www.haomeiwen.com/subject/uficmqtx.html