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);


相关内容

热门资讯

专访王虹:从北大数学系曾经的“... “获奖挺好的。”王虹说话慢慢的,随后话锋一转,带着自嘲式的笑意,“但是题该做不出来,还是做不出来。”...
意大利纵火犯用猫助燃,当地动保... 【环球时报记者 林泽宇 环球时报特约记者 文简】据《意大利晨报》23日报道,近日意大利南部野火肆虐。...
第8国?美媒爆料:美政府正研究... 【环球时报综合报道】据美国《华盛顿邮报》23日报道,多名美国官员透露,美政府正研究是否在西非国家马里...
男子在采血站采血后口吐白沫、神... 7月23日,山西广播电视台《慧帮忙》栏目报道“我弟弟在霍州康宝生物科技采血站采血后口吐白沫、神志不清...
“安全先生”,不安全了 当地时间7月17日凌晨,以色列第25届议会以62票赞成、0票反对通过一项有关政党经费法的相关修正案,...
“AI科学家”,实验效率直接“... □ 本报记者 洪叶 徐春晖 窗明几净的空间里没有科研人员,取而代之的是一排充满科技感的自动化岛台,背...
云南一女子电脑屏内现“蚂蚁窝”... 大家平时清洁电脑显示屏吗? 你见过显示屏里进蚂蚁吗? 据媒体报道,云南临沧一名女子近期发现,自己使...
便携脑电采集新品发布,脑电信号... 一根发带将一个比硬币略大的采集设备贴在额头,就能实现脑电信号采集,脑机接口创新企业神舞科技7月22日...
工业和信息化部:依托标准牵引智... 日前,在北京举办的《人工智能 智能体互联》系列标准应用推进专题会上,工业和信息化部科技司副司长甘小斌...