zoukankan      html  css  js  c++  java
  • 2.HBase相关学习

    1.HBase shell操作

    hbase shell

    查看版本

    version

    查看服务状态

    status

    查看有哪些表

    list

     

    创建表

    create 'students','info'
    list

     

    查看表结构

    describe 'students'

    插入数据

    put 'students','1001','info:name','jack'
    put 'students','1001','info:sex','male'
    put 'students','1001','info:age','18'
    put 'students','1002','info:name','lucy'
    put 'students','1002','info:age','19’

     

    单条查询

    #整行查询
    get 'students','1001'
    #指定列族:列
    get 'students','1001','info:name'

    扫描数据

    scan 'students'
    #左闭右开区间
    scan 'students',{STARTROW => '1001', STOPROW=>'1002'}
    scan 'students',{STARTROW => '1001'}

    更新字段

    get 'students','1001'
    put 'students','1001','info:name','lilei'
    put 'students','1001','info:age','20'
    get 'students','1001'

    count

    count 'students'

    变更表结构

    describe 'students'
    alter 'students',{NAME=>'info',VERSIONS=>3}
    describe 'students'

    删除数据

    #展出指定列
    delete 'students','1002','info:age'
    #删除整行
    deleteall 'students','1001'

    清空表

    #表下线
    disable 'students'
    #清空
    truncate 'students'

    必须先下线再清空

    删除表

    disable 'students'
    drop 'students'

    2.HBase api编程

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.gonghbase</groupId>
        <artifactId>hbase-tutorial-1103</artifactId>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>7</source>
                        <target>7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <properties>
            <hbase.version>2.0.2</hbase.version>
            <junit.version>4.12</junit.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>${hbase.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
        </dependencies>
    </project>

    自动import不太好,建议import-changes

    创建包和类

    package com.dajiangtai.hbase.java;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.*;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.io.compress.Compression;
    import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
    import org.apache.hadoop.hbase.regionserver.BloomType;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    public class HBaseDemo {
        public static Configuration conf;
    
        static {
            //使用HBaseConfiguration 的单例方法实例化
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "node01");
            conf.set("hbase.zookeeper.property.clientPort", "2181");
            conf.set("zookeeper.znode.parent", "/hbase-unsecure");
        }
    
        //创建表
        @Test
        public void testCreateTable() throws IOException {
            // Connection实现了java中的java.lang.AutoCloseable接口。所有实现了这个接口的类都可以在try-with-resources结构中使用。
    
            //创建Connection是一项繁重的操作。Connection线程安全的,因此客户端可以一次创建一个连接,并与其他线程共享。
            //另一方面,HBaseAdmin和HBaseAdmin实例是轻量级的,并且不是线程安全的。
            // 通常,每个客户端应用程序实例化单个Connection,每个线程都将获得其自己的Table实例。不建议对Table and Admin进行缓存或池化。
    
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();) {
                TableName tableName = TableName.valueOf("students");
                if (admin.tableExists(tableName)) {
                    System.out.println("table " + tableName.getNameAsString() + " exists");
                } else {
                    ColumnFamilyDescriptor cfDesc = ColumnFamilyDescriptorBuilder
                            .newBuilder(Bytes.toBytes("F"))
                            .setCompressionType(Compression.Algorithm.SNAPPY)
                            .setCompactionCompressionType(Compression.Algorithm.SNAPPY)
                            .setDataBlockEncoding(DataBlockEncoding.PREFIX)
                            .setBloomFilterType(BloomType.ROW)
                            .build();
    
                    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
                            .setColumnFamily(cfDesc)
                            .build();
    
                    admin.createTable(tableDesc);
                }
            }
        }
    
        //向表中插入数据
        @Test
        public void testPut() throws IOException {
            TableName tableName = TableName.valueOf("students");
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 Table table = connection.getTable(tableName);) {
    
                List<Put> puts = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    Put put = new Put(Bytes.toBytes(i+""));
    
                    put.addColumn(Bytes.toBytes("F"), Bytes.toBytes("name"), Bytes.toBytes("name" + i));
                    put.addColumn(Bytes.toBytes("F"), Bytes.toBytes("age"), Bytes.toBytes(20 + i+""));
                    put.addColumn(Bytes.toBytes("F"), Bytes.toBytes("address"), Bytes.toBytes("djt" + i));
    
                    puts.add(put);
                }
    
                table.put(puts);
            }
    
        }
    
        //获取指定行
        @Test
        public void testGet() throws IOException {
            TableName tableName = TableName.valueOf("students");
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 Table table = connection.getTable(tableName);) {
                String rowkey = "2";
                Get get = new Get(Bytes.toBytes(rowkey));
                Result result = table.get(get);
                for (Cell cell : result.rawCells()) {
                    System.out.println("rowkey = " + 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());
                }
            }
    
        }
    
        //获取指定行,指定列
        @Test
        public void testGet1() throws IOException {
            TableName tableName = TableName.valueOf("students");
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 Table table = connection.getTable(tableName);) {
                String rowkey = "2";
                Get get = new Get(Bytes.toBytes(rowkey));
                get.addColumn(Bytes.toBytes("F"), Bytes.toBytes("name"));
                get.addColumn(Bytes.toBytes("F"), Bytes.toBytes("age"));
                Result result = table.get(get);
                for (Cell cell : result.rawCells()) {
                    System.out.println("rowkey = " + 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());
                }
            }
        }
    
        //scan全表
        @Test
        public void testScan() throws IOException {
            TableName tableName = TableName.valueOf("students");
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 Table table = connection.getTable(tableName);) {
    
                Scan scan = new Scan();
    
                ResultScanner resultScanner = table.getScanner(scan);
    
                for (Result result : resultScanner) {
                    for (Cell cell : result.rawCells()) {
                        System.out.println("rowkey = " + 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());
                    }
                }
    
            }
        }
    
        @Test
        public void testScan1() throws IOException {
            TableName tableName = TableName.valueOf("students");
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 Table table = connection.getTable(tableName);) {
    
                Scan scan = new Scan();
    
                scan.addColumn(Bytes.toBytes("F"), Bytes.toBytes("name"));
                scan.addColumn(Bytes.toBytes("F"), Bytes.toBytes("age"));
    
                scan.setCacheBlocks(true);
                scan.setCaching(100);
                scan.withStartRow(Bytes.toBytes(5 + ""));
                scan.withStopRow(Bytes.toBytes(8 + ""));
    
                ResultScanner resultScanner = table.getScanner(scan);
    
                for (Result result : resultScanner) {
                    for (Cell cell : result.rawCells()) {
                        System.out.println("rowkey = " + 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());
                    }
                }
            }
        }
    
        //删除数据
        @Test
        public void testDelete() throws IOException {
            TableName tableName = TableName.valueOf("students");
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 Table table = connection.getTable(tableName);) {
    
                List<Delete> deletes = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    Delete delete = new Delete(Bytes.toBytes(i + ""));
                    deletes.add(delete);
                }
    
                table.delete(deletes);
            }
        }
    
        //删除表
        @Test
        public void testDeleteTable() throws IOException {
            try (Connection connection = ConnectionFactory.createConnection(conf);
                 HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();) {
                TableName tableName = TableName.valueOf("students");
                if (admin.tableExists(tableName)) {
                    //先disable再delete
                    admin.disableTable(tableName);
                    admin.deleteTable(tableName);
                    System.out.println("table " + tableName.getNameAsString() + " delete success");
                } else {
                    System.out.println("table " + tableName.getNameAsString() + " is not exists");
                }
            }
        }
    
        public static void main(String[] args) {
            System.out.println("hello hbase!");
    
        }
    }

    创建表

    向表中插入数据

    获取指定行的数据

    获取指定行,指定列数据

    全表扫描

    删除数据和删除表就不做演示了

  • 相关阅读:
    听较强节奏的歌曲,能让你更长时间投入到学习中
    小康网站维护笔记
    apache基础
    python安装多版本
    apache负载调优
    docker 进阶
    openstack 网络更改版
    linux 搭建testlink的问题总结
    26. Remove Duplicates from Sorted Array(LeetCode)
    Add to List 172. Factorial Trailing Zeroes(LeetCode)
  • 原文地址:https://www.cnblogs.com/braveym/p/13113950.html
Copyright © 2011-2022 走看看