`
wbj0110
  • 浏览: 1549602 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

HBase利用bulk load批量导入数据

阅读更多

OneCoder只是一个初学者,记录的只是自己的一个过程。不足之处还望指导。

看网上说导入大量数据,用bulk load的方式效率比较高。bulk load可以将固定格式的数据文件转换为HFile文件导入,当然也可以直接导入HFile文件。所以OneCoder最开始考虑的生成HFile文件供HBase导入,不过由于手太新,一直没有搞定。参考了很多网上的代码也没跑通。暂时搁浅。

后来OneCoder采用了,生成普通的数据格式文件,然后用过imporsttsv命令导入的方式成功。生成数据文件代码如下:

 

01.private static final String PATH = "F:/data.txt";
02. 
03./**
04.* @param args
05.* @author lihzh
06.* @alia OneCoder
07.* @blog http://www.it165.net
08.* @throws IOException
09.* @date 2012-11-14 下午4:51:22
10.*/
11.public static void main(String[] args) throws IOException {
12.long startTime = System.currentTimeMillis();
13.File dataFile = getFile();
14.FileWriter writer = null;
15.try {
16.writer = new FileWriter(dataFile);
17.int timeCount = 1;
18.int resourceCount = 1;
19.for (int j = 0; j < timeCount; j++) {
20.long timeStamp = System.currentTimeMillis();
21.for (int i = 0; i < resourceCount; i++) {
22.UUID uuid = UUID.randomUUID();
23.String rowKey = uuid.toString() + "_" + timeStamp;
24.Random random = new Random();
25.String cpuLoad = String.valueOf(random.nextDouble())
26..substring(04);
27.String memory = String.valueOf(random.nextDouble())
28..substring(04);
29.StringBuilder builder = new StringBuilder();
30.builder.append(rowKey).append("\t").append(cpuLoad)
31..append("\t").append(memory).append("\t").append(uuid.toString()).append("\t").append(timeStamp);
32.writer.append(builder.toString());
33.if ((i +  1) * (j + 1) < timeCount * resourceCount) {
34.writer.append("\r");
35.}
36.}
37.}
38.long endTime = System.currentTimeMillis();
39.System.out.println("Cost Time: " + (endTime - startTime));
40.catch (IOException e) {
41.e.printStackTrace();
42.finally {
43.writer.close();
44.}
45.}
46. 
47./**
48.* 得到一个新文件
49.*
50.* @return
51.* @author lihzh
52.* @date 2012-11-14 下午4:53:31
53.*/
54.private static File getFile() {
55.File file = new File(PATH);
56.if (!file.exists()) {
57.try {
58.file.createNewFile();
59.catch (IOException e) {
60.e.printStackTrace();
61.}
62.}
63.return file;
64.}


 

文件格式大致如下:

29611690-69cb-4749-8bd5-be75793d6611_1352968490061 0.41 0.34 29611690-69cb-4749-8bd5-be75793d6611 1352968490061

然后将文件上传到HDFS中,

hadoop fs -put /home/admin/Desktop/data.txt /test

转换成HFile格式存储  www.it165.net

hadoop jar hbase-version.jar importtsv -Dimporttsv.columns=HBASE_ROW_KEY,c1,c2 -Dimporttsv.bulk.output=tmp hbase_table hdfs_file  生成HFile文件。其中c1,c2
是列名,格式为:列族:列名

然后,导入到HBase中:

hadoop jar hbase-version.jar completebulkload /user/hadoop/tmp/cf hbase_table

这里的路径都是hdfs的路径。

 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics