美文网首页我爱编程
javaAPI操作HBase

javaAPI操作HBase

作者: _Kantin | 来源:发表于2017-11-22 10:31 被阅读55次

    平台:window+linux
    工具:IDEA2016

    public class HbaseTest {
        public static void main(String[] args) throws Exception {
    //        createTab();
    //        HTable table = getTable();
    //        System.out.println(table.getName());
    //        putData();
            getData();
        }
    
        //添加文本过滤器
        public static void testFilter() {
            HTable table = null;
            try {
                table = getTable();
                Scan scan = new Scan();
    
                // create filter
                Filter filter = null;
                filter = new PrefixFilter(Bytes.toBytes("rk"));
                filter = new PageFilter(3);
    
                // create hbase comparator
                ByteArrayComparable comp = null;
                comp = new SubstringComparator("lisi");
                filter = new SingleColumnValueFilter(
                        Bytes.toBytes("info"),
                        Bytes.toBytes("name"),
                        CompareOp.EQUAL,
                        comp);
    
                // set filter
                scan.setFilter(filter);
    
                ResultScanner rs = table.getScanner(scan);
                for (Result result : rs) {
                    printResult(result);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(table != null) {
                    IOUtils.closeStream(table);
                }
            }
        }
    
        //HBase扫描数据
        public static void scanData() {
            HTable table = null;
            try {
                table = getTable();
    
                // create Scan instance
                Scan scan = new Scan();
    
                scan.setStartRow(Bytes.toBytes("rk0001"));
                scan.setStopRow(Bytes.toBytes("rk0003"));
    
                scan.addFamily(Bytes.toBytes("info"));
    
                // set cache
                scan.setCacheBlocks(false);
                scan.setBatch(2);
                scan.setCaching(2);
    
                // set permmission
    //          scan.setACL(perms);
    
    
                // get scanner results
                ResultScanner rs = table.getScanner(scan);
    
                for(Result result : rs) {
                    printResult(result);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(table != null) {
                    IOUtils.closeStream(table);
                }
            }
        }
    
        //一个打印的方法
        public static void printResult(Result rs) {
            for (Cell cell : rs.rawCells()) {
                System.out.println(
                        Bytes.toString(CellUtil.cloneFamily(cell)) + " : " +
                                Bytes.toString(CellUtil.cloneRow(cell)) + " : " +
                                Bytes.toString(CellUtil.cloneQualifier(cell)) + " : " +
                                Bytes.toString(CellUtil.cloneValue(cell))
                );
            }
        }
    
        //测试HBase的命名空间
        public static void testNamespace() throws Exception {
            Configuration c = HBaseConfiguration.create();
            HBaseAdmin admin = new HBaseAdmin(c );
    
            // create namespace
            NamespaceDescriptor namespace = NamespaceDescriptor.create("ns1").build();
    //      admin.createNamespace(namespace );
    
            admin.deleteNamespace("ns1");
    
            // close resource
            admin.close();
        }
    
    
        public  static Configuration getConfiguration(){
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.rootdir","hdfs://10.23.12.183:8020/hbase");
            conf.set("hbase.zookeeper.quorum","10.23.12.183");
            return conf;
        }
    
        public  static  void getData() throws Exception {
            HTable table  =getTable();
            Get get =  new Get(Bytes.toBytes("rk0001"));
            Result result = table.get(get);
            for (Cell cell: result.rawCells()
                 ) {
                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  putData() throws Exception {
            HTable table = getTable();
            //插入一个行健row
            Put put = new Put(Bytes.toBytes("rk0001"));
    
            put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
            table.put(put);
            table.close();
        }
    
        //从HBase中读出一个Tab所属的表
        public  static  HTable getTable() throws Exception {
            HTable table = new HTable(getConfiguration(),Bytes.toBytes("t1"));
            return table;
        }
    
        //往HBase中写入一个Tab“t1”
        public  static  void createTab() throws Exception {
            HBaseAdmin admin = new HBaseAdmin(getConfiguration());
    
            boolean b = admin.tableExists(Bytes.toBytes("t1"));
            if(b){
                admin.disableTable(Bytes.toBytes("t1"));
                admin.deleteTable("t1");
            }
            HTableDescriptor table  = new HTableDescriptor(TableName.valueOf("t1"));
            table.addFamily(new HColumnDescriptor((Bytes.toBytes("info"))));
            table.addFamily(new HColumnDescriptor((Bytes.toBytes("secret"))));
    
            admin.createTable(table);
            admin.close();
        }
    
    }
    

    POM.XML为

    <modelVersion>4.0.0</modelVersion>
        <groupId>com.jxky.bigdata</groupId>
        <artifactId>hbase</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <hbase.version>1.1.12</hbase.version>
            <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
        </properties>
    
        <repositories>
            <repository>
                <id>cloudera</id>
                <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
            </repository>
        </repositories>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>${hbase.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-server</artifactId>
                <version>${hbase.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>${hadoop.version}</version>
            </dependency>
        </dependencies>
    </project>
    

    相关文章

      网友评论

        本文标题:javaAPI操作HBase

        本文链接:https://www.haomeiwen.com/subject/wolnvxtx.html