分享知识-快乐本身:HBase编程

HBase编程:

一):大数据(hadoop)初始化环境搭建html

二):大数据(hadoop)环境搭建java

三):运行wordcount案例sql

四):揭秘HDFSshell

五):揭秘MapReduceapache

六):揭秘HBase编程

七):HBase编程windows

-----------------------------------------------------------------安全

HBase JavaAPI概述:

  1.HBase使用Java语言编写的,天然支持Java编程并发

  2.支持CRUD操做:create read update deleteoop

  3.JavaAPI包含了全部HBase的shell,甚至更多

  4.JavaAPI是访问HBase的最快方式

JavaAPI程序设计:

一、建立一个Configuration

Configuration conf = HbaseConfiguration.create();

Configuration对象包含了链接到HBase服务的信息:zookeeper的位置,链接时间等

HbaseConfiguration.create()内部逻辑:

  从CLASSPATH下加载hbase-default.xml和hbase-site.xml文件需将hbase-site.xml放入到CLASSPATH下 hbase-site.xml将覆盖hbase-default.xml的同名属性

自定义配置文件,使用Configuration加载

  Configuration newConf = Configuration.create(existingConf);

  用户自定义的配置文件将在已有配置文件以后加载将覆盖hbase-default.xml和 hbase-site.xml中的配置

自定义参数值:

Configuration conf=HbaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "admin-01,admin-02"); 

一般不推荐这么作!

二、建立HTable句柄

  提供Configuration对象和待访问Table名称 HTable table = new HTable(conf, tableName);

一个table对应一个HTbale句柄:

  提供了CRUD操做

  设计简单、使用方便 

  提供行级事务

  不支持多行事务或者表级别的事务 

  严格的行一致性 

  并发读、顺序写

建立HTable句柄代价很大  扫描.META.表等; 

  建立一次,之后尽量复用;

  若是须要建立多个Htable句柄,使用 HTablePool;

  HTable并不是线程安全的  一个线程建立一个便可 

  Htable支持CRUD批处理 

  非线程安全,仅是为了提升性能

三、执行相应的CRUD操做

  执行put、get、delete、scan等操做

  table.getTableName();

四、关闭HTable句柄

  将内存数据刷新到磁盘上  释放各类资源

  table.close();

前期工做:

将虚拟主机当中的hbase-site.xmlhdfs-site.xml文件复制到项目下的classpath

修改windows真机的hosts文件,添加三台机器的映射

导入POM文件:

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>1.3.1</version>
        </dependency>

HBase_API 操做:

