Hbase installation, Hbase shell and test code

Description: I is vm built four nodes services are node01, node02, node03, node04; node02, node03, the node04 respectively mounted zk, node01 is hadoop master node, node02, node03, node04 is hadoop of child nodes. I am the hbase installed on node04 is hmaster, node03 to prepare hmater, node01, node02, node03 is HregionServer
Note: The time on each node to be consistent, otherwise the problem will appear in the written hbase time
synchronization time mode, on Baidu there are other ways
with ntpdate from time to time to update the server
if the system is not linux ntpdate this command, you can enter the following code to install
yum install ntp
after the installation finished, you do not do what configuration and does not require a direct test
vi / etc / ntp .conf
the Add below:
Server 1.cn.pool.ntp.org
Server 3.asia.pool.ntp.org
Server 2.asia.pool.ntp.org
the restart-Service The Time Sync and
[the root @ localhost ~] # the ntpdate Time .nist.gov
22 is the ntpdate on Oct 21:11:43 [5014]: ADJUST offset -0.018788 sec Time Server 207.200.81.113
If the content of the above described synchronization is successful, then add in the following crontab inside.
/ 10 * # ntpdate time.nist.gov domain name or IP
every ten minutes Synchronization once. Recommend several time server.
time.nist.gov
time.nuri.net
0.asia.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org
3.asia.pool.ntp.org
1. Upload hbase and extract the installation package, configure the environment variables
modified hbase_env.sh under $ HBASE_HOME / conf JAVA_HOME path and switched off in the own Hbase obtained ZooKeeper (= Export HBASE_MANAGES_ZK to false)
Hbase installation, Hbase shell and test code
Hbase installation, Hbase shell and test code
2. modify hbase-site.xm under $ HBASE_HOME / conf file
<Property>,
<name> hbase.rootdir </ name>
<- - mycluster is hadoop inside hdfs-site.xml configuration was in service names!>
<value> HDFS: // mycluster / HBase </ value>
</ Property>
<Property>
<name> hbase.cluster.distributed </ name>
<value> to true </ value>
</ Property>
<
<name> hbase.zookeeper.quorum </ name>
<value> hadoop2, hadoop3, hadoop4 </ value>
</ Property>
3. Modify the file regionserverse $ HBASE_HOME / conf, arranged RegionServer
amdha01
amdha02
node03
4. Modify $ HBASE_HOME backup-masters (standby node) under / conf Lane configuration:
node03
completing the above steps have the following hadoop hdfs-site.xml copied to HBase / conf under

The configured on node04 hbase package distribution to node01, node02, pay attention to configure the environment variables on node03. If node01, node02, no hadoop under node03 hbase / conf path hdfs-site.xml not start up at boot time.
Hadoop first start up front. Performed at the node04 $ HBASE_HOME / bin ./start-hbase.sh
Hbase installation, Hbase shell and test code
Hbase installation, Hbase shell and test code
Hbase installation, Hbase shell and test code
Hbase installation, Hbase shell and test code
Hbase installation, Hbase shell and test code
here I did not use the default port configuration port page the default port 16010 in hbase-site.xml in the background the default port 16000. higher version supports port
<! - - the new changes after the 0.98, the previous version did not .port, the default port is 60000 ->
<Property>
<name> hbase.master.port </ name>
<value> 16000 </ value>
</ Property>
hbase.master .maxclockskew setting a larger value
<Property>
<name> hbase.master.maxclockskew </ name>
<value> 180000 </ value>
<Description> Time -difference of RegionServer from Master </ Description>
</ Property>

每个节点都可以用命令开启运行hbase,切换到$HBASE_HOM/bin下 命令:hbase shell
Hbase installation, Hbase shell and test code
查看帮助命令:
hbase(main):001:0> help

查看表命令:
hbase(main):002:0> list

查看namespace命令:(namespace可以理解为数据库)
hbase(main):003:0> list_namespace

创建一个新的namespace:
hbase(main):007:0> create_namespace 'test_ns'

在test_ns下创建表:
hbase(main):009:0> create 'test_ns:test_table','info' //创建表要指定表面和列族
如果不指定namespace的话创建的表默认在default 的namespace中

查看namespace的描述:
hbase(main):013:0> describe_namespace 'test_ns'

