第5章的代码-林子雨编著《大数据基础编程、实验和案例教程(第3版)》教材

大数据学习路线图

林子雨编著《大数据基础编程、实验和案例教程(第3版)》(教材官网)教材中的命令行和代码,在纸质教材中的印刷效果不是很好,可能会影响读者对命令行和代码的理解,为了方便读者正确理解命令行和代码或者直接拷贝命令行和代码用于上机实验,这里提供全书配套的所有命令行和代码。
查看教材所有章节的代码

第5章 HBase的安装和基础编程

教材第68页

(温馨提示:代码框上方的复制代码按钮,也就是“两张A4纸图标”,用鼠标点击复制代码按钮,就可以把代码框中的代码复制到粘贴板,粘贴到其他地方。但是,有的浏览器可能不支持该功能)

sudo  tar  -zxf  ~/Downloads/hbase-2.5.4-bin.tar.gz  -C  /usr/local
cd /usr/local
sudo  mv  hbase-2.5.4  hbase

教材第69页

vim ~/.bashrc
export PATH=$PATH:/usr/local/hadoop/sbin:/usr/local/hadoop/bin
export PATH=$PATH:/usr/local/hadoop/sbin:/usr/local/hadoop/bin: /usr/local/hbase/bin
source ~/.bashrc
cd  /usr/local
sudo  chown  -R  hadoop  ./hbase
/usr/local/hbase/bin/hbase version

教材第70页

vim /usr/local/hbase/conf/hbase-env.sh
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_171
export HBASE_MANAGES_ZK=true

教材第70页

vim /usr/local/hbase/conf/hbase-site.xml

教材第71页

<configuration>
        <property>
                <name>hbase.rootdir</name>
                <value>file:///usr/local/hbase/hbase-tmp</value>
        </property>
</configuration>
cd /usr/local/hbase
bin/start-hbase.sh  #启动HBase
bin/hbase shell  #进入HBase shell命令行模式
bin/stop-hbase.sh
vim /usr/local/hbase/conf/hbase-env.sh

教材第72页

export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_171
export HBASE_CLASSPATH=/usr/local/hbase/conf
export HBASE_MANAGES_ZK=true
vim /usr/local/hbase/conf/hbase-site.xml
<configuration>
        <property>
                <name>hbase.rootdir</name>
                <value>hdfs://localhost:9000/hbase</value>
        </property>
        <property>
                <name>hbase.cluster.distributed</name>
                <value>true</value>
        </property>
        <property>
                <name>hbase.unsafe.stream.capability.enforce</name>
                <value>false</value>
        </property>
</configuration>

教材第73页

ssh localhost
cd /usr/local/hadoop
./sbin/start-dfs.sh
cd /usr/local/hbase
bin/start-hbase.sh
bin/hbase shell  #进入HBase shell命令行模式
bin/stop-hbase.sh
cd /usr/local/hadoop
./sbin/stop-dfs.sh

教材第74页

sudo  tar  -zxvf  ~/Downloads/hbase-2.5.4-bin.tar.gz  -C  /usr/local
cd /usr/local
sudo  mv  hbase-2.5.4  hbase
cd  /usr/local
sudo  chown  -R  hadoop  ./hbase
vim /usr/local/hbase/conf/hbase-env.sh
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_371
export HBASE_CLASSPATH=/usr/local/hbase/conf
export HBASE_MANAGES_ZK=true
export HBASE_DISABLE_HADOOP_CLASSPATH_LOOKUP=true
vim ~/.bashrc

教材第75页

