hbase java api

pom.xml

 

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>1.3.1</version>
        </dependency>

 

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;

public class HbaseDemo {
    private static Configuration conf = HBaseConfiguration.create();
    private static Admin admin;
 
    static {
        conf.set("hbase.rootdir", "hdfs://master01:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "master01,slave01,slave02");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
 
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
            System.err.println(admin.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //    创建表,可以同时创建多个列族
    public void createTable(String tableName, String... columnFamily) {
        TableName tableNameObj = TableName.valueOf(tableName);
        try {
            if (admin.tableExists(tableNameObj)) {
                System.out.println("Table: " + tableName + " already exists!");
            } else {
                HTableDescriptor tb = new HTableDescriptor(tableNameObj);
                for (int i = 0; i < columnFamily.length; i++) {
                    HColumnDescriptor family = new HColumnDescriptor(columnFamily[i]);
                    tb.addFamily(family);
                }
                admin.createTable(tb);
                System.out.println(tableName + "创建成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(tableName + "创建失败");
        }
    }
 
    //    删除表
    public void delTable(String tableName) {
        TableName tableNameObj = TableName.valueOf(tableName);
        try {
            if (admin.tableExists(tableNameObj)) {
                admin.disableTable(tableNameObj);
                admin.deleteTable(tableNameObj);
                System.out.println(tableName + " 删除成功");
            } else {
                System.out.println(tableName + " 表不存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("删除失败");
        }
    }
 
    //    插入数据
    public void insertRecord(String tableName, String row, String columnFamily, String qualifier, String value) {
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Put put = new Put(row.getBytes());
            put.addColumn(columnFamily.getBytes(), qualifier.getBytes(), value.getBytes());
            table.put(put);
            table.close();
            connection.close();
            System.out.println(tableName + " 表插入成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("插入失败");
        }
    }
 
    //    删除数据
    public void deleteRecord(String tableName, String row, String columnFamily, String qualifier) {
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Delete delete = new Delete(row.getBytes());
            delete.addColumn(columnFamily.getBytes(), qualifier.getBytes());
            table.delete(delete);
            table.close();
            connection.close();
            System.out.println(tableName + " 表删除成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("删除失败");
        }
    }
 
    //    获取某一行的数据
    public void getOneRecord(String tableName, String row) {
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Get get = new Get(row.getBytes());
            Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                System.out.println(new String(result.getRow()) + ":" + new String(CellUtil.cloneFamily(cell)) + ":"
                        + new String(CellUtil.cloneQualifier(cell)) + ":" + new String(CellUtil.cloneValue(cell)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //    获取表中的所有数据
    public void getAll(String tableName) {
        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            ResultScanner results = table.getScanner(scan);
            for (Result result : results) {
                for (Cell cell : result.rawCells()) {
                    System.out.println(new String(result.getRow()) + ":" + new String(CellUtil.cloneFamily(cell)) + ":"
                            + new String(CellUtil.cloneQualifier(cell)) + ":" + new String(CellUtil.cloneValue(cell)));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        HbaseDemo hbaseDemo = new HbaseDemo();
        //hbaseDemo.createTable("userinfo", "cf1", "cf2");
       // hbaseDemo.delTable("userinfo");
//        for(int i=0;i<10;i++) {
//            hbaseDemo.insertRecord("userinfo","001","baseinfo","age"+i,"18");
//        }
        //hbaseDemo.deleteRecord("userinfo", "001", "baseinfo", "age");
          hbaseDemo.getOneRecord("userinfo", "001");
       // hbaseDemo.getAll("userinfo");
    }
}

 

posted @ 2018-12-28 15:34  冬天不眠  阅读(149)  评论(0编辑  收藏  举报