日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

大數(shù)據(jù)存儲:HBase API,DDL,DML創(chuàng)建刪除操作

 

大數(shù)據(jù)存儲: HBase API,DDL,DML

4.1 環(huán)境準(zhǔn)備

新建項目后在pom.xml中添加依賴:

<dependency>
    <groupId>org.Apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>2.0.5</version>
</dependency>

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

4.2 DDL

創(chuàng)建HBase_DDL類

4.2.1 判斷表是否存在

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import JAVA.io.IOException;

public class HBase_DDL {

    //TODO 判斷表是否存在
    public static boolean isTableExist(String tableName) throws IOException {

        //1.創(chuàng)建配置信息并配置
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //2.獲取與HBase的連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //3.獲取DDL操作對象
        Admin admin = connection.getAdmin();

        //4.判斷表是否存在操作
        boolean exists = admin.tableExists(TableName.valueOf(tableName));

        //5.關(guān)閉連接
        admin.close();
        connection.close();

        //6.返回結(jié)果
        return exists;
    }

}

4.2.2 創(chuàng)建表

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBase_DDL {

    //TODO 創(chuàng)建表
    public static void createTable(String tableName, String... cfs) throws IOException {

        //1.判斷是否存在列族信息
        if (cfs.length <= 0) {
            System.out.println("請設(shè)置列族信息!");
            return;
        }

        //2.判斷表是否存在
        if (isTableExist(tableName)) {
            System.out.println("需要創(chuàng)建的表已存在!");
            return;
        }

        //3.創(chuàng)建配置信息并配置
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //4.獲取與HBase的連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //5.獲取DDL操作對象
        Admin admin = connection.getAdmin();

        //6.創(chuàng)建表描述器構(gòu)造器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));

        //7.循環(huán)添加列族信息
        for (String cf : cfs) {
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        }

        //8.執(zhí)行創(chuàng)建表的操作
        admin.createTable(tableDescriptorBuilder.build());

        //9.關(guān)閉資源
        admin.close();
        connection.close();
    }

}

4.2.3 刪除表

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBase_DDL {

    //TODO 刪除表
    public static void dropTable(String tableName) throws IOException {

        //1.判斷表是否存在
        if (!isTableExist(tableName)) {
            System.out.println("需要刪除的表不存在!");
            return;
        }

        //2.創(chuàng)建配置信息并配置
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //3.獲取與HBase的連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //4.獲取DDL操作對象
        Admin admin = connection.getAdmin();

        //5.使表下線
        TableName name = TableName.valueOf(tableName);
        admin.disableTable(name);

        //6.執(zhí)行刪除表操作
        admin.deleteTable(name);

        //7.關(guān)閉資源
        admin.close();
        connection.close();
    }

}

4.2.4 創(chuàng)建命名空間

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBase_DDL {

    //TODO 創(chuàng)建命名空間
    public static void createNameSpace(String ns) throws IOException {

        //1.創(chuàng)建配置信息并配置
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //2.獲取與HBase的連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //3.獲取DDL操作對象
        Admin admin = connection.getAdmin();

        //4.創(chuàng)建命名空間描述器
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();

        //5.執(zhí)行創(chuàng)建命名空間操作
        try {
            admin.createNamespace(namespaceDescriptor);
        } catch (NamespaceExistException e) {
            System.out.println("命名空間已存在!");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //6.關(guān)閉連接
        admin.close();
        connection.close();

    }
}

4.3 DML

創(chuàng)建類HBase_DML

4.3.1 插入數(shù)據(jù)

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

import java.io.IOException;

public class HBase_DML {

    //TODO 插入數(shù)據(jù)
    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {

        //1.獲取配置信息并設(shè)置連接參數(shù)
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //2.獲取連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //3.獲取表的連接
        Table table = connection.getTable(TableName.valueOf(tableName));

        //4.創(chuàng)建Put對象
        Put put = new Put(Bytes.toBytes(rowKey));

        //5.放入數(shù)據(jù)
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));

        //6.執(zhí)行插入數(shù)據(jù)操作
        table.put(put);

        //7.關(guān)閉連接
        table.close();
        connection.close();
    }

}

