java生成数据插入hbase_用java读取一个文件往hbase里插入数据

packagecom.asp;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.client.HBaseAdmin;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hbase.client.HTablePool;importorg.apache.hadoop.hbase.client.Put;public classTest {public static Configuration conf =HBaseConfiguration.create();static{

Configuration HBASE_CONFIG= newConfiguration();

HBASE_CONFIG.set("hbase.zookeeper.quorum","ugcserver3,ugcserver4,ugcserver5");

HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");

conf=HBaseConfiguration.create(HBASE_CONFIG);

}public static voidcreatTable(String tableName, String[] familys)throwsException {

HBaseAdmin admin= newHBaseAdmin(conf);if(admin.tableExists(tableName)) {

System.out.println("table already exists!");

}else{

HTableDescriptor tableDesc= newHTableDescriptor(tableName);for (int i = 0; i < familys.length; i++) {

tableDesc.addFamily(newHColumnDescriptor(familys[i]));

}

admin.createTable(tableDesc);

System.out.println("create table " + tableName + " ok");

}

}public static voidmain(String[] args) {

String tableName= "test";

String[] fa= { "c1", "c2"};int i = 0;

List list = new ArrayList();try{

Test.creatTable(tableName, fa);

}catch(Exception e) {

e.printStackTrace();

}

System.out.println("测试开始插入数据");long start =System.currentTimeMillis();

HTablePool pool= new HTablePool(conf, 1000);

HTable table=(HTable) pool.getTable(tableName);

table.setAutoFlush(false);try{

table.setWriteBufferSize(24*1024*1024);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

File file= new File("/opt/pmdce/datamining/123.txt");

BufferedReader reader= null;

String lineString= null;try{

reader= new BufferedReader(newFileReader(file));while ((lineString = reader.readLine()) != null) {

i++;

String[] lines= lineString.split("\\,");

Put put= new Put(lines[0].getBytes());

put.add("c1".getBytes(), "name".getBytes(), lines[1].getBytes());

put.add("c1".getBytes(), "age".getBytes(), lines[2].getBytes());

put.add("c2".getBytes(), "class".getBytes(),lines[3].getBytes());

list.add(put);if (i % 10000 == 0) {

table.put(list);

list.clear();

table.flushCommits();

}

}

reader.close();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{if (reader != null) {try{

reader.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}long stop =System.currentTimeMillis();

System.out.println("插入数据共耗时:" + (stop - start) * 1.0 / 1000 + "s");

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值