HBase Filter使用方法(三)------批量put导入
admin
2023-02-08 06:20:03
0

一、应用线程导入

    1、创建put方法

public class mmm{
	public static Configuration config=new Configuration();;
	static{
		config.set("hbase.zookeeper.property.clientPoint","2181");
		config.set("hbase.zookeeper.quorum","hbase");
		config.set("dfs.socket.timeout", "180000");
	}
    public static void put(String tablename,String RowKey,String Family,String Qualifier,String Value){
    HTable h=null;
    try{
        h=new HTable(config,tablename);
        Put put=new Put(Bytes.toBytes(RowKey));
        put.add(Bytes.toBytes(Family), Bytes.toBytes(Qualifier), Bytes.toBytes(Value));
        h.put(put);
    }catch(Exception e){e.printStackTrace();}finally{
        try {
        h.close();
        } catch (IOException e) {
            e.printStackTrace();
            }
        }
    }
}

    2、创建线程

        

    public class PutXX {
    public static void main(String[] args) {
        run1 r1=new run1();
        Thread rr1=new Thread(r1);
        rr1.start();
        run2 r2=new run2();
        Thread rr2=new Thread(r2);
        rr2.start();
        //这里创建两个线程,需要可以继续创建~
}
}
class run1 implements Runnable{
public void run() {
for(int i=0;i<=10000000;i++){
    mmm.put("yuan", ""+i+"", "property", "yuan_name", "xx"+i);
    System.out.println(i);
    }
}
}
class run2 implements Runnable{
public void run() {
for(int i=10000001;i<=20000000;i++){
    mmm.put("yuan", ""+i+"", "property", "yuan_name", "xx"+i);
    System.out.println(i);
    }
}
}


    <2015.2.2>

    数据导入速度慢。之前put方法每执行一次就要new一个新HTable然后释放资源。。太墨迹了


    新put

    

public static void NBput(String tablename,int RowKey,String Family,String Qualifier,String Value){
        HTable h=null;
        try {
            h=new HTable(config,tablename);
            for(int i=RowKey;i<=(RowKey+10000000);i++){
            String row=""+i+"";
            Put put=new Put(Bytes.toBytes(row));
            put.add(Bytes.toBytes(Family), Bytes.toBytes(Qualifier), Bytes.toBytes(Value));
            h.put(put);
            System.out.println(i);
        }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
            try {
                h.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

        修改以后速度明显提升啊我去!之前那方法太傻缺了。。。。




二、使用List进行put

    public class mmm{
    public static Configuration config=new Configuration();;
	static{
		config.set("hbase.zookeeper.property.clientPoint","2181");
		config.set("hbase.zookeeper.quorum","hbase");
		config.set("dfs.socket.timeout", "180000");
	}
	/*
	 * 批量put
	 * 
	 */
	public static void moreput(String tablename, List puts){
		HTable h=null;
		try{
			h=new HTable(config,tablename);
			Put put=new Put(Bytes.toBytes(""));
			puts.add(put);
			h.put(puts);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				puts.clear();
				h.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

    

    然后在主函数中:

         
    List puts=new ArrayList();
    for(int i=100000;i<=5000000;i++){
        System.out.println(i);
        Put put=new Put(Bytes.toBytes(""+i+""));
        put.add(Bytes.toBytes("property"), Bytes.toBytes("yuan_name"), Bytes.toBytes("源网"+i));
        puts.add(put);
}
    System.out.println("写入List完成");
    mmm.moreput("yuan", puts);


相关内容

热门资讯

今日重大发现“开心游戏.是不是... 有 亲,根据资深记者爆料开心游戏是可以开挂的,确实有挂(咨询软件无需打开...
我来教教您“麻友圈2.怎么装挂... 网上科普关于“麻友圈2有没有挂”话题很是火热,小编也是针对麻友圈2作*弊开挂的方法以及开挂对应的知识...
最新引进“长虹大厅.是不是有挂... 您好:长虹大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游戏...
今日重大通报“旺旺冲击麻将.有... 家人们!今天小编来为大家解答旺旺冲击麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
重磅消息“蜀山四川麻将.到底有... 有 亲,根据资深记者爆料蜀山四川麻将是可以开挂的,确实有挂(咨询软件无需...
火箭发射高度活跃 产业链上市公... 本报记者 丁蓉 近日,火箭发射活动十分密集。12月13日,快舟十一号遥八运载火箭在酒泉卫星发射中心发...
终于明白“边锋老友麻将.究竟有... 家人们!今天小编来为大家解答边锋老友麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
【第一财经】“丫丫古诗.辅助器... 家人们!今天小编来为大家解答丫丫古诗透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很...
我来教教您“新永和牛牛.开挂器... 有 亲,根据资深记者爆料新永和牛牛是可以开挂的,确实有挂(咨询软件无需打...
玩家分享攻略“十胡卡.辅助开挂... 家人们!今天小编来为大家解答十胡卡透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很多...