export HBASE_HOME=/usr/local/hbase
source ~/.bashrc
vim /usr/local/hbase/conf/hbase-site.xml
<configuration>
  <property>
    <!-- Hbase Web UI 登录 -->
    <name>hbase.master.info.port</name>
    <value>60010</value>
  </property>
  <property>
    <name>hbase.tmp.dir</name>
    <value>/usr/local/hbase/tmp</value>
  </property>
  <property>
    <name>hbase.unsafe.stream.capability.enforce</name>
    <value>false</value>
  </property>
  <property>
    <!-- Hbase写入数据的目录 -->
    <name>hbase.rootdir</name>
    <value>hdfs://hadoop01:9000/hbase </value>
  </property>
  <property>
    <name>hbase.cluster.distributed</name>
    <value>true</value>
  </property>
  <property> 
    <!-- list of zookeeper -->
  <name>hbase.zookeeper.quorum</name>
   <value>hadoop01,hadoop02,hadoop03</value> 
 </property>     
  <property>
    <!--zookooper配置、日志等的存储位置 -->
  <name>hbase.zookeeper.property.dataDir</name>
  <value>/usr/local/hbase/zookeeper</value>
 </property>
</configuration>

教材第76页

vim /usr/local/hbase/conf/regionservers
hadoop01
hadoop02
hadoop03
cd  /usr/local/
tar  -zcf  ~/hbase.master.tar.gz  ./hbase
cd  ~
scp  ./hbase.master.tar.gz  hadoop02:/home/hadoop
scp  ./hbase.master.tar.gz  hadoop03:/home/hadoop
cd  ~
sudo  rm  -rf  /usr/local/hbase/
sudo  tar  -zxf  ~/hbase.master.tar.gz  -C  /usr/local
sudo  chown  -R  hadoop  /usr/local/hbase
cd /usr/local/hadoop
./sbin/start-all.sh
cd /usr/local/hbase
./bin/start-hbase.sh

教材第77页

bin/hbase shell  #进入HBase shell命令行模式
hbase> exit
bin/stop-hbase.sh
cd /usr/local/hadoop
./sbin/stop-dfs.sh
cd /usr/local/hadoop
./sbin/start-dfs.sh
cd /usr/local/hbase
./bin/start-hbase.sh
./bin/hbase shell

教材第78页

hbase> create 'student','Sname','Ssex','Sage','Sdept','course'
hbase> list
hbase> put 'student','95001','Sname','LiYing'
hbase> put 'student','95001','Ssex','male'
hbase> put 'student','95001','Sage','22'
hbase> put 'student','95001','Sdept','CS'
hbase> put 'student','95001','course:math','80'
hbase> get 'student','95001'

教材第79页

hbase> scan 'student'
hbase > delete 'student','95001','Ssex'
hbase> deleteall 'student','95001'

教材第80页

hbase> disable 'student'
hbase> drop 'student'
hbase> create 'teacher',{NAME=>'username',VERSIONS=>5}
hbase> put 'teacher','91001','username','Mary'
hbase> put 'teacher','91001','username','Mary1'
hbase> put 'teacher','91001','username','Mary2'
hbase> put 'teacher','91001','username','Mary3'
hbase> put 'teacher','91001','username','Mary4'
hbase> put 'teacher','91001','username','Mary5'
hbase> get 'teacher','91001',{COLUMN=>'username',VERSIONS=>5}
hbase> get 'teacher','91001',{COLUMN=>'username',VERSIONS=>3}

教材第81页

hbase> exit
bin/stop-hbase.sh
cd /usr/local/hadoop
./sbin/start-dfs.sh
cd /usr/local/hbase
./bin/start-hbase.sh

教材第86页

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.IOException;
public class ExampleForHBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void main(String[] args)throws IOException{
        init();
        createTable("student",new String[]{"score"});
        insertData("student","zhangsan","score","English","69");
        insertData("student","zhangsan","score","Math","86");
        insertData("student","zhangsan","score","Computer","77");
        getData("student", "zhangsan", "score","English");
        close();
    }

    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void createTable(String myTableName,String[] colFamily) throws IOException {
        TableName tableName = TableName.valueOf(myTableName);
        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
            for(String str:colFamily){
                ColumnFamilyDescriptor family = 
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                tableDescriptor.setColumnFamily(family);
            }
            admin.createTable(tableDescriptor.build());
        } 
    }

    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { 
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(),col.getBytes(), val.getBytes());
        table.put(put);
        table.close(); 
    }

    public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException{ 
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        table.close(); 
    }
}

教材第88页

cd /usr/local/hbase
./bin/hbase shell
hbase> list
hbase> scan ‘student’