package com.gdbd; import java.io.IOException; import java.util.Scanner; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; 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.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.util.Bytes; /** * Hello world! * */
public class App { public static void main(String[] args) throws IOException { do { System.out.println("\n\n*********************HBase_API***神码操做平台*********************"); System.out.println("一、建立表"); System.out.println("二、向表中添加数据"); System.out.println("三、查询表中全部数据"); System.out.println("四、查询表中指定RowKey的全部数据"); System.out.println("五、删除指定列族的指定列"); System.out.println("六、删除指定列族"); System.out.println("七、删除表中指定RowKey的全部数据删除表中指定RowKey的全部数据"); System.out.println("八、删除表"); Scanner input = new Scanner(System.in); System.out.println("请输入......"); int num = input.nextInt(); switch (num) { case 1: demo01(); break; case 2: demo02(); break; case 3: cat(); break; case 4: catRows(); break; case 5: delFamilyColumn(); break; case 6: delFamily(); break; case 7: delRow(); break; case 8: delTable(); break; } } while (true); } /*** * 建立表 * * @throws MasterNotRunningException * @throws ZooKeeperConnectionException * @throws IOException */
    private static void demo01() throws MasterNotRunningException, ZooKeeperConnectionException, IOException { System.out.println("正在 建立表...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); /** * 建立HBaseAdmin对象 此对象 提供了 建表,建立列族,检查表是否存在,修改表和列族结构,删除表等功能 HBaseAdmin实例的生命周期不宜太长 */ HBaseAdmin admin = new HBaseAdmin(configuration); if (admin.tableExists("hbase_demo")) { System.out.println("表已经存在!!!"); } else { // 证实表不存在
            HTableDescriptor table = new HTableDescriptor("hbase_demo"); // 建立表的描述对象 // new HColumnDescriptor对象:设置列族的特性
            table.addFamily(new HColumnDescriptor("grade")); table.addFamily(new HColumnDescriptor("course")); // 定义好了表名和列族 能够建立表
 admin.createTable(table); System.out.println("表建立成功!!!"); } } /*** * 向表中添加数据 * * @throws IOException */
    private static void demo02() throws IOException { System.out.println("正在  向表中添加数据...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HTable对象
        HTable table = new HTable(configuration, "hbase_demo"); // 建立put对象
        Put put = new Put("xiaohei".getBytes()); // xiaohei是 row key
        put.addColumn("course".getBytes(), "java".getBytes(), "90".getBytes()); // course是列族
        put.addColumn("course".getBytes(), "sql".getBytes(), "99".getBytes()); // java 和sql 都是列
        put.addColumn("grade".getBytes(), "one".getBytes(), "1".getBytes()); // 向表中增长数据
 table.put(put); // 关闭table
 table.close(); System.out.println("插入成功......"); } /*** * 查询表数据 * * @throws IOException */
    private static void cat() throws IOException { System.out.println("正在  查询表数据...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HTable对象
        HTable table = new HTable(configuration, "hbase_demo"); // 建立ResultScanner对象
        ResultScanner rs = table.getScanner(new Scan()); for (Result r : rs) { System.out.println("***************************Result Start***************************"); for (Cell cell : r.rawCells()) { System.out.println("=======================start============================"); System.out.println("RowKey(行键)===>" + Bytes.toString(r.getRow())); System.out.println("family(列族)===>" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("column(列)===>" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value(值)===>" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("=========================END=========================="); } System.out.println("****************************Result END****************************"); } // 关闭table
 table.close(); System.out.println("表数据查询成功..."); } /*** * 查询表中指定RowKey的全部数据 * * @throws IOException */
    private static void catRows() throws IOException { System.out.println("正在  查询表中指定RowKey的全部数据...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HTable对象
        HTable table = new HTable(configuration, "hbase_demo"); // 建立get对象 获取全部rowkey是xiaohei的全部数据
        Get get = new Get(Bytes.toBytes("xiaohei")); Result result = table.get(get); for (Cell cell : result.rawCells()) { System.out.println("=======================start============================"); System.out.println("RowKey(行键)===>" + Bytes.toString(result.getRow())); System.out.println("family(列族)===>" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("column(列)===>" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value(值)===>" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("=========================END==========================\n"); } // 关闭table
 table.close(); System.out.println("成功 查询表中指定RowKey的全部数据"); } /*** * 删除表中指定RowKey的全部数据 * * @throws IOException */
    private static void delRow() throws IOException { System.out.println("正在删除表中指定RowKey的全部数据...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HTable对象
        HTable table = new HTable(configuration, "hbase_demo"); // 建立delete对象
        Delete delete = new Delete(Bytes.toBytes("xiaohei")); table.delete(delete); // 关闭table
 table.close(); System.out.println("成功删除表中指定RowKey的全部数据..."); } /*** * 删除指定列族的指定列 * * @throws IOException */
    private static void delFamilyColumn() throws IOException { System.out.println("正在 删除指定列族的指定列...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HTable对象
        HTable table = new HTable(configuration, "hbase_demo"); // 建立delete对象
        Delete delete = new Delete(Bytes.toBytes("xiaobaibai"));// 指定的
        delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("java")); table.delete(delete); // 关闭table
 table.close(); System.out.println("成功 删除指定列族的指定列..."); } /*** * 删除指定列族的 * * @throws IOException */
    private static void delFamily() throws IOException { System.out.println("正在 删除指定列族...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HTable对象
        HTable table = new HTable(configuration, "hbase_demo"); // 建立delete对象
        Delete delete = new Delete(Bytes.toBytes("xiaobaibai"));// 指定的
        delete.addFamily("course".getBytes()); table.delete(delete); // 关闭table
 table.close(); System.out.println("成功 删除指定列族..."); } /*** * 删除表 * * @throws MasterNotRunningException * @throws ZooKeeperConnectionException * @throws IOException */
    private static void delTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException { System.out.println("正在 删除表...\n"); // 建立一个Configuration对象
        Configuration configuration = HBaseConfiguration.create(); // 建立HAdmin对象
        HBaseAdmin admin = new HBaseAdmin(configuration); admin.disableTable("hbase_demo");// 禁用表
        admin.deleteTable("hbase_demo");// 删除表 // 关闭admin对象
 admin.close(); System.out.println("成功 删除表..."); } }
相关文章
相关标签/搜索