【Springboot HBase】遇到的一些问题

想要运行的代码需要在application中运行

使用@Component并实现CommandLineRunner接口、重写方法@Override run( )

@Component
public class BillionOrder implements CommandLineRunner {
...
...
...
@Override
    public void run(String... args) throws Exception {
        //在这里写要运行的代码
    }
   
}

list=null 与 list.clear()的区别:

总之,清空List用clrar()方法是正确无误的,=null不好,反正我用=null在程序中出问题了==
java中list集合通过clear()方法清空,只会将list中的对象变成垃圾回收清空,但是list对象还是存在。
但是通过list=null后,不仅列表中的对象变成了垃圾,为列表分配的空间也会回收,什么都不做与赋值NULL一样,说明直到程序结束也用不上列表list了,它自然就成为垃圾了.clear()只是清除了对象的引用,使那些对象成为垃圾.

HBase api的使用

table.close()可解决错误"java.io.IOException:远程主机强迫关闭了一个现有的连接。"
出错的原因是插入数据建立连接没有关闭,连接建立太多了

	//插入数据
    //orderindex索引表
    public static void insertData(String tableName, List<Order> olist) throws IOException {
        TableName tablename = TableName.valueOf(tableName);
        Table table = initHbase().getTable(tablename);
        //orderindex索引表
        TableName tablenameindex = TableName.valueOf("orderindex");
        Table indextable = initHbase().getTable(tablenameindex);

        for(Order order:olist){
            String rowkey=order.getUserid()+"_"+order.getOrdertime();
            Put put = new Put((rowkey).getBytes());
            //参数:1.列族名  2.列名  3.值
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("userid"),Bytes.toBytes(order.getUserid()+"")) ;
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("orderid"), Bytes.toBytes(order.getOrderid()+"")) ;
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("ordertime"), Bytes.toBytes(order.getOrdertime())) ;
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("proids"), Bytes.toBytes(order.getProids().toString())) ;
            put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("state"), Bytes.toBytes(order.getState())) ;
            table.put(put);

            Put putindex = new Put((order.getOrderid()+"").getBytes());
            //参数:1.列族名  2.列名  3.值
            putindex.addColumn(Bytes.toBytes("info"),Bytes.toBytes("rowkey"),Bytes.toBytes(rowkey)) ;
            indextable.put(putindex);
        }
        table.close();
        indextable.close();
    }
原文地址:https://www.cnblogs.com/BIG-BOSS-ZC/p/11807285.html