个性化阅读
专注于IT技术分析

HBase示例

本文概述

让我们看一个HBase示例, 该示例在HBase表中导入文件数据。

用例

我们必须通过Java API将文件中存在的数据导入到HBase表中。

Data_file.txt包含以下数据

1, India, Bihar, Champaran, 2009, April, P1, 1, 5
2, India, Bihar, Patna, 2009, May, P1, 2, 10
3, India, Bihar, Bhagalpur, 2010, June, P2, 3, 15
4, United States, California, Fresno, 2009, April, P2, 2, 5
5, United States, California, Long Beach, 2010, July, P2, 4, 10
6, United States, California, San Francisco, 2011, August, P1, 6, 20

Java代码如下所示

此数据必须输入到要通过JAVA API创建的新HBase表中。必须创建以下列族

"sample, region, time.product, sale, profit".

列族区域具有三个列限定符:国家, 州, 城市

列族时间有两个列限定符:年, 月

Jar文件

确保编写HBase所需的代码时存在以下jar。

  1. commons-loging-1.0.4
  2. commons-loging-api-1.0.4
  3. hadoop-core-0.20.2-cdh3u2
  4. hbase-0.90.4-cdh3u2
  5. log4j-1.2.15
  6. zookeper-3.3.3-cdh3u0

程式码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;


public class readFromFile {
	public static void main(String[] args) throws IOException{
		if(args.length==1)
			{
			Configuration conf = HBaseConfiguration.create(new Configuration());
			HBaseAdmin hba = new HBaseAdmin(conf);
			if(!hba.tableExists(args[0])){
				HTableDescriptor ht = new HTableDescriptor(args[0]);
				ht.addFamily(new HColumnDescriptor("sample"));
				ht.addFamily(new HColumnDescriptor("region"));
				ht.addFamily(new HColumnDescriptor("time"));
				ht.addFamily(new HColumnDescriptor("product"));
				ht.addFamily(new HColumnDescriptor("sale"));
				ht.addFamily(new HColumnDescriptor("profit"));
				hba.createTable(ht);
				System.out.println("New Table Created");
				
				HTable table = new HTable(conf, args[0]);
			
				File f = new File("/home/training/Desktop/data");
				BufferedReader br = new BufferedReader(new FileReader(f));
				String line = br.readLine();
				int i =1;
				String rowname="row";
				while(line!=null && line.length()!=0){
					System.out.println("Ok till here");
					StringTokenizer tokens = new StringTokenizer(line, ", ");
					rowname = "row"+i;
					Put p = new Put(Bytes.toBytes(rowname));
					p.add(Bytes.toBytes("sample"), Bytes.toBytes("sampleNo."), Bytes.toBytes(Integer.parseInt(tokens.nextToken())));
					p.add(Bytes.toBytes("region"), Bytes.toBytes("country"), Bytes.toBytes(tokens.nextToken()));
					p.add(Bytes.toBytes("region"), Bytes.toBytes("state"), Bytes.toBytes(tokens.nextToken()));
					p.add(Bytes.toBytes("region"), Bytes.toBytes("city"), Bytes.toBytes(tokens.nextToken()));
					p.add(Bytes.toBytes("time"), Bytes.toBytes("year"), Bytes.toBytes(Integer.parseInt(tokens.nextToken())));
					p.add(Bytes.toBytes("time"), Bytes.toBytes("month"), Bytes.toBytes(tokens.nextToken()));
					p.add(Bytes.toBytes("product"), Bytes.toBytes("productNo."), Bytes.toBytes(tokens.nextToken()));
					p.add(Bytes.toBytes("sale"), Bytes.toBytes("quantity"), Bytes.toBytes(Integer.parseInt(tokens.nextToken())));
					p.add(Bytes.toBytes("profit"), Bytes.toBytes("earnings"), Bytes.toBytes(tokens.nextToken()));
					i++;
					table.put(p);
					line = br.readLine();
				}
					br.close();
					table.close();
				}
			else
				System.out.println("Table Already exists.Please enter another table name");
		}
		else
			System.out.println("Please Enter the table name through command line");		
	}
}
赞(0)
未经允许不得转载:srcmini » HBase示例

评论 抢沙发

评论前必须登录!