HBASE學習d端口master:16010(java操作hbase)https://www.cnblogs.com/junrong624/p/7323483.html


HBase提示已創建表,但是list查詢時,卻顯示表不存在。

https://blog.csdn.net/liu16659/article/details/80216085

下載網址

http://archive.apache.org/dist/hbase/

下載1.2.6bin.tar.gz

安裝查看網址

https://www.cnblogs.com/lzxlfly/p/7221890.html

關於hbase關系圖的理解
 https://blog.csdn.net/a519781181/article/details/79423512 

Hbase安裝部署基本步驟:

  1、在Hadoop配置的基礎上,配置環境變量HBASE_HOME、hbase-env.sh

    編輯 vim /etc/profile  加入 

     

    export HBASE_HOME=/home/hadoop/hadoop_home/hbase-1.2.6
    export PATH=$PATH:$HBASE_HOME/bin

source ~/.profile

復制一下需要修改的內容

 

cp /home/hadoop/hadoop_home/hbase-1.2.6/conf/hbase-env.sh /home/hadoop/hadoop_home/hbase-1.2.6/conf/hbase-env.sh.bak

cp /home/hadoop/hadoop_home/hbase-1.2.6/conf/hbase-site.xml  /home/hadoop/hadoop_home/hbase-1.2.6/conf/hbase-site.xml.bak

cp /home/hadoop/hadoop_home/hbase-1.2.6/conf/regionservers  /home/hadoop/hadoop_home/hbase-1.2.6/conf/regionservers.bak

    編輯vim /home/hadoop/hadoop_home/hbase-1.2.6/conf/hbase-env.sh  加入

      export JAVA_HOME=/home/hadoop/hadoop_home/jdk1.8.0_181(jdk安裝路徑)

    去掉注釋 # export  HBASE_MANAGES_ZK=true,不使用hbase自帶zookeeper。

去掉注釋 # export HBASE_MANAGES_ZK=true,。這里我們注釋
或者改成false

HBase 需要依賴的 Jar 包

由於 HBase 需要依賴Hadoop,所以替換 HBase 的 lib 目錄下的 jar 包,以解決兼容問題:

1) 刪除原有的 jar:

$ rm -rf /home/admin/modules/hbase-1.2.6/lib/hadoop-*
$ rm -rf /home/admin/modules/hbase-1.2.6/lib/zookeeper-3.4.6.jar

2) 拷貝新 jar,涉及的 jar 有:
hadoop-annotations-2.7.2.jar
hadoop-auth-2.7.2.jar
hadoop-client-2.7.2.jar
hadoop-common-2.7.2.jar
hadoop-hdfs-2.7.2.jar
hadoop-mapreduce-client-app-2.7.2.jar
hadoop-mapreduce-client-common-2.7.2.jar
hadoop-mapreduce-client-core-2.7.2.jar
hadoop-mapreduce-client-hs-2.7.2.jar
hadoop-mapreduce-client-hs-plugins-2.7.2.jar
hadoop-mapreduce-client-jobclient-2.7.2.jar
hadoop-mapreduce-client-jobclient-2.7.2-tests.jar
hadoop-mapreduce-client-shuffle-2.7.2.jar
hadoop-yarn-api-2.7.2.jar
hadoop-yarn-applications-distributedshell-2.7.2.jar
hadoop-yarn-applications-unmanaged-am-launcher-2.7.2.jar
hadoop-yarn-client-2.7.2.jar
hadoop-yarn-common-2.7.2.jar
hadoop-yarn-server-applicationhistoryservice-2.7.2.jar
hadoop-yarn-server-common-2.7.2.jar
hadoop-yarn-server-nodemanager-2.7.2.jar
hadoop-yarn-server-resourcemanager-2.7.2.jar
hadoop-yarn-server-tests-2.7.2.jar

hadoop-yarn-server-web-proxy-2.7.2.jar
zookeeper-3.4.9.jar

 

尖叫提示:這些 jar 包的對應版本應替換成你目前使用的 hadoop 版本,具體情況具體分析。
查找 jar 包舉例

$ find /home/admin/modules/hadoop-2.7.2/ -name hadoop-annotations*

然后將找到的 jar 包復制到 HBase 的 lib 目錄下即可。

HBase 軟連接 Hadoop 配置

