本文概述
- 用例
- Jar文件
- 程式码
用例我们必须通过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示例】确保编写HBase所需的代码时存在以下jar。
- commons-loging-1.0.4
- commons-loging-api-1.0.4
- hadoop-core-0.20.2-cdh3u2
- hbase-0.90.4-cdh3u2
- log4j-1.2.15
- 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");
}
}
推荐阅读
- HBase安装
- HBase数据模型
- Mac版Word的11个最佳替代品合集(文字处理软件)
- Mac的EMR软件(最佳EHR和医疗计费软件合集)
- Mac的10款最佳OCR扫描软件(PDF)完整合集
- Mac的10款最佳卡拉OK软件列表(你最喜欢哪个())
- Mac的10个最佳个人理财软件(Quicken替代品)
- Mac的12大最佳免费图表软件合集(Visio替代品)
- 适用于Mac的8款最佳GPS软件合集(哪个最好())