20,808
社区成员
发帖
与我相关
我的任务
分享
package com.ericsson.andromeda.put;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
public class PutToHbase {
public static Configuration conf;
private static HTable table;
private static File file;
static{
/**
*
*/
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set(
"hbase.zookeeper.quorum",
"sdw017.andromeda.com,sdw016.andromeda.com,sdw015.andromeda.com");
conf.set("hbase.master", "192.168.1.202:60000");
// conf.set("hbase.zookeeper.property.dataDir", "/root/zookeeper");
conf.set("hbase.cluster.distributed", "true");
conf.set("hbase.rootdir", "hdfs://mdw.andromeda.com:8020/apps/hbase/data");
file = new File("/andromeda/cdrfile/NC_5G.csv");
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
//set a list for hadoop put be used
List<Put> puts = new ArrayList<Put>();
//read the source file
BufferedReader bufr;
int count = 0;
int count2 = 0;
try {
table = new HTable(conf, "testnc");
//table.setAutoFlush(false);
//To determine whether a file exists
if(!file.exists()){
System.out.println("source file is not exist or find");
System.exit(0);
}
bufr = new BufferedReader(new FileReader(file));
String read = null;
//print time on start job is done
System.out.println("Start time : "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
//start the put process
while((read = bufr.readLine())!=null){
//split the source date to the String[]
String[] fields = read.split("\\|");
//the callingNum is the Row_Key
String callingNum = fields[9];
//get the Put Object
Put put = new Put(callingNum.getBytes());
for(int i=0;i<fields.length;i++){
//get the field from source file,transfrom this to be the hbase field,eg:family,column and value
put.add("other".getBytes(),("field"+i).getBytes(), fields[i].getBytes());
}
puts.add(put);
//if the put object > 5000 ,commit date to hbase
if(puts.size()>5000){
table.put(puts);
table.flushCommits();
puts.clear();
System.out.println("now it is "+(count2++)+".........5000.........");
}
count++;
}
if(puts.size()>0){
table.put(puts);
table.flushCommits();
puts.clear();
}
System.out.println("Put data is done.Total is "+count+" line");
table.close();
long endTime = System.currentTimeMillis();
long time = endTime-startTime;
System.out.println("end time : "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
System.out.println("The put job has time is: "+time+" second");
} catch (Exception e) {
e.printStackTrace();
}
}
}