《HBase不睡覺書》——客戶端API(基礎版)

HBase Maven依賴

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hbase.version>1.3.5</hbase.version>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>${hbase.version}</version>
        </dependency>
</dependencies>       

配置文件

  • 把HBase配置文件夾中的hbase-site.xml和Hadoop配置文件夾中的core-site.xml配置文件都從服務器上拖下來放到resources文件夾內
  • 截屏2020-03-0513.53.13.png

Hello HBase

  1. 于加載需要連接HBase的各項配置
    Configuration config = HBaseConfiguration.create();
  2. 添加配置文件
    config.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI())); config.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI()));
  3. 創(chuàng)建Connection
    Connection connection = ConnectionFactory.createConnection(config);
  4. 建表
    • 定義表名的TableName
      TableName tableName = TableName.valueOf("mytable");
    • 定義表屬性的HTableDescriptor
      HTableDescriptor table = new HTableDescriptor(tablename);
    • 創(chuàng)建列族
      HColumnDescriptor mycf=new HColumnDescriptor("mycf"); table.addFamily(new HColumnDescriptor(mycf));
    • 執(zhí)行
      Admin admin = connection.qetAdmin(); admin.createTable(table);
    • 結束
      admin.close(); connection.close();
  5. 綜上
public class Client {

    public static void main(String[] args) throws 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()));
        // 創(chuàng)建連接
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            Admin admin = connection.getAdmin();
            // 定義表名
            TableName tableName = TableName.valueOf("mytable");
            // 定義表
            HTableDescriptor table = new HTableDescriptor(tableName);
            // 定義列族
            HColumnDescriptor mycf = new HColumnDescriptor("mycf");
            table.addFamily(new HColumnDescriptor(mycf));
            // 執(zhí)行創(chuàng)建表動作
            admin.createTable(table);
            admin.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容