5、HBase API入门

准备工作

  1. 创建maven工程,在pom.xml添加
<dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
  1. 在resource目录下添加log4j.properties文件
log4j.rootLogger=INFO, stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n 
log4j.appender.logfile=org.apache.log4j.FileAppender 
log4j.appender.logfile.File=target/spring.log 
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
  1. 在resource目录下添加core-site.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
    <!-- 指定HDFS中NameNode的地址 -->
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://hadoop-100:9000</value>
    </property>

    <!-- 指定hadoop运行时产生文件的存储目录 -->
    <property>
        <name>hadoop.tmp.dir</name>
        <value>/opt/module/hadoop-2.8.3/data/tmp</value>
    </property>

    <property>
                <name>hadoop.proxyuser.hue.hosts</name>
                <value>*</value>
        </property>

    <property>
                <name>hadoop.proxyuser.hue.groups</name>
                <value>*</value>
        </property>
</configuration>
  1. 在resource目录下添加hbase-site.xml文件
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>
    <property>     
        <name>hbase.rootdir</name>     
        <value>hdfs://hadoop-100:9000/hbase</value>   
    </property>

    <property>   
        <name>hbase.cluster.distributed</name>
        <value>true</value>
    </property>

   <!-- 0.98后的新变动,之前版本没有.port,默认端口为60000 -->
    <property>
        <name>hbase.master.port</name>
        <value>16000</value>
    </property>

    <property>   
        <name>hbase.zookeeper.quorum</name>
         <value>hadoop-100:2181,hadoop-101:2181,hadoop-102:2181</value>
    </property>

    <property>   
        <name>hbase.zookeeper.property.dataDir</name>
         <value>/opt/module/zookeeper-3.4.10/zkData</value>
    </property>
</configuration>

快速开始

public class TenQuiceStart {
    public static void main(String[] args) throws IOException, URISyntaxException {
        // 获取配置文件
        Configuration configuration = HBaseConfiguration.create();
        configuration.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
        configuration.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
        try (
                // 创建连接
                Connection connection = ConnectionFactory.createConnection(configuration);
                Admin admin = connection.getAdmin()) {
            // 定义表名
            TableName apitable = TableName.valueOf("apitable");
            // 定义表
            HTableDescriptor hTableDescriptor = new HTableDescriptor(apitable);
            // 定义列族
            hTableDescriptor.addFamily(new HColumnDescriptor("apicf"));
            // 执行创建表操作
            admin.createTable(hTableDescriptor);
        }
    }
}

快速开始升级

ublic class ThirtyQuickStart {
    public static void main(String[] args) throws IOException, URISyntaxException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
        configuration.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI()));
        try (
                Connection connection = ConnectionFactory.createConnection(configuration);
                Admin admin = connection.getAdmin();
        ) {
            TableName mytable = TableName.valueOf("mytable");
            HTableDescriptor descriptor = new HTableDescriptor(mytable);
            descriptor.addFamily(new HColumnDescriptor("mycf").setCompressionType(Compression.Algorithm.NONE));
            createOrOverwrite(admin, descriptor);
            descriptor = new HTableDescriptor(mytable);
            descriptor.addFamily(new HColumnDescriptor("mycfadd1").setCompressionType(Compression.Algorithm.NONE));
            descriptor.addFamily(new HColumnDescriptor("mycfadd2").setCompressionType(Compression.Algorithm.NONE));
            descriptor.addFamily(new HColumnDescriptor("mycfadd3").setCompressionType(Compression.Algorithm.NONE));
            addColumnFamilys(admin, descriptor);
            descriptor = new HTableDescriptor(mytable);
            descriptor.addFamily(new HColumnDescriptor("mycfadd1").setCompressionType(Compression.Algorithm.NONE).setMaxVersions(4));
            descriptor.addFamily(new HColumnDescriptor("mycfadd2").setCompressionType(Compression.Algorithm.NONE).setMaxVersions(4));
            descriptor.addFamily(new HColumnDescriptor("mycfadd3").setCompressionType(Compression.Algorithm.NONE).setMaxVersions(4));
            modifyColumnFamilys(admin, descriptor);
            deleteColumnFamily(admin, mytable, "mycfadd3");
            deleteTable(admin, mytable);
        }
    }

    /**
     * 检查表是否存在,如果存在就删除掉再重新建立
     *
     * @param admin
     * @param descriptor
     * @throws IOException
     */
    public static void createOrOverwrite(Admin admin, HTableDescriptor descriptor) throws IOException, URISyntaxException {
        // 判断表存不存在
        if (admin.tableExists(descriptor.getTableName())) {
            // 禁用表
            admin.disableTable(descriptor.getTableName());
            // 删除表
            admin.deleteTable(descriptor.getTableName());
        }
        createTable(admin, descriptor);
    }

    /**
     * 创建表
     *
     * @param descriptor
     * @throws URISyntaxException
     * @throws IOException
     */
    public static void createTable(Admin admin, HTableDescriptor descriptor) throws URISyntaxException, IOException {
        admin.createTable(descriptor);
    }

    /**
     * 添加列族
     *
     * @param descriptor
     * @throws IOException
     */
    public static void addColumnFamilys(Admin admin, HTableDescriptor descriptor) throws IOException, URISyntaxException {
        if (!admin.tableExists(descriptor.getTableName())) {
            System.out.println(descriptor.getTableName() + " doesn't exist");
            System.exit(-1);
        }
        HColumnDescriptor[] columnFamilies = descriptor.getColumnFamilies();
        for (HColumnDescriptor columnDescriptor : columnFamilies) {
            admin.addColumn(descriptor.getTableName(), columnDescriptor);
        }
    }

    /**
     * 修改列族
     *
     * @param descriptor
     * @throws URISyntaxException
     * @throws IOException
     */
    public static void modifyColumnFamilys(Admin admin, HTableDescriptor descriptor) throws URISyntaxException, IOException {

        if (!admin.tableExists(descriptor.getTableName())) {
            System.out.println(descriptor.getTableName() + " doesn't exist");
            System.exit(-1);
        }
        HColumnDescriptor[] columnFamilies = descriptor.getColumnFamilies();
        // for(HColumnDescriptor columnDescriptor : columnFamilies) {
        //     admin.modifyColumn(descriptor.getTableName(), columnDescriptor);
        // }
        HTableDescriptor tableDescriptor = admin.getTableDescriptor(descriptor.getTableName());
        for (HColumnDescriptor columnDescriptor : columnFamilies) {
            tableDescriptor.modifyFamily(columnDescriptor);
            admin.modifyTable(descriptor.getTableName(), tableDescriptor);
        }
    }

    /**
     * 删除列族
     *
     * @param tableName
     * @param columnFamilyName
     * @throws URISyntaxException
     * @throws IOException
     */
    public static void deleteColumnFamily( Admin admin, TableName tableName, String columnFamilyName) throws URISyntaxException, IOException {
        admin.disableTable(tableName);
        admin.deleteColumn(tableName, columnFamilyName.getBytes());
        admin.enableTable(tableName);
    }

    /**
     * 删除表
     *
     * @param tableName
     * @throws URISyntaxException
     * @throws IOException
     */
    public static void deleteTable(Admin admin, TableName tableName) throws URISyntaxException, IOException {

        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }
}

CRUD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值