2021年7月13日星期二

IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI

在此之前要配置好三节点的hadoop集群,zookeeper集群,并启动它们,然后再配置好HBase环境

本文只是HBase2.3.5API操作作相应说明,如果前面环境还没有配置好,可以翻看我之前的博客,欢迎留言交流

节点hadoop01

 

 节点hadoop02

 

节点hadoop03

1 maven依赖

<dependency>   <groupId>org.apache.hbase</groupId>   <artifactId>hbase-server</artifactId>   <version>2.3.5</version>  </dependency>  <dependency>   <groupId>org.apache.hbase</groupId>   <artifactId>hbase-client</artifactId>   <version>2.3.5</version>  </dependency>  <dependency>   <groupId>jdk.tools</groupId>   <artifactId>jdk.tools</artifactId>   <version>1.8</version>   <scope>system</scope>   <systemPath>C:/Program Files/Java/jdk1.8.0_261/lib/tools.jar</systemPath>  </dependency>

2 API操作

package com.hbase;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.stringtemplate.v4.ST;import scala.util.control.Exception;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ConfigulationHBase { private static Configuration configuration; private static Connection connection; private static Admin admin; static {  configuration = HBaseConfiguration.create();  configuration.set("hbase.zookeeper.quorum", "192.168.161.141");  configuration.set("hbase.zookeeper.property.clientPort", "2181");  try {   connection = ConnectionFactory.createConnection(configuration);   admin = connection.getAdmin();  } catch (IOException e) {   e.printStackTrace();  } } /**  * 判斷表是否存在  * <br>存在則返回true  * @param tableName  * @return  * @throws IOException  */ public static boolean isTableExist(String tableName) throws IOException {  boolean b = admin.tableExists(TableName.valueOf(tableName));  return b; } /**  * 創建表  *  * 参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。<br>  * 要求当HBase已经存在名为tableName的表时,先删除原有的表,然后再<br>  * 创建新的表 field:列族<br>  * @param tableName 表名  * @param fields 列族名  * @throws IOException  */ public static void createTable(String tableName,String[] fields) throws IOException {  if(isTableExist(tableName)){   System.out.println(tableName + " table is alreadly exist...");   admin.disableTable(TableName.valueOf(tableName));   admin.deleteTable(TableName.valueOf(tableName));   System.out.println(tableName + " table is deleted...");  }  HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));  for(String str:fields){   HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);   hTableDescriptor.addFamily(hColumnDescriptor);  }  admin.createTable(hTableDescriptor);  System.out.println(tableName + " table is created!");  admin.close(); } /**  * 添加数据  *  * 向表tableName,行键rowKey和fields字段指定的单元格中添加对应的值values<br>  * 例如:表名:student,行键:1001,添加的字段:info:name,添加的值:Janna<br>  * put 'student','1001','info:name','Janna'<br>  *  * @param tableName 表名  * @param rowKey 行键  * @param family 列族  * @param qualifier 列族值  * @param value  值  * @throws IOException  */ public static void addRow(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {  Table table = connection.getTable(TableName.valueOf(tableName));  byte[] rowKeyAsBytes = rowKey.getBytes();  Put put = new Put(rowKeyAsBytes);  put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());  table.put(put);  table.close();  admin.close(); } /**  * 删除一行或者多行数据  * @param tableName  * @param rows  * @throws IOException  */ public static void deleteMultiRow(String tableName, String... rows) throws IOException {  Table table = connection.getTable(TableName.valueOf(tableName));  List<Delete> deleteList = new ArrayList<Delete>();  for (int i = 0; i < rows.length; i++) {   Delete delete = new Delete(rows[i].getBytes());   deleteList.add(delete);  }  table.delete(deleteList);  table.close();  admin.close(); } /**  *  * 查询某tableName所有的数据  * @param tableName 表名  * @throws IOException  */ public static void getAllRows(String tableName) throws IOException {  Table table = connection.getTable(TableName.valueOf(tableName));  Scan scan = new Scan();  ResultScanner scanner = table.getScanner(scan);  for (Result result:scanner){   Cell[] cells = result.rawCells();   for (Cell cell:cells){    System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));    System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));    System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));    System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));    System.out.println("--------------------------------------");   }   System.out.println("=======================================");  }  table.close(); } /**  * 查询tableName表的rowKey行键的数据  * get 'student','1001'  * @param tableName 表名  * @param rowKey 行键  */ public static void getRow(String tableName, String rowKey) throws IOException {  Table table = connection.getTable(TableName.valueOf(tableName));  Get get = new Get(rowKey.getBytes());  Result result = table.get(get);  for (Cell cell:result.rawCells()){   System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)));   System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell)));   System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));   System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));   System.out.println("--------------------------------------");  } } /**  * 获取某表某行键某列族的值  *  * @p......

原文转载:http://www.shaoqun.com/a/873492.html

跨境电商:https://www.ikjzd.com/

执御:https://www.ikjzd.com/w/717.html

急速:https://www.ikjzd.com/w/1861

递四方:https://www.ikjzd.com/w/1066


在此之前要配置好三节点的hadoop集群,zookeeper集群,并启动它们,然后再配置好HBase环境本文只是HBase2.3.5API操作作相应说明,如果前面环境还没有配置好,可以翻看我之前的博客,欢迎留言交流节点hadoop01节点hadoop02节点hadoop031maven依赖<dependency><groupId>org.apache.hbase</g
盘县哒啦仙谷在哪里,怎么走?:http://www.30bags.com/a/230920.html
磐安龙溪漂流在哪里,怎么走?:http://www.30bags.com/a/380326.html
盼了7年,这条"最美"天路终于开通!一起相约西藏吧!:http://www.30bags.com/a/240464.html
盼了7年,中国最美天路开通!霸占西藏80%美景,此生不去心难安:http://www.30bags.com/a/240466.html
男催乳师打开我双腿 医生轻点揉我快受不了了:http://lady.shaoqun.com/a/248309.html
宝宝我们换个姿势继续 一个吃奶两个㖭下面3pp:http://lady.shaoqun.com/m/a/248203.html
二个人一上一下进出 把女朋友做到下不了床:http://lady.shaoqun.com/m/a/248082.html
宝宝我们换个姿势继续 一个吃奶两个㖭下面3pp:http://www.30bags.com/m/a/249883.html
男的半夜去公园让女网友"下地",又抢了钱:http://lady.shaoqun.com/a/418108.html
不注意这点,订单再多也是白干!原来跨境大卖们在用它……:https://www.ikjzd.com/articles/146592
女人偷偷和同事在野外约会"亲密"老公用卫生纸抓奸(图):http://lady.shaoqun.com/a/418109.html
90%的男生阅历很深,女生有那么多"随便"的意思?:http://lady.shaoqun.com/a/419109.html

没有评论:

发表评论