一共60个。
    image.pngimage.png

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>HBaseClientDemo</groupId>
    7. <artifactId>com.learn</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.apache.hbase</groupId>
    12. <artifactId>hbase-client</artifactId>
    13. <version>1.3.1</version>
    14. </dependency>
    15. </dependencies>
    16. <build>
    17. <!--<plugins>
    18. &lt;!&ndash; 该插件可以生成一个带依赖和不带依赖的jar包 &ndash;&gt;
    19. <plugin>
    20. <groupId>org.apache.maven.plugins</groupId>
    21. <artifactId>maven-shade-plugin</artifactId>
    22. <version>3.2.0</version>
    23. <configuration>
    24. <createDependencyReducedPom>true</createDependencyReducedPom>
    25. </configuration>
    26. <executions>
    27. <execution>
    28. <phase>package</phase>
    29. <goals>
    30. <goal>shade</goal>
    31. </goals>
    32. <configuration>
    33. <transformers>
    34. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    35. &lt;!&ndash; 主类的全限定名 &ndash;&gt;
    36. <mainClass>com.healchow.consumer.Main</mainClass>
    37. </transformer>
    38. </transformers>
    39. </configuration>
    40. </execution>
    41. </executions>
    42. </plugin>
    43. </plugins>-->
    44. <plugins>
    45. <!-- 该插件会把jar放到dependency目录 -->
    46. <plugin>
    47. <groupId>org.apache.maven.plugins</groupId>
    48. <artifactId>maven-dependency-plugin</artifactId>
    49. <executions>
    50. <execution>
    51. <id>copy</id>
    52. <phase>package</phase>
    53. <goals>
    54. <goal>copy-dependencies</goal>
    55. </goals>
    56. <!--<configuration>-->
    57. <!--<outputDirectory>${project.build.directory}/lib-->
    58. <!--</outputDirectory>-->
    59. <!--</configuration>-->
    60. </execution>
    61. </executions>
    62. </plugin>
    63. </plugins>
    64. </build>
    65. </project>

    基本API使用

    1. package com.learn.demo;
    2. import org.apache.hadoop.conf.Configuration;
    3. import org.apache.hadoop.hbase.*;
    4. import org.apache.hadoop.hbase.client.*;
    5. import org.apache.hadoop.hbase.util.Bytes;
    6. import java.io.IOException;
    7. public class HBaseDemo {
    8. private static Configuration conf = null;
    9. private static Admin admin = null;
    10. private static Connection connection = null;
    11. static {
    12. try {
    13. conf = HBaseConfiguration.create();
    14. conf.set("hbase.zookeeper.quorum","hadoop101,hadoop102,hadoop13");
    15. connection = ConnectionFactory.createConnection(conf);
    16. admin = connection.getAdmin();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. public static void main(String[] args) throws IOException {
    22. isExistsTable1("aaa");
    23. }
    24. /**
    25. * 老版本API
    26. */
    27. public static boolean isExistsTable1(String tableName) throws IOException {
    28. HBaseConfiguration conf = new HBaseConfiguration();
    29. conf.set("hbase.zookeeper.quorum","hadoop101,hadoop102,hadoop13");
    30. //由于设置了 hbase.zookeeper.property.clientPort 默认为2181 ,所有hbase.zookeeper.quorum只需写道ip ,否则需要加上端口
    31. //conf.set("hbase.zookeeper.quorum","hadoop101:2181,hadoop102:2181,hadoop13:2181");
    32. HBaseAdmin admin = new HBaseAdmin(conf);
    33. return admin.tableExists(tableName);
    34. }
    35. /**
    36. * 新版本API
    37. */
    38. public static boolean isExistsTable2(String tableName) throws IOException {
    39. Configuration conf = HBaseConfiguration.create();
    40. conf.set("hbase.zookeeper.quorum","hadoop101,hadoop102,hadoop13");
    41. //由于设置了 hbase.zookeeper.property.clientPort 默认为2181 ,所有hbase.zookeeper.quorum只需写道ip ,否则需要加上端口
    42. //conf.set("hbase.zookeeper.quorum","hadoop101:2181,hadoop102:2181,hadoop13:2181");
    43. Connection conn = ConnectionFactory.createConnection(conf);
    44. Admin admin = conn.getAdmin();
    45. return admin.tableExists(TableName.valueOf(tableName));
    46. }
    47. /**
    48. * 创建表
    49. * @param tableName 表名
    50. * @param cfs 列族名称
    51. * @throws IOException
    52. */
    53. public static void createTable(String tableName , String...cfs) throws IOException {
    54. if (cfs.length <= 0){
    55. System.out.println("参数不全");
    56. return;
    57. }
    58. if (isExistsTable2(tableName)){
    59. System.out.println("表已经存中");
    60. return;
    61. }
    62. //创建表描述器
    63. HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
    64. //循环添加列族信息
    65. for (String cf : cfs) {
    66. //创建列族描述信息
    67. HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
    68. //添加具体的列族信息
    69. descriptor.addFamily(hColumnDescriptor);
    70. }
    71. //创建表
    72. admin.createTable(descriptor);
    73. }
    74. /**
    75. * 删除表
    76. * @param tableName
    77. * @throws IOException
    78. */
    79. public static void dropTable(String tableName) throws IOException {
    80. if (!isExistsTable2(tableName)){
    81. System.out.println("表不存在");
    82. return;
    83. }
    84. //使表不可用
    85. admin.disableTable(TableName.valueOf(tableName));
    86. //删除表
    87. admin.deleteTable(TableName.valueOf(tableName));
    88. }
    89. /**
    90. * 创建命名空间
    91. * @param ns
    92. * @throws IOException
    93. */
    94. public static void creatNamespace(String ns) throws IOException {
    95. NamespaceDescriptor descriptor = NamespaceDescriptor.create(ns).build();
    96. admin.createNamespace(descriptor);
    97. }
    98. /**
    99. * 插入数据
    100. * @param tableName 表名
    101. * @param rowKey
    102. * @param cf 列族
    103. * @param cn 列名
    104. * @param value 数据
    105. * @throws IOException
    106. */
    107. public static void insertData(String tableName, String rowKey , String cf , String cn , String value) throws IOException {
    108. //获取表对象
    109. Table table = connection.getTable(TableName.valueOf(tableName));
    110. //创建Put对象
    111. Put put = new Put(Bytes.toBytes(rowKey));
    112. /**
    113. * @param family family name
    114. * @param qualifier column qualifier
    115. * @param value column value
    116. */
    117. put.addColumn(Bytes.toBytes(cf) , Bytes.toBytes(cn) , Bytes.toBytes(value));
    118. table.put(put);
    119. table.close();
    120. }
    121. }