华为云用户手册

  • NameNode 用于管理文件系统的命名空间、目录结构、元数据信息以及提供备份机制等,分为: Active NameNode:主NameNode,管理文件系统的命名空间、维护文件系统的目录结构树以及元数据信息;记录写入的每个“数据块”与其归属文件的对应关系。 Standby NameNode:备NameNode,与主NameNode中的数据保持同步;随时准备在主NameNode出现异常时接管其服务。
  • 操作步骤 以客户端安装用户,登录安装HBase客户端的节点。 进入HBase客户端安装目录: 例如:cd /opt/client 执行以下命令配置环境变量。 source bigdata_env 如果当前集群已启用Kerberos认证,执行以下命令认证当前用户,当前用户需要具有创建HBase表的权限,具体请参见创建角色配置拥有对应权限的角色,参考创建用户为用户绑定对应角色。如果当前集群未启用Kerberos认证,则无需执行此命令。 kinit MRS 集群用户 例如,kinit hbaseuser。 直接执行Phoenix客户端命令。 sqlline.py 建表: CREATE TABLE TEST (id VARCHAR PRIMARY KEY, name VARCHAR); 插入数据: UPSERT INTO TEST(id,name) VALUES ('1','jamee'); 查询数据: SELECT * FROM TEST; 删表: DROP TABLE TEST; 退出Phoenix命令行。 !quit
  • 操作场景 HBase客户端应用通过自定义加载主备集群配置项,实现了双读能力。HBase双读作为提高HBase集群系统高可用性的一个关键特性,适用于四个查询场景:使用Get读取数据、使用批量Get读取数据、使用Scan读取数据,以及基于二级索引查询。它能够同时读取主备集群数据,减少查询毛刺,具体表现为: 高成功率:双并发读机制,保证每一次读请求的成功率。 可用性:单集群故障时,查询业务不中断。短暂的网络抖动也不会导致查询时间变长。 通用性:双读特性不支持双写,但不影响原有的实时写场景。 易用性:客户端封装处理,业务侧不感知。 HBase双读使用约束: HBase双读特性基于Replication实现,备集群读取的数据可能和主集群存在差异,因此只能实现最终一致性。 目前HBase双读功能仅用于查询。主集群宕机时,最新数据无法同步,备集群可能查询不到最新数据。 HBase的Scan操作可能分解为多次RPC。由于相关session信息在不同集群间不同步,数据不能保证完全一致,因此双读只在第一次RPC时生效,ResultScanner close之前的请求会固定访问第一次RPC时使用的集群。 HBase Admin接口、实时写入接口只会访问主集群。所以主集群宕机后,不能提供Admin接口功能和实时写入接口功能,只能提供Get、Scan查询服务。
  • 代码样例 创建双读Configuration,下面代码片段在“com.huawei.bigdata.hbase.examples”包的“TestMain”类的init方法中添加。 private static void init() throws IOException { // Default load from conf directory conf = HBaseConfiguration.create(); //In Windows environment String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator; //In Linux environment //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator; conf.addResource(new Path(userdir + "hbase-dual.xml"), false); } 确定数据来源的集群 GET请求,以下代码片段在“com.huawei.bigdata.hbase.examples”包的“HBaseSample”类的testGet方法中添加。 Result result = table.get(get); if (result instanceof DualResult) { LOG.info(((DualResult)result).getClusterId()); } Scan请求,以下代码片段在“com.huawei.bigdata.hbase.examples”包的“HBaseSample”类的testScanData方法中添加。 ResultScanner rScanner = table.getScanner(scan); if (rScanner instanceof HBaseMultiScanner) { LOG.info(((HBaseMultiScanner)rScanner).getClusterId()); } 客户端支持打印metric信息 “log4j.properties”文件中增加如下内容,客户端将metric信息输出到指定文件。指标项信息可参考打印metric信息。 log4j.logger.DUAL=debug,DUAL log4j.appender.DUAL=org.apache.log4j.RollingFileAppender log4j.appender.DUAL.File=/var/log/dual.log //客户端本地双读日志路径,根据实际路径修改,但目录要有写入权限log4j.additivity.DUAL=false log4j.appender.DUAL.MaxFileSize=${hbase.log.maxfilesize} log4j.appender.DUAL.MaxBackupIndex=${hbase.log.maxbackupindex} log4j.appender.DUAL.layout=org.apache.log4j.PatternLayout log4j.appender.DUAL.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n
  • Impala SQL编写之不支持隐式类型转换 查询语句使用字段的值做过滤时,不支持使用Hive类似的隐式类型转换来编写Impala SQL: Impala示例: select * from default.tbl_src where id = 10001;select * from default.tbl_src where name = 'TestName'; Hive示例(支持隐式类型转换): select * from default.tbl_src where id = '10001';select * from default.tbl_src where name = TestName; 表tbl_src的id字段为Int类型,name字段为String类型。
  • 回答 bulkload是通过启动MapReduce任务直接生成HFile文件,再将HFile文件注册到HBase,因此错误的使用bulkload会因为启动MapReduce任务而占用更多的集群内存和CPU资源,也可能会生成大量很小的HFile文件频繁的触发Compaction,导致查询速度急剧下降。 错误的使用put,会造成数据加载慢,当分配给RegionServer内存不足时会造成RegionServer内存溢出从而导致进程退出。 下面给出bulkload和put适合的场景: bulkload适合的场景: 大量数据一次性加载到HBase。 对数据加载到HBase可靠性要求不高,不需要生成WAL文件。 使用put加载大量数据到HBase速度变慢,且查询速度变慢时。 加载到HBase新生成的单个HFile文件大小接近HDFS block大小。 put适合的场景: 每次加载到单个Region的数据大小小于HDFS block大小的一半。 数据需要实时加载。 加载数据过程不会造成用户查询速度急剧下降。
  • 操作场景 HBase把Thrift结合起来可以向外部应用提供HBase服务。在HBase服务安装时可选部署ThriftServer实例,ThriftServer系统可访问HBase的用户,拥有HBase所有NameSpace和表的读、写、执行、创建和管理的权限。访问ThriftServer服务同样需要进行Kerberos认证。HBase实现了两套Thrift Server服务,此处“hbase-thrift-example”为ThriftServer实例服务的调用实现。
  • 接口使用建议 建议使用org.apache.hadoop.hbase.Cell作为KV数据对象,而不是org.apache.hadoop.hbase.KeyValue。 建议使用Connection connection = ConnectionFactory.createConnection(conf)来创建连接,废弃HTablePool。 建议使用org.apache.hadoop.hbase.mapreduce,不建议使用org.apache.hadoop.hbase.mapred。 建议通过构造出来的Connection对象的getAdmin()方法来获取HBase的客户端操作对象。
  • 场景说明 当不同的多个Manager系统下安全模式的集群需要互相访问对方的资源时,管理员可以设置互信的系统,使外部系统的用户可以在本系统中使用。每个系统用户安全使用的范围定义为“域”,不同的Manager系统需要定义唯一的域名。跨Manager访问实际上就是用户跨域使用。集群配置互信具体操作步骤请参考集群互信管理章节。 多集群互信场景下,以符合跨域访问的用户身份,使用从其中一个manager系统中获取到的用于Kerberos安全认证的keytab文件和principal文件,以及多个Manager系统各自的客户端配置文件,可实现一次认证登录后访问调用多集群的HBase服务。 以下代码在hbase-example样例工程的“com.huawei.bigdata.hbase.examples”包的“TestMultipleLogin”类中。
  • 配置安全登录 请根据实际情况,在hbase-example样例工程的“com.huawei.bigdata.hbase.examples”包的“TestMultipleLogin”类中修改“userName”为实际用户名,例如“developuser”。 private static void login(Configuration conf, String confDir) throwsIOException { if (User.isHBaseSecurityEnabled(conf)) { userName = " developuser "; //In Windows environment String userdir = TestMain.class.getClassLoader().getResource(confDir).getPath() + File.separator; //In Linux environment //String userdir = System.getProperty("user.dir") + File.separator + confDir + File.separator; userKeytabFile = userdir + "user.keytab"; krb5File = userdir + "krb5.conf"; /* * if need to connect zk, please provide jaas info about zk. of course, * you can do it as below: * System.setProperty("java.security.auth.login.config",confDirPath + * "jaas.conf"); but the demo can help you more : Note: if this process * will connect more than one zk cluster, the demo may be not proper. you * can contact us for more help */ LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userName,userKeytabFile); LoginUtil.login(userName, userKeytabFile, krb5File, conf); }}
  • 操作步骤 通过运行日志可查看应用提交后的执行详情,例如,hbase-example样例运行成功后,显示信息如下: 2280 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Entering testCreateTable.3091 [main] WARNcom.huawei.hadoop.hbase.example.HBaseSample- table already exists3091 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Exiting testCreateTable.3091 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Entering testPut.3264 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Put successfully.3264 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Exiting testPut.3264 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Entering testGet.3283 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- 012005000201:info,address,Shenzhen, Guangdong3283 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- 012005000201:info,name,yugeZhang San3283 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Get data successfully.3283 [main] INFOcom.huawei.hadoop.hbase.example.HBaseSample- Exiting testGet.3283 [main] INFOorg.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation- Closing zookeeper sessionid=0xd000035eba278e93297 [main] INFOorg.apache.zookeeper.ZooKeeper- Session: 0xd000035eba278e9 closed3297 [main-EventThread] INFOorg.apache.zookeeper.ClientCnxn- EventThread shut down-----------finish HBase -------------------
  • 操作步骤 导出Jar包。具体步骤请参考安装客户端时编译并运行程序章节的1。 准备依赖的Jar包和配置文件。 在Linux环境新建目录,例如“/opt/test”,并创建子目录“lib”和“conf”。将样例工程依赖的Jar包导出,导出步骤请参考安装客户端时编译并运行程序章节的2,以及1导出的Jar包,上传到Linux的“lib”目录。将准备连接集群配置文件获取的配置文件及认证文件上传到Linux中“conf”目录。 在“/opt/test”根目录新建脚本“run.sh”,修改内容如下并保存: #!/bin/shBASEDIR=`cd $(dirname $0);pwd`cd ${BASEDIR}for file in ${BASEDIR}/lib/*.jardoi_cp=$i_cp:$fileecho "$file"donefor file in ${BASEDIR}/conf/*doi_cp=$i_cp:$filedonejava -cp .${i_cp} com.huawei.bigdata.hbase.examples.TestMain 其中,com.huawei.bigdata.hbase.examples.TestMain为举例,具体以实际样例代码为准。 切换到“/opt/test”,执行以下命令,运行Jar包。 sh run.sh
  • 操作步骤 hbase-example样例运行成功后,显示信息如下: ...2020-09-09 22:11:48,496 INFO [main] example.TestMain: Entering testCreateTable.2020-09-09 22:11:48,894 INFO [main] example.TestMain: Creating table...2020-09-09 22:11:50,545 INFO [main] example.TestMain: Master: 10-1-131-140,16000,1441784082485Number of backup masters: 1 10-1-131-130,16000,1441784098969Number of live region servers: 3 10-1-131-150,16020,1441784158435 10-1-131-130,16020,1441784126506 10-1-131-140,16020,1441784118303Number of dead region servers: 0Average load: 1.0Number of requests: 0Number of regions: 3Number of regions in transition: 02020-09-09 22:11:50,562 INFO [main] example.TestMain: Lorg.apache.hadoop.hbase.NamespaceDescriptor;@11c6af62020-09-09 22:11:50,562 INFO [main] example.TestMain: Table created successfully.2020-09-09 22:11:50,563 INFO [main] example.TestMain: Exiting testCreateTable.2020-09-09 22:11:50,563 INFO [main] example.TestMain: Entering testMultiSplit.2020-09-09 22:11:50,630 INFO [main] example.TestMain: MultiSplit successfully.2020-09-09 22:11:50,630 INFO [main] example.TestMain: Exiting testMultiSplit.2020-09-09 22:11:50,630 INFO [main] example.TestMain: Entering testPut.2020-09-09 22:11:51,148 INFO [main] example.TestMain: Put successfully.2020-09-09 22:11:51,148 INFO [main] example.TestMain: Exiting testPut.2020-09-09 22:11:51,148 INFO [main] example.TestMain: Entering createIndex.... 在Windows环境运行样例代码时会出现下面的异常,但是不影响业务: java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries. 日志说明 日志级别默认为INFO,可以通过调整日志打印级别(DEBUG,INFO,WARN,ERROR,FATL)来显示更详细的信息。可以通过修改“log4j.properties”文件来实现,如: hbase.root.logger=INFO,console...log4j.logger.org.apache.zookeeper=INFO#log4j.logger.org.apache.hadoop.fs.FSNamesystem=DEBUGlog4j.logger.org.apache.hadoop.hbase=INFO# Make these two classes DEBUG-level. Make them DEBUG to see more zk debug.log4j.logger.org.apache.hadoop.hbase.zookeeper.ZKUtil=INFOlog4j.logger.org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher=INFO...
  • 代码样例 以下代码片段在“hbase-zk-example\src\main\java\com\huawei\hadoop\hbase\example”包的“TestZKSample”类中,用户主要需要关注“login”和“connectApacheZK”这两个方法。 private static void login(String keytabFile, String principal) throws IOException { conf = HBaseConfiguration.create(); //In Windows environment String confDirPath = TestZKSample.class.getClassLoader().getResource("").getPath() + File.separator;[1] //In Linux environment //String confDirPath = System.getProperty("user.dir") + File.separator + "conf" + File.separator; // Set zoo.cfg for hbase to connect to fi zookeeper. conf.set("hbase.client.zookeeper.config.path", confDirPath + "zoo.cfg"); if (User.isHBaseSecurityEnabled(conf)) { // jaas.conf file, it is included in the client pakcage file System.setProperty("java.security.auth.login.config", confDirPath + "jaas.conf"); // set the kerberos server info,point to the kerberosclient System.setProperty("java.security.krb5.conf", confDirPath + "krb5.conf"); // set the keytab file name conf.set("username.client.keytab.file", confDirPath + keytabFile); // set the user's principal try { conf.set("username.client.kerberos.principal", principal); User.login(conf, "username.client.keytab.file", "username.client.kerberos.principal", InetAddress.getLocalHost().getCanonicalHostName()); } catch (IOException e) { throw new IOException("Login failed.", e); } } } private void connectApacheZK() throws IOException, org.apache.zookeeper.KeeperException { try { // Create apache zookeeper connection. ZooKeeper digestZk = new ZooKeeper("127.0.0.1:2181", 60000, null); LOG.info("digest directory:{}", digestZk.getChildren("/", null)); LOG.info("Successfully connect to apache zookeeper."); } catch (InterruptedException e) { LOG.error("Found error when connect apache zookeeper ", e); } }
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“PhoenixSample”类的testPut方法中。 /** * Put data */ public void testPut() { LOG.info("Entering testPut."); String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum"); // Insert String upsertSQL = "UPSERT INTO TEST VALUES(1,'John','100000', TO_DATE('1980-01-01','yyyy-MM-dd'))"; try (Connection conn = DriverManager.getConnection(url, props); Statement stat = conn.createStatement()){ // Execute Update SQL stat.executeUpdate(upsertSQL); conn.commit(); LOG.info("Put successfully."); } catch (Exception e) { LOG.error("Put failed.", e); } LOG.info("Exiting testPut."); }
  • 注意事项 注[1]:创建联合索引 HBase支持在多个字段上创建二级索引,例如在列name和age上。 HIndexSpecification iSpecUnite = new HIndexSpecification(indexName); iSpecUnite.addIndexColumn(new HColumnDescriptor("info"), "name", ValueType.String); iSpecUnite.addIndexColumn(new HColumnDescriptor("info"), "age", ValueType.String);
  • 功能介绍 针对添加了二级索引的用户表,您可以通过Filter来查询数据。其数据查询性能高于针对无二级索引用户表的数据查询。 HIndex支持的Filter类型为“SingleColumnValueFilter”,“SingleColumnValueExcludeFilter”以及“SingleColumnValuePartitionFilter”。 HIndex支持的Comparator为“BinaryComparator”,“BitComparator”,“LongComparator”,“DecimalComparator”,“DoubleComparator”,“FloatComparator”,“IntComparator”,“NullComparator”。 二级索引的使用规则如下: 针对某一列或者多列创建了单索引的场景下: 当查询时使用此列进行过滤时,不管是AND还是OR操作,该索引都会被利用来提升查询性能。 例如:Filter_Condition(IndexCol1) AND/OR Filter_Condition(IndexCol2) 当查询时使用“索引列AND非索引列”过滤时,此索引会被利用来提升查询性能。 例如:Filter_Condition(IndexCol1) AND Filter_Condition(IndexCol2) AND Filter_Condition(NonIndexCol1) 当查询时使用“索引列OR非索引列”过滤时,此索引将不会被使用,查询性能不会因为索引得到提升。 例如:Filter_Condition(IndexCol1) AND/OR Filter_Condition(IndexCol2) OR Filter_Condition(NonIndexCol1) 针对多个列创建的联合索引场景下: 当查询时使用的列(多个),是联合索引所有对应列的一部分或者全部,且列的顺序与联合索引一致时,此索引会被利用来提升查询性能。 例如,针对C1、C2、C3列创建了联合索引,生效的场景包括: Filter_Condition(IndexCol1) AND Filter_Condition(IndexCol2) AND Filter_Condition(IndexCol3) Filter_Condition(IndexCol1) AND Filter_Condition(IndexCol2) Filter_Condition(IndexCol1) 不生效的场景包括: Filter_Condition(IndexCol2) AND Filter_Condition(IndexCol3) Filter_Condition(IndexCol1) AND Filter_Condition(IndexCol3) Filter_Condition(IndexCol2) Filter_Condition(IndexCol3) 当查询时使用“索引列AND非索引列”过滤时,此索引会被利用来提升查询性能。 例如: Filter_Condition(IndexCol1) AND Filter_Condition(NonIndexCol1) Filter_Condition(IndexCol1) AND Filter_Condition(IndexCol2) AND Filter_Condition(NonIndexCol1) 当查询时使用“索引列OR非索引列”过滤时,此索引不会被使用,查询性能不会因为索引得到提升。 例如: Filter_Condition(IndexCol1) OR Filter_Condition(NonIndexCol1) (Filter_Condition(IndexCol1) AND Filter_Condition(IndexCol2))OR ( Filter_Condition(NonIndexCol1)) 当查询时使用多个列进行范围查询时,只有联合索引中最后一个列可指定取值范围,前面的列只能设置为“=”。 例如:针对C1、C2、C3列创建了联合索引,需要进行范围查询时,只能针对C3设置取值范围,过滤条件为“C1=XXX,C2=XXX,C3=取值范围”。 针对添加了二级索引的用户表,可以通过Filter来查询数据,在单列索引和复合列索引上进行过滤查询,查询结果都与无索引结果相同,且其数据查询性能高于无二级索引用户表的数据查询性能。
  • 代码样例 下面代码片段在com.huawei.hadoop.hbase.example包的“HBaseSample”类的testScanDataByIndex方法中: 样例:使用二级索引查找数据 public void testScanDataByIndex() { LOG.info("Entering testScanDataByIndex."); Table table = null; ResultScanner scanner = null; try { table = conn.getTable(tableName); // Create a filter for indexed column. Filter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOperator.EQUAL, "Li Gang".getBytes()); Scan scan = new Scan(); scan.setFilter(filter); scanner = table.getScanner(scan); LOG.info("Scan indexed data."); for (Result result : scanner) { for (Cell cell : result.rawCells()) { LOG.info("{}:{},{},{}", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); } } LOG.info("Scan data by index successfully."); } catch (IOException e) { LOG.error("Scan data by index failed."); } finally { if (scanner != null) { // Close the scanner object. scanner.close(); } try { if (table != null) { table.close(); } } catch (IOException e) { LOG.error("Close table failed."); } } LOG.info("Exiting testScanDataByIndex."); }
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testDelete方法中。 public void testDelete() { LOG.info("Entering testDelete."); byte[] rowKey = Bytes.toBytes("012005000201"); Table table = null; try { // Instantiate an HTable object. table = conn.getTable(tableName); // Instantiate an Delete object. Delete delete = new Delete(rowKey); // Submit a delete request. table.delete(delete); LOG.info("Delete table successfully."); } catch (IOException e) { LOG.error("Delete table failed " ,e); } finally { if (table != null) { try { // Close the HTable object. table.close(); } catch (IOException e) { LOG.error("Close table failed " ,e); } } } LOG.info("Exiting testDelete."); } 如果被删除的cell所在的列族上设置了二级索引,也会同步删除索引数据。
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“PhoenixSample”类的testSelect方法中。 /** * Select Data */ public void testSelect() { LOG.info("Entering testSelect."); String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum"); // Query String querySQL = "SELECT * FROM TEST WHERE id = ?"; Connection conn = null; PreparedStatement preStat = null; Statement stat = null; ResultSet result = null; try { // Create Connection conn = DriverManager.getConnection(url, props); // Create Statement stat = conn.createStatement(); // Create PrepareStatement preStat = conn.prepareStatement(querySQL); // Execute query preStat.setInt(1, 1); result = preStat.executeQuery(); // Get result while (result.next()) { int id = result.getInt("id"); String name = result.getString(1); System.out.println("id: " + id); System.out.println("name: " + name); } LOG.info("Select successfully."); } catch (Exception e) { LOG.error("Select failed.", e); } finally { if (null != result) { try { result.close(); } catch (Exception e2) { LOG.error("Result close failed.", e2); } } if (null != stat) { try { stat.close(); } catch (Exception e2) { LOG.error("Stat close failed.", e2); } } if (null != conn) { try { conn.close(); } catch (Exception e2) { LOG.error("Connection close failed.", e2); } } } LOG.info("Exiting testSelect."); }
  • 功能介绍 HBase通过ConnectionFactory.createConnection(configuration)方法创建Connection对象。传递的参数为上一步创建的Configuration。 Connection封装了底层与各实际服务器的连接以及与ZooKeeper的连接。Connection通过ConnectionFactory类实例化。创建Connection是重量级操作,Connection是线程安全的,因此,多个客户端线程可以共享一个Connection。 典型的用法,一个客户端程序共享一个单独的Connection,每一个线程获取自己的Admin或Table实例,然后调用Admin对象或Table对象提供的操作接口。不建议缓存或者池化Table、Admin。Connection的生命周期由调用者维护,调用者通过调用close(),释放资源。
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testScanData方法中。 public void testScanData() { LOG.info("Entering testScanData."); Table table = null; // Instantiate a ResultScanner object. ResultScanner rScanner = null; try { // Create the Configuration instance. table = conn.getTable(tableName); // Instantiate a Get object. Scan scan = new Scan(); scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name")); // Set the cache size. scan.setCaching(1000); // Submit a scan request. rScanner = table.getScanner(scan); // Print query results. for (Result r = rScanner.next(); r != null; r = rScanner.next()) { for (Cell cell : r.rawCells()) { LOG.info("{}:{},{},{}", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); } } LOG.info("Scan data successfully."); } catch (IOException e) { LOG.error("Scan data failed " ,e); } finally { if (rScanner != null) { // Close the scanner object. rScanner.close(); } if (table != null) { try { // Close the HTable object. table.close(); } catch (IOException e) { LOG.error("Close table failed " ,e); } } } LOG.info("Exiting testScanData."); }
  • 注意事项 当前二级索引不支持使用SubstringComparator类定义的对象作为Filter的比较器。 例如,如下示例中的用法当前不支持: Scan scan = new Scan();filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier),CompareOperator.EQUAL, new SubstringComparator(substring)));scan.setFilter(filterList);
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“PhoenixSample”类的testCreateTable方法中。 /** * Create Table */ public void testCreateTable() { LOG.info("Entering testCreateTable."); String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum"); // Create table String createTableSQL = "CREATE TABLE IF NOT EXISTS TEST (id integer not null primary key, name varchar, " + "account char(6), birth date)"; try (Connection conn = DriverManager.getConnection(url, props); Statement stat = conn.createStatement()) { // Execute Create SQL stat.executeUpdate(createTableSQL); LOG.info("Create table successfully."); } catch (Exception e) { LOG.error("Create table failed.", e); } LOG.info("Exiting testCreateTable."); } /** * Drop Table */ public void testDrop() { LOG.info("Entering testDrop."); String URL = "jdbc:phoenix:" + conf.get("hbase.zookeeper.quorum"); // Delete table String dropTableSQL = "DROP TABLE TEST"; try (Connection conn = DriverManager.getConnection(url, props); Statement stat = conn.createStatement()) { stat.executeUpdate(dropTableSQL); LOG.info("Drop successfully."); } catch (Exception e) { LOG.error("Drop failed.", e); } LOG.info("Exiting testDrop."); }
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testGet方法中。 public void testGet() { LOG.info("Entering testGet."); // Specify the column family name. byte[] familyName = Bytes.toBytes("info"); // Specify the column name. byte[][] qualifier = { Bytes.toBytes("name"), Bytes.toBytes("address") }; // Specify RowKey. byte[] rowKey = Bytes.toBytes("012005000201"); Table table = null; try { // Create the Table instance. table = conn.getTable(tableName); // Instantiate a Get object. Get get = new Get(rowKey); // Set the column family name and column name. get.addColumn(familyName, qualifier[0]); get.addColumn(familyName, qualifier[1]); // Submit a get request. Result result = table.get(get); // Print query results. for (Cell cell : result.rawCells()) { LOG.info("{}:{},{},{}", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); } LOG.info("Get data successfully."); } catch (IOException e) { LOG.error("Get data failed " ,e); } finally { if (table != null) { try { // Close the HTable object. table.close(); } catch (IOException e) { LOG.error("Close table failed " ,e); } } } LOG.info("Exiting testGet."); }
  • 代码样例 以下代码片段是登录,创建Connection并创建表的示例,在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的HBaseSample方法中。 private TableName tableName = null; private Connection conn = null; public HBaseSample(Configuration conf) throws IOException { this.tableName = TableName.valueOf("hbase_sample_table"); this.conn = ConnectionFactory.createConnection(conf);}
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testSingleColumnValueFilter方法中。 public void testSingleColumnValueFilter() { LOG.info("Entering testSingleColumnValueFilter."); Table table = null; ResultScanner rScanner = null; try { table = conn.getTable(tableName); Scan scan = new Scan(); scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name")); // Set the filter criteria. SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOperator.EQUAL, Bytes.toBytes("Xu Bing")); scan.setFilter(filter); // Submit a scan request. rScanner = table.getScanner(scan); // Print query results. for (Result r = rScanner.next(); r != null; r = rScanner.next()) { for (Cell cell : r.rawCells()) { LOG.info("{}:{},{},{}", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell))); } } LOG.info("Single column value filter successfully."); } catch (IOException e) { LOG.error("Single column value filter failed " ,e); } finally { if (rScanner != null) { // Close the scanner object. rScanner.close(); } if (table != null) { try { // Close the HTable object. table.close(); } catch (IOException e) { LOG.error("Close table failed " ,e); } } } LOG.info("Exiting testSingleColumnValueFilter."); }
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testCreateTable方法中。 public void testCreateTable() { LOG.info("Entering testCreateTable."); // Specify the table descriptor. TableDescriptorBuilder htd = TableDescriptorBuilder.newBuilder(tableName);(1) // Set the column family name to info. ColumnFamilyDescriptorBuilder hcd = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"));(2) // Set data encoding methods, HBase provides DIFF,FAST_DIFF,PREFIX hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF); // Set compression methods, HBase provides two default compression // methods:GZ and SNAPPY // GZ has the highest compression rate,but low compression and // decompression effeciency,fit for cold data // SNAPPY has low compression rate, but high compression and // decompression effeciency,fit for hot data. // it is advised to use SNAANPPY hcd.setCompressionType(Compression.Algorithm.SNAPPY);//注[1] htd.setColumnFamily(hcd.build()); (3) Admin admin = null; try { // Instantiate an Admin object. admin = conn.getAdmin(); (4) if (!admin.tableExists(tableName)) { LOG.info("Creating table..."); admin.createTable(htd.build());//注[2] (5) LOG.info(admin.getClusterMetrics().toString()); LOG.info(admin.listNamespaceDescriptors().toString()); LOG.info("Table created successfully."); } else { LOG.warn("table already exists"); } } catch (IOException e) { LOG.error("Create table failed " ,e); } finally { if (admin != null) { try { // Close the Admin object. admin.close(); } catch (IOException e) { LOG.error("Failed to close admin " ,e); } } } LOG.info("Exiting testCreateTable."); }
  • 注意事项 注[1] 可以设置列族的压缩方式,代码片段如下: //设置编码算法,HBase提供了DIFF,FAST_DIFF,PREFIX三种编码算法。 hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF); //设置文件压缩方式,HBase默认提供了GZ和SNAPPY两种压缩算法 //其中GZ的压缩率高,但压缩和解压性能低,适用于冷数据 //SNAPPY压缩率低,但压缩解压性能高,适用于热数据 //建议默认开启SNAPPY压缩 hcd.setCompressionType(Compression.Algorithm.SNAPPY); 注[2] 可以通过指定起始和结束RowKey,或者通过RowKey数组预分Region两种方式建表,代码片段如下: // 创建一个预划分region的表 byte[][] splits = new byte[4][]; splits[0] = Bytes.toBytes("A"); splits[1] = Bytes.toBytes("H"); splits[2] = Bytes.toBytes("O"); splits[3] = Bytes.toBytes("U"); admin.createTable(htd, splits);
  • 代码样例 以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testModifyTable方法中。 public void testModifyTable() { LOG.info("Entering testModifyTable."); // Specify the column family name. byte[] familyName = Bytes.toBytes("education"); Admin admin = null; try { // Instantiate an Admin object. admin = conn.getAdmin(); // Obtain the table descriptor. TableDescriptor htd = admin.getTableDescriptor(tableName); // Check whether the column family is specified before modification. if (!htd.hasColumnFamily(familyName)) { // Create the column descriptor. TableDescriptor tableBuilder = TableDescriptorBuilder.newBuilder(htd) .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(familyName).build()).build(); // Disable the table to get the table offline before modifying // the table. admin.disableTable(tableName);//注[1] // Submit a modifyTable request. admin.modifyTable(tableBuilder); // Enable the table to get the table online after modifying the // table. admin.enableTable(tableName); } LOG.info("Modify table successfully."); } catch (IOException e) { LOG.error("Modify table failed " ,e); } finally { if (admin != null) { try { // Close the Admin object. admin.close(); } catch (IOException e) { LOG.error("Close admin failed " ,e); } } } LOG.info("Exiting testModifyTable."); }
共100000条