hbase 環境搭建:單機版、遠程訪問

1. 環境

2. 安裝及測試

  • 將相關文件解壓至相關目錄,以   /opt/apache/爲例
  • 配置 hbase-2.2.2/conf/hbase-env.sh, 
export JAVA_HOME=/usr/java/jdk1.8.0_161  (jdk路徑)
  • 配置 /hbase-2.2.2/conf/hbase-site.xml,
<configuration>
	
	<property>  
		<name>hbase.zookeeper.quorum</name>  
        <!-- 增加統計支持  更改爲服務器計算機名字  -->
		<value>ubuntu</value>  
	</property> 
	<property>  
		<name>hbase.rootdir</name>  
        <!--自定義路徑 -->
		<value>file:///data/apache/hbase/root</value>  
	</property>  
	<property>  
		<name>hbase.tmp.dir</name>  
        <!--自定義路徑 -->
		<value>/data/apache/hbase/tmp</value>  
	</property>  
 
	<property>  
		<name>hbase.zookeeper.property.dataDir</name> 
        <!--自定義路徑 --> 
		<value>/data/apache/hbase/zoo</value>  
	</property>  
	<property>
		<name>hbase.unsafe.stream.capability.enforce</name>
		<value>false</value>

	</property>

</configuration>
  • 配置 host ,此步驟影響遠程訪問 。linux (vi /etc/hosts)

             添加當前計算機ip 名字,比如    192.168.3.1   ubuntu

  • 啓動及測試

              (1)    命令行運行    hbase-2.2.2/bin/start-hbase.sh

                             如果出現  running master, logging to /opt/apache/hbase-2.2.2/bin/../logs/hbase-devin-master-ubxxxxxxxxxx

              (2)  且使用 jps 命令查看 有  HMaster 進程 ,則表示成功,入下圖

             

              (3)  網頁  http://192.168.3.31:16010/master-status   (更改爲自己ip或者主機地址)

  • 服務器本地 shell 測試

         (1) 打開工具   hbase-2.2.2/bin/hbase  shell ,如下圖

              

      (2) 測試是否通 , 輸入命令 list  

           

    (3) 也可以測試建表等命令,比如 

           create 'spark_test','name','sex'

3. 遠程連接

  • 如果windows , 配置環境遍歷 HADOOP_HOME, 指向  之前準備的hadoop_commen 地址
  • 配置本地host ,  同服務器配置,將服務器ip地址配置爲相應的名字。比如  

             

  • 關閉局域網等
  • 測試是否可通: telnet 192.xxx  2181
  • 編碼,pom文件如下
  • <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.11.12</scala.version>
        <scala.compat.version>2.11</scala.compat.version>
        <hadoop.version>3.2.1</hadoop.version>
        <spark.version>2.4.3</spark.version>
        <hbase.version>2.2.2</hbase.version>
      </properties>
    
    <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-client</artifactId>
          <version>${hadoop.version}</version>
          <exclusions>
            <exclusion>
              <artifactId>commons-httpclient</artifactId>
              <groupId>commons-httpclient</groupId>
            </exclusion>
            <exclusion>
              <artifactId>httpcore</artifactId>
              <groupId>org.apache.httpcomponents</groupId>
            </exclusion>
            <exclusion>
              <artifactId>hadoop-common</artifactId>
              <groupId>org.apache.hadoop</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-client</artifactId>
          <version>${hbase.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-common</artifactId>
          <version>${hbase.version}</version>
        </dependency>
        <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-server</artifactId>
          <version>${hbase.version}</version>
        </dependency>

     

  • 部分測試代碼
   val tableName = "spark_test"
    val columnFamilys = List("a", "b", "c")
    val conf = HBaseConfiguration.create()
    conf.set("hbase.zookeeper.quorum","ubuntu")
    conf.set("hbase.zookeeper.property.clientPort", "2181")
    val hbaseconn = ConnectionFactory.createConnection(conf)
    val admin:Admin = hbaseconn.getAdmin()
    val myTableName :TableName = TableName.valueOf(tableName)

    if (admin.tableExists(myTableName)) {
      println(tableName +" Table exists!")
      //val tableDesc: HTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename))
      //tableDesc.addCoprocessor("org.apache.hadoop.hbase.coprocessor.AggregateImplementation")
    }else {
      // 表描述器構造器
      println(tableName +" Table not exists!")
      val tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName))
      if(null != columnFamilys)
        for (columnFamily <- columnFamilys) {
          //列族描述起構造器//列族描述起構造器
          val cdb = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily))
          //獲得列描述起
          val cfd: ColumnFamilyDescriptor = cdb.build
          //添加列族
          tdb.setColumnFamily(cfd)
        }
      // 獲得表描述器
      val td = tdb.build
      admin.createTable(td)
      println("create successful!! ")

    }
    admin.close
  •  

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章