修改namespace的名称:
hbase(main):015:0> alter_namespace 'test_ns',{METHOD => 'set', 'name' => 'test_ns2'}
修改前查看:
Hbase installation, Hbase shell and test code
修改后查看:
Hbase installation, Hbase shell and test code
删除namespace的name:
hbase(main):018:0> alter_namespace 'test_ns',{METHOD => 'unset',NAME => 'name'}
Hbase installation, Hbase shell and test code
删除表:
hbase(main):022:0> disable 'test_ns:test_table'
hbase(main):023:0> drop 'test_ns:test_table'
Hbase installation, Hbase shell and test code
删除namespace:
hbase(main):025:0> drop_namespace 'test_ns' //注意如果namespace里有表,要先删表再删namespace,否则直接删namespace报错
Hbase installation, Hbase shell and test code
创建分片表:
hbase(main):013:0> create 'test_ns:teacher','info',SPLITS => ['2','4','6']
Hbase installation, Hbase shell and test code
往表里插入数据:
说明: 1 是指rowkey, name,sex,age是列名,hbase建议列族即info,不要多.put一次不能插入多条
hbase(main):029:0> put 'test_ns:student','1','info:name','zhangsan'
hbase(main):030:0> put 'test_ns:student','1','info:sex','male'
hbase(main):031:0> put 'test_ns:student','1','info:age','22'

查看表结构:
hbase(main):002:0> describe 'test_ns:student'
Hbase installation, Hbase shell and test code
添加一个列族:
hbase(main):009:0> alter 'test_ns:student',{NAME => 'info1'}
Hbase installation, Hbase shell and test code
删除一个列族:
hbase(main):011:0> alter 'test_ns:student',{'delete' => 'info'}
Hbase installation, Hbase shell and test code
注意:但表里只有一个列族时,不能删除,直接删除报错

扫描表:
hbase(main):008:0> scan 'test_ns:student'
Hbase installation, Hbase shell and test code
通过指定版本扫描表:
hbase(main):010:0> scan 'test_ns:student',{RAW => true, VERSIONS => 10}
Hbase installation, Hbase shell and test code
通过指定列查询数据:
hbase(main):014:0> scan 'test_ns:student',{COLUMNS => 'info:name'}
Hbase installation, Hbase shell and test code
分页查询:
hbase(main):016:0> scan 'test_ns:student',{COLUMNS => ['info:name'],LIMIT => 2, STARTROW => '1'}
Hbase installation, Hbase shell and test code
get查询:
hbase(main):017:0> get 'test_ns:student','1'
hbase(main):018:0> get 'test_ns:student','1','info:name'
Hbase installation, Hbase shell and test code
根据时间戳查询 是一个范围,包头不包尾
hbase(main):035:0> get 'test_ns:student','1', {TIMERANGE => [1574231458967, 1574234043781]}
Hbase installation, Hbase shell and test code
更新数据:
hbase(main):037:0> put 'test_ns:student','1','info:age','30'
Hbase installation, Hbase shell and test code
incr计数器
hbase(main):044:0> incr 'test_ns:student','1','info:name'

删除一列:
hbase(main):002:0> delete 'test_ns:student','1','info:age'

删除一行:
hbase(main):004:0> deleteall 'test_ns:student','1'

删除指定版本:
hbase(main):013:0> delete 'test_ns:student','3','info:name',TIMESTAMP => 1574232778235
Hbase installation, Hbase shell and test code
判断表是否存在:
hbase(main):001:0> exists 'test_ns:123'
Hbase installation, Hbase shell and test code
统计表行数:
hbase(main):002:0> count 'test_ns:student'
Hbase installation, Hbase shell and test code
表生效和失效
hbase(main):085:0> enable 'myns:user_info'
hbase(main):086:0> disable 'myns:user_info'

清空表数据:
hbase(main):003:0> truncate 'test_ns:student'
Hbase installation, Hbase shell and test code

=======代码示例=============
pom.xml的配置
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.4</version>
</dependency>

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>2.0.4</version>
    </dependency>
</dependencies>

    代码示例:
    package com.test;

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 testHbaseApi {

public static Connection connection = null;
public static Admin admin = null;

static {
    try {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node02,node03,node04");
        connection = ConnectionFactory.createConnection(conf);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//连接得到用户
public static  void getAdmin() throws IOException {
     if(admin == null){
         admin = connection.getAdmin();
     }
}

//关闭
public static void closeConn() throws IOException {
    if(admin != null) {
        admin.close();
    }
    if(connection != null){
        connection.close();
    }
}

//判断表是否存在
public static boolean isExistTable(String tableName) throws IOException {
    boolean isboolean = admin.tableExists(TableName.valueOf(tableName));
    return isboolean;
}

//创建表
public static void createTable(String tableName, String... cfs) throws IOException {
    if (tableName == null || tableName.trim().length() == 0) {
        System.out.println("tableName isn't null");
        return;
    }
    if (isExistTable(tableName)) {
        System.out.println("tableName is exist");
        return;
    }
    if (cfs.length == 0) {
        System.out.println("cloumn family isn't null");
        return;
    }
    TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    for(String c : cfs){
        ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.of(c);
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    }
    TableDescriptor build = tableDescriptorBuilder.build();
    admin.createTable(build);

    }

    //删除表
  public static void deleteTable(String tablename) throws IOException {
    if(!isExistTable(tablename)){
        System.out.println("tableName isn't exist");
        return;
    }
    admin.disableTable(TableName.valueOf(tablename));
    admin.deleteTable(TableName.valueOf(tablename));
  }

  //创建namespace
 public static void createNs(String nspace){
     NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(nspace);
     NamespaceDescriptor nsDescriptor = builder.build();
     try {
         admin.createNamespace(nsDescriptor);
     } catch (NamespaceExistException e1){
         System.out.println(nspace + " is exist");
     } catch (IOException e) {
         e.printStackTrace();
     }
 }

 //删除namespace
 public static  void deleteNs(String ns) throws IOException {
       admin.deleteNamespace(ns);
 }

 //插入数据
public static void insterData(String tablename, String rowkey, String columnFamily, String cloumnname, String value) throws IOException {
    Table table = connection.getTable(TableName.valueOf(tablename));
    Put put = new Put(Bytes.toBytes(rowkey));
    put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(cloumnname), Bytes.toBytes(value));
    table.put(put);
    table.close();
}

//获取数据
public static void getData(String tablename, String rowkey, String columnFamily, String cloumnname) throws IOException {
    Table table = connection.getTable(TableName.valueOf(tablename));
    Get get = new Get(Bytes.toBytes(rowkey));
    //获取指定的列族
   // get.addFamily(Bytes.toBytes(columnFamily));

    //获取指定的列族的列
    //get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(cloumnname));

    //获取指定的版本
    //get.readAllVersions(); //所有版本
    //get.readVersions(5);  //指定版本

    Result result = table.get(get);
    for(Cell cell : result.rawCells()){
        System.out.println("columnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
                ",cloumnname: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
                ",value: " + Bytes.toString(CellUtil.cloneValue(cell))
        );
    }
    table.close();
}

//scan扫描表数据
public static void scanData(String tablename) throws IOException {
    Table table = connection.getTable(TableName.valueOf(tablename));
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);
    for(Result r : scanner){
        System.out.println("=======" + r);
        for(Cell cell : r.rawCells()){
            System.out.println("columnFamily: " + Bytes.toString(CellUtil.cloneFamily(cell)) +
                    ",cloumnname: " + Bytes.toString(CellUtil.cloneQualifier(cell)) +
                    ",value: " + Bytes.toString(CellUtil.cloneValue(cell))
            );
        }
    }
    table.close();
}

public static void main(String[] args) throws IOException {
    getAdmin();

/ System.out.println(isExistTable("test_ns:student"));
System.out.println("==================下面创建表========================");
System.out.println(isExistTable("test_ns:teacher"));
createTable("test_ns:teacher","info","info1");
System.out.println(isExistTable("test_ns:teacher"));
System.out.println("==================下面删除表========================");
deleteTable("test_ns:teacher");
System.out.println("==================下面创建ns========================");
createNs("test_ns");
System.out.println("==================下面删除ns========================");
deleteNs("test_ns1");
System.out.println("==================下面插入表数据========================");
insterData ( "test_ns: Student", "001", "info", "name", "zhangsan");
System.out.println ( "================== The following table data acquiring ======================== ");
the getData (" test_ns: Student "," 001 ", null, null);
/
the System .out.println ( "================== traverse the table below data ====================== == ");
SCANDATA (" test_ns: Student ");
closeConn ();
}
}

Guess you like

Origin blog.51cto.com/14159501/2452294