使用 Java API 实现增删操作

任务描述

本关任务:使用 Java API 删除表 exam_tb2,创建表 exam_tb3 并添加如下表中数据。

user_infoaddress_info
nameageaddressemailphone
1Avatar100pandoraavatar@163.com123456
2change50moonchange@163.com234567
3nezha6earthnezha@163.com

345678

编程要求

实现删除表 exam_tb2,创建表 exam_tb3 并添加数据。

代码实现

package com.yy;

import org.apache.hadoop.conf.Configuration;
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.util.ArrayList;
import java.util.List;
import java.io.IOException;

public class HbaseUtil  {

    private static Admin admin = null;
    private static Connection connection = null;
    private static Configuration conf = null;

    static {
        // HBase配置文件
        conf = HBaseConfiguration.create();
        // 获取连接对象
        try {
            connection = ConnectionFactory.createConnection(conf);
            // 获取HBase管理员对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void close(Connection conn, Admin admin) throws IOException {
        if (conn != null) {
            conn.close();
        }
        if (admin != null) {
            admin.close();
        }
    }

    //删除exam_tb2表
    public void deleteTable() throws IOException {
    /**********Begin**********/
    TableName tableName = TableName.valueOf("exam_tb2");
    if (admin.tableExists(tableName)) {
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }
    /**********End**********/
      
        
	}

    //创建exam_tb3表
    public void createTab() throws IOException {

    /**********Begin**********/
    TableName tableName = TableName.valueOf("exam_tb3");
    if (!admin.tableExists(tableName)) {
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("user_info".getBytes());
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("address_info".getBytes());
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        admin.createTable(tableDescriptorBuilder.build());
    }

    /**********End**********/

    
    }
    //往exam_tb3表中添加数据
    public void  putBatch() throws IOException {
    /**********Begin**********/
    TableName tableName = TableName.valueOf("exam_tb3");
    Table table = connection.getTable(tableName);
    List<Put> puts = new ArrayList<>();
    Put put1 = new Put(Bytes.toBytes("1"));
    put1.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("Avatar"));
    put1.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("100"));
    put1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("pandora"));
    put1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("avatar@163.com"));
    put1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("123456"));
    puts.add(put1);

    Put put2 = new Put(Bytes.toBytes("2"));
    put2.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("change"));
    put2.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("50"));
    put2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("moon"));
    put2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("change@163.com"));
    put2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("234567"));
    puts.add(put2);

    Put put3 = new Put(Bytes.toBytes("3"));
    put3.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("nezha"));
    put3.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("6"));
    put3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("earth"));
    put3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("nezha@163.com"));
    put3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("345678"));
    puts.add(put3);

    table.put(puts);
    table.close();

    /**********End**********/ 

   }


}

运行结果

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值