$ ln -s ~/hadoop_home/etc/hadoop/core-site.xml ~/hadoop_home/hbase-1.2.6/conf/core-site.xml
$ ln -s ~/hadoop_home/etc/hadoop/hdfs-site.xml ~/hadoop_home/hbase-1.2.6/conf/hdfs-site.xml

 

    2、配置hbase-site.xml文件 

    <configuration>

      <property> 
        <name>hbase.rootdir</name> <!-- hbase存放數據目錄 -->
        <value>hdfs://master:9000/user/hadoop/hbase/hbase_db</value>

          <!-- 端口要和Hadoop的fs.defaultFS端口一致-->
      </property> 
      <property> 
        <name>hbase.cluster.distributed</name> <!-- 是否分布式部署 -->
        <value>true</value> 
      </property> 

      <!-- 0.98 后的新變動,之前版本沒有.port,默認端口為 60000 -->
      <property>
          <name>hbase.master.port</name>
          <value>16000</value>
      </property>

      <property> 
        <name>hbase.zookeeper.quorum</name> <!-- list of  zookooper -->
        <value>master:2181,node1:2181,node2:2181</value> 
      </property>     

       <property><!--zookooper配置、日志等的存儲位置 -->
          <name>hbase.zookeeper.property.dataDir</name> 
          <value>/home/hadoop/hadoop_home/hbase-1.2.6/hbase/zookeeper</value>
       </property>

    </configuration>

  3、配置regionservers

    編輯 vim /opt/hbase-1.2.6/conf/regionservers   去掉默認的localhost,
     加入node1、node2,(換行)保存退出 

     然后把在master上配置好的hbase,通過遠程復制命令

     scp -r /home/hadoop/hadoop_home/hbase-1.2.6/  node1:/home/hadoop/hadoop_home/

    scp -r ~/.profile node1:/home/hadoop/.profile

然后在node1下 

source ~/.profile

    scp -r /home/hadoop/hadoop_home/hbase-1.2.6/  node2:/home/hadoop/hadoop_home/

scp -r ~/.profile node2:/home/hadoop/.profile

然后在node2下 

source ~/.profile

     復制到node1、node對應的位置

  4、啟動與停止Hbase

     (1)在Hadoop已經啟動成功的基礎上,輸入start-hbase.sh,過幾秒鍾便啟動完成,

      輸入jps命令查看進程是否啟動成功,若 master上出現HMaster、HQuormPeer,

      slave上出現HRegionServer、HQuorumPeer,就是啟動成功了。

      (2)輸入hbase shell 命令 進入hbase命令模式

          輸入status命令可以看到如下內容,1個master,2 servers,3機器全部成功啟動。

          1 active master, 0 backup masters, 2 servers, 0 dead, 2.0000 average load

    (3)接下來配置本地hosts,(前邊配置過的無需再配置了)

       編輯 C:\Windows\System32\drivers\etc的hosts文件,加入

        192.168.0.235   master

        192.168.0.225   node1

        192.168.0.215   node2

      在瀏覽器中輸入http://master:16010就可以在界面上看到hbase的配置了

 

 

    (4)當要停止hbase時輸入stop-hbase.sh,過幾秒后hbase就會被停止了。

輸入

hbase shell

進入hbase界面

 

尖叫提示: 如果集群之間的節點時間不同步,會導致 regionserver 無法啟動,拋出
ClockOutOfSyncException 異常。
修復提示:
a、同步時間服務 請參看幫助文檔:《大數據幫助文檔 1.0》重啟即可
b、屬性:hbase.master.maxclockskew 設置更大的值

<property>

  <name>hbase.master.maxclockskew</name>
  <value>180000</value>
  <description>Time difference of regionserver from master</description>
</property>

 

創建表的例子

例如

create 'test','data'  建表

list  查看表

put 'test','row1','data:1','value1'  插入數據

put 'test','row2','data:2','value2'

put 'test','row3','data:3','value3'

然后查看表內容

scan 'test'

 

 disable 'test'  移除表之前要設為禁用

drop 'test'  移除表

 

 

2.4、JavaAPI(或者新建空java工程,把hbase/lib下面所有包全部導出去放到java工程下lib包里,不需要maven)
2.4.1、安裝 Maven 並配置環境變量

 $ tar -zxf ~/softwares/installations/apache-maven-3.5.0-bin.tar.gz -C ~/modules/

在環境變量中添加:

MAVEN_HOME=/home/admin/modules/apache-maven-3.5.0
export PATH=$PATH:$MAVEN_HOME/bin

2.4.2、新建 Maven Project
新建項目后在 pom.xml 中添加依賴:

<dependency>

  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-server</artifactId>
  <version>1.2.6</version>
