【六】java代码操作Hbase

package cn.com.hbasedemo.hbasedemo;

import java.io.IOException;
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.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;

/**
 * <dependency>
 *   <groupId>org.apache.hbase</groupId>
 *   <artifactId>hbase</artifactId>
 *   <version>0.94.7</version>
 * </dependency>
 * Hello   world!  

     本代码测试通过,使用的hbase版本是0.94.7,通过maven进行jar包管理的
 *
 */
public class App {
 public static void main(String[] args) throws Exception {

        System.out.println("start create table ......");
  
        listTables();
  
        createTable("java_test");
  
      insertData("java_test");

    

      QueryAll("java_test");
//  
//  deleteRow("java_test", "112233bbbcccc") ;
//  
//  dropTable("java_test");
 }

 public static Configuration configuration;
 static {
  configuration = HBaseConfiguration.create();
  configuration.set("hbase.zookeeper.property.clientPort", "2181");
  configuration.set("hbase.zookeeper.quorum", "hadoop0");
  configuration.set("hbase.master", "hadoop0:60000");
 }
 
 /**
  * 列出系统中所有的表
  * @throws IOException
  */
 public static void listTables() throws IOException {
  HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
  System.out.println("start hBaseAdmin ......" + hBaseAdmin);
  HTableDescriptor[] tableNames = hBaseAdmin.listTables();
  for (HTableDescriptor tableName : tableNames) {
   System.out.println(tableName.getNameAsString());
  }
 }

 /**
  * 创建表
  * @param tableName
  * @throws IOException
  */
 public static void createTable(String tableName) throws IOException {
  System.out.println("start create table ......");

  HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
  if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
   hBaseAdmin.disableTable(tableName);
   hBaseAdmin.deleteTable(tableName);
   System.out.println(tableName + " is exist,detele....");
  }
  HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
  tableDescriptor.addFamily(new HColumnDescriptor("name"));
  tableDescriptor.addFamily(new HColumnDescriptor("age"));
  tableDescriptor.addFamily(new HColumnDescriptor("address"));
  hBaseAdmin.createTable(tableDescriptor);
  System.out.println("end create table ......");
 }

 /**
  * 插入数据
  * @param tableName
  * @throws IOException
  */
 public static void insertData(String tableName) throws IOException {
  System.out.println("start insert data ......");
  HTable table = new HTable(configuration, tableName);
  // 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
  Put put = new Put("20083051201".getBytes());
  put.add("name".getBytes(), null, "丽丽".getBytes());// 本行数据的第一列
  put.add("age".getBytes(), null, "24".getBytes());// 本行数据的第三列
  put.add("address".getBytes(), null, "华北航天工业学院1".getBytes());// 本行数据的第三列
  
  //增加一个sex列
  HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
  hBaseAdmin.disableTable(tableName);
  HColumnDescriptor columnDescriptor = new HColumnDescriptor("sex");
  hBaseAdmin.addColumn(tableName, columnDescriptor);
  hBaseAdmin.enableTable(tableName);
  // 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
  Put put1 = new Put("2008305120X".getBytes());
  put1.add("name".getBytes(), null, "张3丰".getBytes());// 本行数据的第一列
  put1.add("age".getBytes(), null, "22".getBytes());// 本行数据的第三列
  put1.add("address".getBytes(), null, "华北航天工业学院".getBytes());// 本行数据的第三列
  put1.add("sex".getBytes(), null, "男".getBytes());// 本行数据的第三列
  
  try {
   table.put(put);
   table.put(put1);
  } catch (Exception e) {
   e.printStackTrace();
  }
  System.out.println("end insert data ......");
 }

 /**
  * 删除表
  * @param tableName
  */
 public static void dropTable(String tableName) {
  try {
   HBaseAdmin admin = new HBaseAdmin(configuration);
   admin.disableTable(tableName);
   admin.deleteTable(tableName);
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 /**
  * 删除行
  * @param tablename
  * @param rowkey
  */
 @SuppressWarnings({ "rawtypes", "unchecked" })
 public static void deleteRow(String tablename, String rowkey) {
  try {
   HTable table = new HTable(configuration, tablename);
   List list = new ArrayList();
   Delete d1 = new Delete(rowkey.getBytes());
   list.add(d1);

   table.delete(list);
   System.out.println("删除行成功!");

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 /**
  * 查询表中的数据
  * @param tableName
  * @throws IOException
  */
 public static void QueryAll(String tableName) throws IOException {

  HTable table = new HTable(configuration, tableName);
  try {
   ResultScanner rs = table.getScanner(new Scan());
   for (Result r : rs) {
    System.out.println("rowkey:" + new String(r.getRow()));
    for (KeyValue keyValue : r.raw()) {
     System.out.println(new String(keyValue.getFamily())
       + "====:" + new String(keyValue.getValue()));
    }
    System.out.println("");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

相关推荐