HBase Java API

Hadoop兼容性问题解决

 

 在本机中配置环境变量

配置完后需要重启电脑

 

RegionServer内存分布

     -1.用户读取数据的缓存,有时候在一段时间内反复查询数据,系统meta表的数据应全部放到内存中

     -2.用于写的缓存,memStore的使用

 

如果HBase集群,如果写的要求不高,就慢慢写,那么memstore的值可以设置小一些

衍生

     从HBASE读取数据的时候,会从那些地方去查询

     -1.MemStore

     -2.Cache Block

     -3.StoreFile

 

读取hbase 表中数据(get):

public class A_GetDemo {
    public static void main(String[] args) {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = null;
        Table table = null;
        try {
            connection = ConnectionFactory.createConnection(conf);
            table = connection.getTable(TableName.valueOf("ns1:sale_orders"));

            Get get = new Get(Bytes.toBytes("40419271_2015-04-21 00:00:21_314295453511152"));
            Result result = table.get(get);

            System.out.println("RowKey:"+Bytes.toString(result.getRow()));

            for (Cell cell:result.rawCells()){
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                String field = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(cf +":"+field+"->"+value);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeStream(table);
            IOUtils.closeStream(connection);
        }
    }
}

 

扫描表(sacn)

public class B_ScanDemo {
    public static void main(String[] args) {
        //1、读取配置信息
        Configuration conf = HBaseConfiguration.create();
        //System.out.println(conf);
        Connection conn = null;
        Table table = null;

        try {
            conn = ConnectionFactory.createConnection(conf);
            table = conn.getTable(TableName.valueOf("ns1:sale_orders"));
            scanData(table);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void scanData(Table table){
        Scan scan = new Scan();


        scan.setStartRow(Bytes.toBytes("15267271_2015-04-21 00:00:00"));
        scan.setStopRow(Bytes.toBytes("15267271_2015-04-22 00:00:00"));

        scan.addFamily(Bytes.toBytes("info"));
        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("date"));
        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("order_amt"));
        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("order_id"));

        try {
            ResultScanner scanner = table.getScanner(scan);

            for (Result result:scanner) {
                System.out.println("RowKey:"+Bytes.toString(result.getRow()));
                for (Cell cell:result.rawCells()){
                    String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                    String field = Bytes.toString(CellUtil.cloneQualifier(cell));
                    String value = Bytes.toString(CellUtil.cloneValue(cell));
                    System.out.println(cf +":"+field+"->"+value);
                }
                System.out.println("----------------------------------");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

创建表:

public class C_CreateTableDemo {
    public static void main(String[] args) {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = null;
        Table table = null;
        HBaseAdmin admin = null;

        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) conn.getAdmin();

            String namespace = "ns2";
            NamespaceDescriptor nsDesc = NamespaceDescriptor.create(namespace).build();
            admin.createNamespace(nsDesc);

            /**
             * 创建表:表名,列簇,
             * 压缩,设置预分区、BLOCKCACHE
             */
            String tableName = namespace + ":stu_info";
            if (admin.tableExists(tableName)){
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            }
            HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
            HColumnDescriptor family = new HColumnDescriptor("info");
            family.setBlockCacheEnabled(false);

            family.setCompressionType(Compression.Algorithm.SNAPPY);

            family.setVersions(1,3);
            desc.addFamily(family);

            byte[][] splitKeys = {
              Bytes.toBytes("5"),Bytes.toBytes("8")
            };
            admin.createTable(desc,splitKeys);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeStream(admin);
            IOUtils.closeStream(conn);
        }
    }

}

 

向表中添加数据:

public class D_PutDemo {
    public static void main(String[] args) {
        //1、读取配置信息
        Configuration conf = HBaseConfiguration.create();
        //System.out.println(conf);
        Connection conn = null;
        Table table = null;

        try {
            //2.获取连接
                conn = ConnectionFactory.createConnection( conf );
                //3.获取HBASE table的句柄,可以对表中的数据进行CURD操作
                table = conn.getTable( TableName.valueOf( "ns2:stu_info" ) );
                putData(table);
//                delete(table);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
            IOUtils.closeStream( table );
            IOUtils.closeStream( conn);
        }
    }



    private static void putData(Table table) {
        HashMap<String, String> stuMap = new HashMap<>();
        stuMap.put("id","111");
        stuMap.put("name","xyy");
        stuMap.put("age","18");
        stuMap.put("address","qqcy");

        Put put = new Put(Bytes.toBytes("100001"));
        byte[] cf = Bytes.toBytes("info");

        for (Map.Entry<String,String> entry:stuMap.entrySet()) {
            put.addColumn(cf,Bytes.toBytes(entry.getKey()),Bytes.toBytes(entry.getValue()));
        }

        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void delete(Table table) {
        Delete delete = new Delete(Bytes.toBytes("100001"));
        delete.addColumn(Bytes.toBytes("info"),Bytes.toBytes("id"));

        try {
            table.delete(delete);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值