</dependency>
<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>1.2.6</version>
</dependency>
<dependency>
  <groupId>jdk.tools</groupId>
  <artifactId>jdk.tools</artifactId>
  <version>1.8</version>
  <scope>system</scope>
  <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>

 

 

 

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 java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

  //1) 首先需要獲取 Configuration 對象:
  public static Configuration conf;
  static {
    //使用 HBaseConfiguration 的單例方法實例化
    conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "master,node1,node2");
    conf.set("hbase.zookeeper.property.clientPort", "2181");
    conf.set("hbase.master", "master:60000");
  }

  public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
    HBaseAdmin admin = new HBaseAdmin(conf);
    if (isTableExist(tableName)) {
      admin.disableTable(tableName);
      admin.deleteTable(tableName);
      System.out.println("表" + tableName + "刪除成功!");
    } else {
      System.out.println("表" + tableName + "不存在!");
    }
  }

  //向表中插入數據

  public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
    //創建 HTable 對象
    HTable hTable = new HTable(conf, tableName);
    //向表中插入數據
    Put put = new Put(Bytes.toBytes(rowKey));
    //向 Put 對象中組裝數據
    put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
    hTable.put(put);
    hTable.close();
    System.out.println("插入數據成功");
  }

  //刪除多行數據

  public static void deleteMultiRow(String tableName, String... rows) throws IOException {
    HTable hTable = new HTable(conf, tableName);
    List<Delete> deleteList = new ArrayList<Delete>();
    for (String row : rows) {
      Delete delete = new Delete(Bytes.toBytes(row));
      deleteList.add(delete);
    }

    hTable.delete(deleteList);
    hTable.close();
  }

  //得到所有數據

  public static void getAllRows(String tableName) throws IOException {
    HTable hTable = new HTable(conf, tableName);
    //得到用於掃描 region 的對象
    Scan scan = new Scan();
    //使用 HTable 得到 resultcanner 實現類的對象
    ResultScanner resultScanner = hTable.getScanner(scan);
    for (Result result : resultScanner) {
      Cell[] cells = result.rawCells();
      for (Cell cell : cells) {
        //得到 rowkey
        System.out.println("行鍵:" + Bytes.toString(CellUtil.cloneRow(cell)));
        //得到列族
        System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
        System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
        System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
      }
    }
  }

  //得到某一行

  public static void getRow(String tableName, String rowKey) throws IOException {
    HTable table = new HTable(conf, tableName);
    Get get = new Get(Bytes.toBytes(rowKey));
    //get.setMaxVersions();顯示所有版本
    //get.setTimeStamp();顯示指定時間戳的版本
    Result result = table.get(get);
    for (Cell cell : result.rawCells()) {
      System.out.println("行鍵:" + Bytes.toString(result.getRow()));
      System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
      System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
      System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
      System.out.println("時間戳:" + cell.getTimestamp());
    }
  }

  //獲取某一行指定“列族:列”的數據

  public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {
    HTable table = new HTable(conf, tableName);
    Get get = new Get(Bytes.toBytes(rowKey));
    get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
    Result result = table.get(get);
    for (Cell cell : result.rawCells()) {
      System.out.println("行鍵:" + Bytes.toString(result.getRow()));
      System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
      System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
      System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
    }
  }

  //主程序入口

  public static void main(String args[]) throws IOException {
    //System.out.println(isTableExist("student"));
    //createTable("student2",new String[]{"info1","info2"});
    //dropTable("student2");
    //addRowData("student","1003","info","name","zhangy");
    //deleteMultiRow("student",new String[]{"1002","1003"});
    //getAllRows("student");
    //getRow("student","1002");
    getRowQualifier("student","1001","info","name");
  }

  //判斷表是否存在

  public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
    //在 HBase 中管理、訪問表需要先創建 HBaseAdmin 對象
    Connection connection = ConnectionFactory.createConnection(conf);

    //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
    HBaseAdmin admin = new HBaseAdmin(conf);
    return admin.tableExists(tableName);
  }

  //創建表

  public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
    HBaseAdmin admin;
    admin = new HBaseAdmin(conf);
    //判斷表是否存在
    if (isTableExist(tableName)) {
      System.out.println("表" + tableName + "已存在");
      System.exit(0);
    } else {
      //創建表屬性對象,表名需要轉字節
      HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
      //創建多個列族
      for (String cf : columnFamily) {
        descriptor.addFamily(new HColumnDescriptor(cf));
      }
      //根據對表的配置,創建表
      admin.createTable(descriptor);
    System.out.println("表" + tableName + "創建成功!");
    }
  }
}

 

 

配置SSH所有機器之間免密碼登錄

  (1)CentOS默認沒有啟動ssh無密登錄,編輯 vim  /etc/ssh/sshd_config,

      去掉以下兩行注釋,開啟Authentication免登陸。

      #RSAAuthentication yes
           #PubkeyAuthentication yes

           如果是root用戶下進行操作,還要去掉 #PermitRootLogin yes注釋,允許root用戶登錄。

  (2)輸入命令,ssh-keygen -t rsa,生成key,一直按回車,

      就會在/root/.ssh生成:authorized_keys   id_rsa.pub   id_rsa 三個文件,

     這里要說的是,為了各個機器之間的免登陸,在每一台機器上都要進行此操作。

  (3) 接下來,在master服務器,合並公鑰到authorized_keys文件,

     進入/root/.ssh目錄,輸入以下命令

          cat id_rsa.pub>> authorized_keys    把master公鑰合並到authorized_keys 中

       ssh root@192.168.172.72 cat ~/.ssh/id_rsa.pub>> authorized_keys    

       ssh root@192.168.172.73 cat ~/.ssh/id_rsa.pub>> authorized_keys    

       把slave1、slave2公鑰合並到authorized_keys 中

                完成之后輸入命令,把authorized_keys遠程copy到slave1和slave2之中

      scp authorized_keys 192.168.172.72:/root/.ssh/  

         scp authorized_keys 192.168.172.73:/root/.ssh/

      最好在每台機器上進行chmod 600  authorized_keys操作,

       使當前用戶具有 authorized_keys的讀寫權限。

      拷貝完成后,在每台機器上進行 service sshd restart  操作, 重新啟動ssh服務。

      之后在每台機器輸入 ssh 192.168.172.xx,測試能否無需輸入密碼連接另外兩台機器。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM