HBase 1.1.2 Java 客户端 api(hbase 1.0 增删改查,建表、删表等)

说明:
1.第一部分为代码
2.第二部分为工程pom文件

  1. import org.apache.hadoop.conf.Configuration;  
  2. import org.apache.hadoop.hbase.*;  
  3. import org.apache.hadoop.hbase.client.*;  
  4. import org.apache.hadoop.hbase.util.Bytes;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. /** 
  9.  * Created by xuemin on 15/9/28. 
  10.  */  
  11. public class HBaseTest {  
  12.   
  13.   
  14.     public static Configuration configuration;  
  15.     public static Connection connection;  
  16.     public static Admin admin;  
  17.   
  18.   
  19.     public static void main(String[] args) throws IOException {  
  20.         createTable("t2",new String[]{"cf1","cf2"});  
  21.         insterRow("t2", "rw1", "cf1", "q1", "val1");  
  22.         getData("t2", "rw1", "cf1", "q1");  
  23.         scanData("t2", "rw1", "rw2");  
  24.         deleRow("t2","rw1","cf1","q1");  
  25.         deleteTable("t2");  
  26.     }  
  27.   
  28.     //初始化链接  
  29.     public static void init(){  
  30.         configuration = HBaseConfiguration.create();  
  31.         configuration.set("hbase.zookeeper.quorum","10.10.3.181,10.10.3.182,10.10.3.183");  
  32.         configuration.set("hbase.zookeeper.property.clientPort","2181");  
  33.         configuration.set("zookeeper.znode.parent","/hbase");  
  34.   
  35.         try {  
  36.             connection = ConnectionFactory.createConnection(configuration);  
  37.             admin = connection.getAdmin();  
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42.   
  43.     //关闭连接  
  44.     public static  void close(){  
  45.         try {  
  46.             if(null != admin)  
  47.                 admin.close();  
  48.             if(null != connection)  
  49.                 connection.close();  
  50.         } catch (IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.   
  54.     }  
  55.   
  56.     //建表  
  57.     public static void createTable(String tableNmae,String[] cols) throws IOException {  
  58.   
  59.         init();  
  60.         TableName tableName = TableName.valueOf(tableNmae);  
  61.   
  62.         if(admin.tableExists(tableName)){  
  63.             System.out.println("talbe is exists!");  
  64.         }else {  
  65.             HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);  
  66.             for(String col:cols){  
  67.                 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);  
  68.                 hTableDescriptor.addFamily(hColumnDescriptor);  
  69.             }  
  70.             admin.createTable(hTableDescriptor);  
  71.         }  
  72.         close();  
  73.     }  
  74.   
  75.     //删表  
  76.     public static void deleteTable(String tableName) throws IOException {  
  77.         init();  
  78.         TableName tn = TableName.valueOf(tableName);  
  79.         if (admin.tableExists(tn)) {  
  80.             admin.disableTable(tn);  
  81.             admin.deleteTable(tn);  
  82.         }  
  83.         close();  
  84.     }  
  85.   
  86.     //查看已有表  
  87.     public static void listTables() throws IOException {  
  88.         init();  
  89.         HTableDescriptor hTableDescriptors[] = admin.listTables();  
  90.         for(HTableDescriptor hTableDescriptor :hTableDescriptors){  
  91.             System.out.println(hTableDescriptor.getNameAsString());  
  92.         }  
  93.         close();  
  94.     }  
  95.   
  96.     //插入数据  
  97.     public static void insterRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException {  
  98.         init();  
  99.         Table table = connection.getTable(TableName.valueOf(tableName));  
  100.         Put put = new Put(Bytes.toBytes(rowkey));  
  101.         put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));  
  102.         table.put(put);  
  103.   
  104.         //批量插入  
  105.        /* List<Put> putList = new ArrayList<Put>(); 
  106.         puts.add(put); 
  107.         table.put(putList);*/  
  108.         table.close();  
  109.         close();  
  110.     }  
  111.   
  112.     //删除数据  
  113.     public static void deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException {  
  114.         init();  
  115.         Table table = connection.getTable(TableName.valueOf(tableName));  
  116.         Delete delete = new Delete(Bytes.toBytes(rowkey));  
  117.         //删除指定列族  
  118.         //delete.addFamily(Bytes.toBytes(colFamily));  
  119.         //删除指定列  
  120.         //delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));  
  121.         table.delete(delete);  
  122.         //批量删除  
  123.        /* List<Delete> deleteList = new ArrayList<Delete>(); 
  124.         deleteList.add(delete); 
  125.         table.delete(deleteList);*/  
  126.         table.close();  
  127.         close();  
  128.     }  
  129.   
  130.     //根据rowkey查找数据  
  131.     public static void getData(String tableName,String rowkey,String colFamily,String col)throws  IOException{  
  132.         init();  
  133.         Table table = connection.getTable(TableName.valueOf(tableName));  
  134.         Get get = new Get(Bytes.toBytes(rowkey));  
  135.         //获取指定列族数据  
  136.         //get.addFamily(Bytes.toBytes(colFamily));  
  137.         //获取指定列数据  
  138.         //get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));  
  139.         Result result = table.get(get);  
  140.   
  141.         showCell(result);  
  142.         table.close();  
  143.         close();  
  144.     }  
  145.   
  146.     //格式化输出  
  147.     public static void showCell(Result result){  
  148.         Cell[] cells = result.rawCells();  
  149.         for(Cell cell:cells){  
  150.             System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");  
  151.             System.out.println("Timetamp:"+cell.getTimestamp()+" ");  
  152.             System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");  
  153.             System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");  
  154.             System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");  
  155.         }  
  156.     }  
  157.   
  158.     //批量查找数据  
  159.     public static void scanData(String tableName,String startRow,String stopRow)throws IOException{  
  160.         init();  
  161.         Table table = connection.getTable(TableName.valueOf(tableName));  
  162.         Scan scan = new Scan();  
  163.         //scan.setStartRow(Bytes.toBytes(startRow));  
  164.         //scan.setStopRow(Bytes.toBytes(stopRow));  
  165.         ResultScanner resultScanner = table.getScanner(scan);  
  166.         for(Result result : resultScanner){  
  167.             showCell(result);  
  168.         }  
  169.         table.close();  
  170.         close();  
  171.     }  
  172. }  
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
    3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    5.     <modelVersion>4.0.0</modelVersion>  
    6.   
    7.     <groupId>td</groupId>  
    8.     <artifactId>hbase_test</artifactId>  
    9.     <version>1.0</version>  
    10.   
    11.     <properties>  
    12.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    13.        <!-- <hadoop.version>2.6.0-cdh5.4.5</hadoop.version>-->  
    14.         <hbase.version>1.1.2</hbase.version>  
    15.     </properties>  
    16.   
    17.     <dependencies>  
    18.         <dependency>  
    19.             <groupId>org.apache.hbase</groupId>  
    20.             <artifactId>hbase-client</artifactId>  
    21.             <version>${hbase.version}</version>  
    22.         </dependency>  
    23.     </dependencies>  
    24.   
    25. </project

posted on 2016-11-23 21:21  1130136248  阅读(272)  评论(0)    收藏  举报

导航