ãä¸è¬æ
åµä¸ï¼æ们使ç¨Linuxçshellå½ä»¤ï¼å°±å¯ä»¥é常轻æ¾çæä½Hbaseï¼ä¾å¦ä¸äºå»ºè¡¨ï¼å»ºåç°ï¼æå¼ï¼æ¾ç¤ºææ表ï¼ç»è®¡æ°éççï¼ä½ææ¶ä¸ºäºæé«çµæ´»æ§ï¼æ们ä¹éè¦ä½¿ç¨ç¼ç¨è¯è¨æ¥æä½Hbaseï¼å½ç¶Hbaseéè¿Thriftæ¥å£æä¾äºå¯¹å¤§å¤æ°ä¸»æµç¼ç¨è¯è¨çæ¯æï¼ä¾å¦C++,PHPï¼Pythonï¼Rubyççï¼é£ä¹æ¬ç¯ï¼æ£ä»ç»åºçä¾åæ¯åºäºJavaåççAPIæä½Hbaseï¼ç¸æ¯å
¶ä»çä¸äºç¼ç¨è¯è¨ï¼ä½¿ç¨Javaæä½Hbaseï¼ä¼æ´å é«æä¸äºï¼å 为Hbaseæ¬èº«å°±æ¯ä½¿ç¨Javaè¯è¨ç¼åçã转载
ä¸é¢ï¼æ£ä»ç»åºæºç ï¼ä»¥ä¾åè:
ããpackage com.hbase;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
/**
* @author ä¸å«æ£ä»
*
* **/
public class Test {
static Configuration conf=null;
static{
conf=HBaseConfiguration.create();//hbaseçé
置信æ¯
conf.set("hbase.zookeeper.quorum", "10.2.143.5"); //zookeeperçå°å
}
public static void main(String[] args)throws Exception {
Test t=new Test();
//t.createTable("temp", new String[]{"name","age"});
//t.insertRow("temp", "2", "age", "myage", "100");
// t.getOneDataByRowKey("temp", "2");
t.showAll("temp");
}
/***
* å建ä¸å¼ 表
* 并æå®åç°
* */
public void createTable(String tableName,String cols[])throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);//客æ·ç«¯ç®¡çå·¥å
·ç±»
if(admin.tableExists(tableName)){
System.out.println("æ¤è¡¨å·²ç»åå¨.......");
}else{
HTableDescriptor table=new HTableDescriptor(tableName);
for(String c:cols){
HColumnDescriptor col=new HColumnDescriptor(c);//åç°å
table.addFamily(col);//æ·»å å°æ¤è¡¨ä¸
}
admin.createTable(table);//å建ä¸ä¸ªè¡¨
admin.close();
System.out.println("å建表æå!");
}
}
/**
* æ·»å æ°æ®,
* 建议使ç¨æ¹éæ·»å
* @param tableName 表å
* @param row è¡å·
* @param columnFamily åç°
* @param column å
* @param value å
·ä½çå¼
*
* **/
public void insertRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(row));
// åæ°åºåå«ï¼åæãåãå¼
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);
table.close();//å
³é
System.out.println("æå
¥ä¸æ¡æ°æ®æå!");
}
/**
* å é¤ä¸æ¡æ°æ®
* @param tableName 表å
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Delete d=new Delete(Bytes.toBytes(rowkey));
h.delete(d);//å é¤ä¸æ¡æ°æ®
h.close();
}
/**
* å é¤å¤æ¡æ°æ®
* @param tableName 表å
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey[])throws Exception{
HTable h=new HTable(conf, tableName);
List<Delete> list=new ArrayList<Delete>();
for(String k:rowkey){
Delete d=new Delete(Bytes.toBytes(k));
list.add(d);
}
h.delete(list);//å é¤
h.close();//éæ¾èµæº
}
/**
* å¾å°ä¸æ¡æ°æ®
*
* @param tableName 表å
* @param rowkey è¡å·
* ***/
public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Get g=new Get(Bytes.toBytes(rowkey));
Result r=h.get(g);
for(KeyValue k:r.raw()){
System.out.println("è¡å·: "+Bytes.toStringBinary(k.getRow()));
System.out.println("æ¶é´æ³: "+k.getTimestamp());
System.out.println("åç°: "+Bytes.toStringBinary(k.getFamily()));
System.out.println("å: "+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
// System.out.println("å¼: "+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println("å¼: "+ss);
//}
}
h.close();
}
/**
* æ«ææææ°æ®æç¹å®æ°æ®
* @param tableName
* **/
public void showAll(String tableName)throws Exception{
HTable h=new HTable(conf, tableName);
Scan scan=new Scan();
//æ«æç¹å®åºé´
//Scan scan=new Scan(Bytes.toBytes("å¼å§è¡å·"),Bytes.toBytes("ç»æè¡å·"));
ResultScanner scanner=h.getScanner(scan);
for(Result r:scanner){
System.out.println("==================================");
for(KeyValue k:r.raw()){
System.out.println("è¡å·: "+Bytes.toStringBinary(k.getRow()));
System.out.println("æ¶é´æ³: "+k.getTimestamp());
System.out.println("åç°: "+Bytes.toStringBinary(k.getFamily()));
System.out.println("å: "+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
// System.out.println("å¼: "+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println("å¼: "+ss);
//}
}
}
h.close();
}
}
æ¾ç¤ºæææ°æ®çæå°è¾åºå¦ä¸ï¼
ãã==================================
è¡å·: 1
æ¶é´æ³: 1385597699287
åç°: name
å: myname
å¼: 秦ä¸äº®
==================================
è¡å·: 2
æ¶é´æ³: 1385598393306
åç°: age
å: myage
å¼: 100
è¡å·: 2
æ¶é´æ³: 1385597723900
åç°: name
å: myname
å¼: ä¸å«æ£ä»
ç±æ¤ï¼å¯ä»¥çåºHbaseç对å¤çAPIæä¾æ¥å£ï¼æ¯é常ç®åæç¨çã
温馨提示:内容为网友见解,仅供参考