//下面代码需要新建一个文本文件作为读取内容 import java.io.BufferedReader; import java.io.FileReader; import java.util.List; import java.util.ArrayList; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Get; 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.HTableDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.KeyValue; /* * @author michael * 2014.4.22 * * */ public class HBaseSample { static Configuration conf=null; static{ conf=HBaseConfiguration.create();// this is used to configure the hbase conf.set("hbase.zookeeper.quorum", "localhost"); } public static void main(String[] args) { // TODO Auto-generated method stub HBaseSample tableNew=new HBaseSample(); try { HBaseAdmin admin=new HBaseAdmin(conf); if(admin.tableExists("shakespeare")) //if the table has existed,delete it { admin.disableTable("shakespeare"); admin.deleteTable("shakespeare"); } tableNew.createTable("shakespeare", new String[]{"word","count"}); //create the table BufferedReader reader=new BufferedReader(new FileReader("wordCountPart.txt")); String str,rowkey,word,value; int rowNum=0; while((str=reader.readLine())!=null) //循环从文本中读入数据;文本中的每一行是:word count;例如: a 111; { rowNum++; rowkey=String.valueOf(rowNum); word=str.split("\t")[0]; //以tbale键分开 value=str.split("\t")[1]; tableNew.insertRow("shakespeare",rowkey,"word","the only word",word); tableNew.insertRow("shakespeare",rowkey,"count",word+"'s value",value); } reader.close(); System.out.println("下面输出整张表"); tableNew.showAll("shakespeare"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /***** * create a table,and assign the column * */ public void createTable(String tableName,String columns[])throws Exception{ HBaseAdmin admin=new HBaseAdmin(conf); //客户端管理工具类 if(admin.tableExists(tableName)) System.out.println("The table is already exist"); else { HTableDescriptor table=new HTableDescriptor(tableName); for(String c:columns) { HColumnDescriptor col=new HColumnDescriptor(c); table.addFamily(col); } admin.createTable(table); admin.close(); System.out.println("The table has been created successfully"); } } /** * delete the table * */ public void deleteTable(String tableName)throws Exception { HBaseAdmin hAdmin=new HBaseAdmin(conf); if(hAdmin.tableExists(tableName)) { hAdmin.disableTable(tableName); hAdmin.deleteTable(tableName); } System.out.println("the table has been deleted successfully!"); } /** * add the data * */ public static Put insertRowByPut(String row,String columnFamily, String column, String value) { Put put=new Put(Bytes.toBytes(row)); put.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column), Bytes.toBytes(value)); return put; } 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("The data has been inserted successfully!"); } /** * 删除多条数据 * */ public void deleteByRow(String tableName,String rowkey[])throws Exception { HTable h=new HTable(conf,tableName); List<Delete>list=new ArrayList<Delete>(); //Get g=new Get(Bytes.toBytes(rowkey)); for(String k:rowkey) { Delete d=new Delete(Bytes.toBytes(k)); list.add(d); } h.delete(list); h.close(); } /** * 得到一个数据 * */ 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())); String ss=Bytes.toString(k.getValue()); System.out.println("值是"+ss); } h.close(); } /** * 扫描所有的数据 * */ public void showAll(String tableName) throws Exception{ HTable h=new HTable(conf,tableName); Scan scan=new Scan(); ResultScanner scanner=h.getScanner(scan); for(Result r:scanner) { System.out.println("=============================="); for(KeyValue k:r.raw()) { System.out.println("the rowkey:"+Bytes.toStringBinary(k.getRow())); System.out.println("the TimeStap:"+k.getTimestamp()); System.out.println("the ColumnKey:"+Bytes.toStringBinary(k.getFamily())); System.out.println("the Column:"+Bytes.toStringBinary(k.getQualifier())); String ss=Bytes.toString(k.getValue()); System.out.println("the value:"+ss); } } h.close(); } }