4.3.2 單條數(shù)據(jù)查詢

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

import java.io.IOException;

public class HBase_DML {

    //TODO 單條數(shù)據(jù)查詢(GET)
    public static void getDate(String tableName, String rowKey, String cf, String cn) throws IOException {

        //1.獲取配置信息并設(shè)置連接參數(shù)
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //2.獲取連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //3.獲取表的連接
        Table table = connection.getTable(TableName.valueOf(tableName));

        //4.創(chuàng)建Get對象
        Get get = new Get(Bytes.toBytes(rowKey));
        // 指定列族查詢
        // get.addFamily(Bytes.toBytes(cf));
        // 指定列族:列查詢
        // get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //5.查詢數(shù)據(jù)
        Result result = table.get(get);

        //6.解析result
        for (Cell cell : result.rawCells()) {
            System.out.println("CF:" + Bytes.toString(cell.getFamilyArray()) +
                    ",CN:" + Bytes.toString(cell.getQualifierArray()) +
                    ",Value:" + Bytes.toString(cell.getValueArray()));
        }

        //7.關(guān)閉連接
        table.close();
        connection.close();

    }

}

4.3.3 掃描數(shù)據(jù)

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

import java.io.IOException;

public class HBase_DML {

    //TODO 掃描數(shù)據(jù)(Scan)
    public static void scanTable(String tableName) throws IOException {

        //1.獲取配置信息并設(shè)置連接參數(shù)
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //2.獲取連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //3.獲取表的連接
        Table table = connection.getTable(TableName.valueOf(tableName));

        //4.創(chuàng)建Scan對象
        Scan scan = new Scan();

        //5.掃描數(shù)據(jù)
        ResultScanner results = table.getScanner(scan);

        //6.解析results
        for (Result result : results) {
            for (Cell cell : result.rawCells()) {
                System.out.println("CF:" + Bytes.toString(cell.getFamilyArray()) +
                        ",CN:" + Bytes.toString(cell.getQualifierArray()) +
                        ",Value:" + Bytes.toString(cell.getValueArray()));
            }
        }

        //7.關(guān)閉資源
        table.close();
        connection.close();

    }

}

4.3.4 刪除數(shù)據(jù)

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

import java.io.IOException;

public class HBase_DML {

    //TODO 刪除數(shù)據(jù)
    public static void deletaData(String tableName, String rowKey, String cf, String cn) throws IOException {

        //1.獲取配置信息并設(shè)置連接參數(shù)
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        //2.獲取連接
        Connection connection = ConnectionFactory.createConnection(configuration);

        //3.獲取表的連接
        Table table = connection.getTable(TableName.valueOf(tableName));

        //4.創(chuàng)建Delete對象
        Delete delete = new Delete(Bytes.toBytes(rowKey));

        // 指定列族刪除數(shù)據(jù)
        // delete.addFamily(Bytes.toBytes(cf));
        // 指定列族:列刪除數(shù)據(jù)(所有版本)
        // delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
        // 指定列族:列刪除數(shù)據(jù)(指定版本)
        // delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //5.執(zhí)行刪除數(shù)據(jù)操作
        table.delete(delete);

        //6.關(guān)閉資源
        table.close();
        connection.close();

}

}

你的贊,我都當(dāng)成喜歡。

專注分享大數(shù)據(jù)技術(shù)&智能技術(shù)&基礎(chǔ)&實戰(zhàn),干貨,資料。

關(guān)注本號,讓更多人了解技術(shù),讓技術(shù)造福更多人。歡迎轉(zhuǎn)發(fā)傳播,感謝您的關(guān)注,謝謝。

分享到:
標(biāo)簽:數(shù)據(jù)存儲
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定