簡單使用hbase關(guān)鍵字: hbase我們來簡單了解如何操作hbase,hbase與我們常用的數(shù)據(jù)庫最大的差別就是列存儲和無數(shù)據(jù)類型,所有數(shù)據(jù)都以string類型存儲,再有如果hbase table里有5個字段,但實際只有4個字段有值,那么為null的那個字段是不占用空間的,這點比較好,可以跟我們常用的數(shù)據(jù)庫比較下
/**
* 定義幾個常量
*/
public static HBaseConfiguration conf = new HBaseConfiguration();
static HTable table = null;
/**
* 創(chuàng)建hbase table
* @param table
* @throws IOException
*/
public static void creatTable(String tablename) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (!admin.tableExists(new Text(tablename))) {
HTableDescriptor tableDesc = new HTableDescriptor(tablename);
tableDesc.addFamily(new HColumnDescriptor("ip:"));
tableDesc.addFamily(new HColumnDescriptor("time:"));
tableDesc.addFamily(new HColumnDescriptor("type:"));
tableDesc.addFamily(new HColumnDescriptor("cookie:"));
//注意這個C列,下面我會簡單以此列來說明列存儲
tableDesc.addFamily(new HColumnDescriptor("c:"));
admin.createTable(tableDesc);
System.out.println("table create ok!!!");
} else {
System.out.println("table Already exists");
}
}
將hadoop/hbase都啟動再運行,在hql中使用"desc tablename"就可以看出這個表有5個字段,下面我們再來往這個表里錄入點數(shù)據(jù),上面說過值為空的字段是不占用空間的,這里還要注意點,經(jīng)過我的測試,如果發(fā)現(xiàn)這個字段無值,就不要往hbase里面寫null值,當(dāng)然你要真往這個字段存null值是不會有任何問題的,但你查詢這個有null值的字段時,會有點毛病,當(dāng)然這個我也不知道怎么描述,有興趣的可以試試看,所以我下面有判斷,再者,hbase table里面的每一行數(shù)據(jù)集都必須有一個唯一row關(guān)鍵字,這個row你可以隨便定義,方便準(zhǔn)確找到你需要的數(shù)據(jù)
/**
* 錄入數(shù)據(jù)
* @throws Exception
*/
public static void insertData() throws Exception{
//讀取日志文件
BufferedReader reader = new BufferedReader(new FileReader("log file name"));
if(table==null)
table = new HTable(conf, new Text(tablename));
String line;
while((line = reader.readLine()) != null){
//這里我就不說了,先前有說明
LogAccess log = new LogAccess(line);
//這里我使用time+cookie為row關(guān)鍵字,確保不重復(fù),如果cookie記錄有重復(fù),將區(qū)別對待,這里暫不多做說明
String row = createRow(log.getTime(),log.getCookie());
long lockid = table.startUpdate(new Text(row));
if(!log.getIp().equals("") && log.getIp()!=null)
table.put(lockid, new Text("ip:"), log.getIp().getBytes());
if(!log.getTime().equals("") && log.getTime()!=null)
table.put(lockid, new Text("time:"), log.getTime().getBytes());
if(!log.getType().equals("") && log.getType()!=null)
table.put(lockid, new Text("type:"), log.getType().getBytes());
if(!log.getCookie().equals("") && log.getCookie()!=null)
table.put(lockid, new Text("cookie:"), log.getCookie().getBytes());
//這里要注意,我是往c列中寫入了5個字段,你可以想象,我在c列中存入了一個map
if(!log.getRegmark().equals("") && log.getRegmark()!=null)
table.put(lockid, new Text("c:_regmark"), log.getRegmark().getBytes());
if(!log.getRegmark2().equals("") && log.getRegmark2()!=null)
table.put(lockid, new Text("c:_regmark2"), log.getRegmark2().getBytes());
if(!log.getSendshow().equals("") && log.getSendshow()!=null)
table.put(lockid, new Text("c:_sendshow"), log.getSendshow().getBytes());
if(!log.getCurrenturl().equals("") && log.getCurrenturl()!=null)
table.put(lockid, new Text("c:_currenturl"), log.getCurrenturl().getBytes());
if(!log.getAgent().equals("") && log.getAgent()!=null)
table.put(lockid, new Text("c:_agent"), log.getAgent().getBytes());
//存入數(shù)據(jù)
table.commit(lockid);
}
}
O了,測試下吧
|
|
|
來自: 石頭狗 > 《bigtable》
評論
上面這篇文章詳細(xì)講解了hadoop/hbase的安裝以及啟動方式,有興趣看看吧