1. 程式人生 > 其它 >使用Java api對HBase 2.4.5進行增刪改查

使用Java api對HBase 2.4.5進行增刪改查

1.執行hbase

2.新建maven專案

2.將hbase-site.xml放在專案的resources資料夾下

3.修改pom.xml檔案,引入hbase相關資源

  <repositories><!-- 程式碼庫 -->
    <repository>
      <id>maven-ali</id>
      <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
      <releases>
        <enabled
>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> </repositories> <
dependencies> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase</artifactId> <version>2.4.5</version> <type>pom</type> </dependency
> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>2.4.5</version> </dependency> <!-- jstl標籤庫--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.3.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>

4.執行java程式

package test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HBaseDemo {
    private static Configuration conf = HBaseConfiguration.create();
    private static Admin admin;

    static {
        conf.set("hbase.rootdir", "hdfs://node01:8020/hbase");
        conf.set("hbase.zookeeper.quorum", "node01,node02,node03");

        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        HBaseDemo hBaseDemo = new HBaseDemo();
        //hBaseDemo.createTable();//建表
        //hBaseDemo.createTable2("test","f1","f2");//建表(制定行鍵)
        //hBaseDemo.addData();//插入資料
        //hBaseDemo.insertBatchData("test");//插入多個數據
        //hBaseDemo.getData();//通過Get查詢單行資料,注意列值的型別
        //hBaseDemo.scanAllData("test");//通過Scan方法掃描全表
        //hBaseDemo.deleteData();//刪除表資料
        //hBaseDemo.deleteTable("test");//刪除表
    }


    public void createTable2(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 createTable(String tableName) throws IOException {
        //連線hbase叢集
        Configuration configuration = HBaseConfiguration.create();
        //指定hbase的zk連線地址
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取管理員物件
        Admin admin = connection.getAdmin();
        //通過管理員物件建立表
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        //給表新增列族
        HColumnDescriptor f1 = new HColumnDescriptor("f1");
        HColumnDescriptor f2 = new HColumnDescriptor("f2");
        //將兩個列族設定到 建立的表中
        hTableDescriptor.addFamily(f1);
        hTableDescriptor.addFamily(f2);
        //建立表
        admin.createTable(hTableDescriptor);
        //關閉連線
        admin.close();
        connection.close();
        System.out.println("建立表成功");
    }

    public void addData(String tableName) throws IOException {
        //獲取連線
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取表物件
        Table myuser = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put("0001".getBytes());
        put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("小李子"));
        put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(18));
        put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("火星人"));
        put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("13033607199"));
        put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("啦啦啦啦"));
        myuser.put(put);
        //關閉表
        myuser.close();
        System.out.println("插入資料成功");
    }

    public void insertBatchData(String tableName) throws IOException {
        //獲取連線
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取表
        Table myuser = connection.getTable(TableName.valueOf(tableName));
        //建立put物件,並指定rowkey
        Put put = new Put("0002".getBytes());
        put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
        put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
        put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛國譙縣"));
        put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
        put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));

        Put put2 = new Put("0003".getBytes());
        put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
        put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("劉備"));
        put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
        put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿縣"));
        put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
        put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));

        Put put3 = new Put("0004".getBytes());
        put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
        put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孫權"));
        put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
        put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
        put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
        put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));

        Put put4 = new Put("0005".getBytes());
        put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
        put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("諸葛亮"));
        put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
        put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
        put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出師表你背了嘛"));

        Put put5 = new Put("0006".getBytes());
        put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司馬懿"));
        put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
        put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪裡人有待考究"));
        put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
        put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟諸葛亮死掐"));

        Put put6 = new Put("0007".getBytes());
        put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—呂布"));
        put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("內蒙人"));
        put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
        put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蟬去哪了"));

        List<Put> listPut = new ArrayList<Put>();
        listPut.add(put);
        listPut.add(put2);
        listPut.add(put3);
        listPut.add(put4);
        listPut.add(put5);
        listPut.add(put6);

        myuser.put(listPut);
        myuser.close();
        System.out.println("插入多個數據成功");
    }


    public void getData(String tableName) throws IOException {
        //獲取連線
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);

        //獲取表物件
        Table myuser = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get("0005".getBytes());
        get.addColumn("f2".getBytes(),"address".getBytes());//列族;列名

        //獲取返回結果
        Result result = myuser.get(get);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            //獲取列族的名稱
            String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
            //獲取列的名稱
            String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
            if ( familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")){
                int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
            } else {
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
            }
        }
        //關閉表
        myuser.close();
    }

    public void scanAllData(String tableName) throws IOException {
        //獲取連線
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取表物件
        Table myuser = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        //設定起始和結束的rowkey,掃描結果是:[)型別
        scan.setStartRow("0001".getBytes());
        scan.setStopRow("0008".getBytes());
        ResultScanner scanner = myuser.getScanner(scan);
        for (Result result : scanner) {
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                //獲取列族的名稱
                String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                //獲取列的名稱
                String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                if ( familyName.equals("f1") && columnName.equals("id") || columnName.equals("age")){
                    int value = Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                    System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
                } else {
                    String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                    System.out.println("列族名: " + familyName + " ,列名: " + columnName + " ,列值:" + value);
                }
            }
        }
        //獲取返回結果
        myuser.close();
    }

    public void deleteData(String talbeName) throws IOException {
        //獲取連線
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取表物件
        Table myuser = connection.getTable(TableName.valueOf(talbeName));

        Delete delete = new Delete("0007".getBytes());
        myuser.delete(delete);

        myuser.close();

        System.out.println("刪除成功");
    }

    public void deleteTable(String tableName) throws IOException {

        //獲取連線
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        //獲取管理員物件
        Admin admin = connection.getAdmin();
        //禁用表
        admin.disableTable(TableName.valueOf(tableName));
        //刪除表
        admin.deleteTable(TableName.valueOf(tableName));
        System.out.println("刪除表成功");
    }
}