12月7日总结

实验3
熟悉常用的HBase操作

1.实验目的
(1)理解HBase在Hadoop体系结构中的角色;
(2)熟练使用HBase操作常用的Shell命令;
(3)熟悉HBase操作常用的Java API。
2.实验平台
(1)操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04);
(2)Hadoop版本:3.1.3;
(3)HBase版本:2.2.2;
(4)JDK版本:1.8;
(5)Java IDE:Eclipse。
3. 实验步骤
(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
(1)列出HBase所有的表的相关信息,例如表名;
(2)在终端打印出指定的表的所有记录数据;
(3)向已经创建好的表添加和删除指定的列族或列;
(4)清空指定的表的所有记录数据;
(5)统计表的行数。

(二)HBase数据库操作

  1. 现有以下关系型数据库中的表和数据(见表14-3到表14-5),要求将其转换为适合于HBase存储的表并插入数据:
    表14-3 学生表(Student)
    学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age)
    2015001 Zhangsan male 23
    2015002 Mary female 22
    2015003 Lisi male 24

表14-4 课程表(Course)
课程号(C_No) 课程名(C_Name) 学分(C_Credit)
123001 Math 2.0
123002 Computer Science 5.0
123003 English 3.0

表14-5 选课表(SC)
学号(SC_Sno) 课程号(SC_Cno) 成绩(SC_Score)
2015001 123001 86
2015001 123003 69
2015002 123002 77
2015002 123003 99
2015003 123001 98
2015003 123002 95

  1. 请编程实现以下功能:
    (1)createTable(String tableName, String[] fields)
    创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
    (2)addRecord(String tableName, String row, String[] fields, String[] values)
    向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。
    (3)scanColumn(String tableName, String column)
    浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
    (4)modifyData(String tableName, String row, String column)
    修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
    (5)deleteRow(String tableName, String row)
    删除表tableName中row指定的行的记录。

4.实验报告
题目: 熟悉常用的HBase操作 姓名 刘梦阳 日期2023.11.18
实验环境:
(1)操作系统:Linux(centOS);
(2)Hadoop版本:3.1.3;
(3)HBase版本:2.2.2;
(4)JDK版本:1.8;
(5)Java IDE:IDEA。

实验内容与完成情况:
(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
(1)列出HBase所有的表的相关信息,例如表名;
shell命令

java代码
package HbaseAPI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.
;
import java.io.IOException;
import java.util.List;
public class shiyan11 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

//建立连接
public static void init(){
    configuration  = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

    try{
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }catch (IOException e){
        e.printStackTrace();
    }
}
//关闭连接
public static void close(){
    try{
        if(admin != null){
            admin.close();
        }
        if(null != connection){
            connection.close();
        }
    }catch (IOException e){
        e.printStackTrace();
    }
}

//(1)列出HBase所有的表的相关信息,例如表名、创建时间等
public static void listTables(String stu) throws IOException {
    init();//建立连接
    List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
    for(TableDescriptor tableDescriptor : tableDescriptors){
        TableName tableName = tableDescriptor.getTableName();
        System.out.println("Table:" + tableName);
    }
    close();//关闭连接
}

public static void main(String[] args) throws IOException {
listTables("s1");
}
}
运行截图

(2)在终端打印出指定的表的所有记录数据;
Hbase shell

java代码
package HbaseAPI;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.;
import org.apache.hadoop.hbase.client.
;
import java.util.Scanner;
public class Test_2 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

/*** @paramargs
 */

//建立连接
public static void init() {
configuration = HBaseConfiguration.create();
// configuration.set("hbase.rootdir", "hdfs://node1:8020/hbase");
configuration.set("hbase.zookeeper.quorum","node1,node2,node3");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}

//关闭连接
public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/*** 根据表名查找表信息
 */
public static void getData(String tableName) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);

    for (Result result : scanner) {
        showCell((result));
    }
    close();
}

/*** 格式化输出
 * @paramresult
 */
public static void showCell(Result result) {
    Cell[] cells = result.rawCells();
    for (Cell cell : cells) {
        System.out.println("RowName(行键):" + new String(CellUtil.cloneRow(cell)) + " ");
        System.out.println("Timetamp(时间戳):" + cell.getTimestamp() + " ");
        System.out.println("column Family(列簇):" + new String(CellUtil.cloneFamily(cell)) + " ");
        System.out.println("column Name(列名):" + new String(CellUtil.cloneQualifier(cell)) + " ");
        System.out.println("value:(值)" + new String(CellUtil.cloneValue(cell)) + " ");
        System.out.println();
    }
}


public static void main(String[] args) throws IOException {
    //TODO Auto-generated method stub
    Test_2 t = new Test_2();
    System.out.println("请输入要查看的表名");
    Scanner scan = new Scanner(System.in);
    String tableName = scan.nextLine();
    System.out.println("信息如下:");
    t.getData(tableName);

}

}

运行截图

(3)向已经创建好的表添加和删除指定的列族或列;
先在Shell中创建表s1,作为示例表,命令如下:

然后,可以在s1中添加数据,命令如下:

之后,可以执行如下命令删除指定的列:

java代码
package HbaseAPI;

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;
/**

    1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
      *(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;
      *(2) 在终端打印出指定的表的所有记录数据;
      *(3) 向已经创建好的表添加和删除指定的列族或列;
      *(4) 清空指定的表的所有记录数据;
      *(5) 统计表的行数。
      */
      public class shiyan13 {
      public static Configuration configuration;
      public static Connection connection;
      public static Admin admin;

    //建立连接
    public static void init(){
    configuration = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

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

    }
    //关闭连接
    public static void close(){
    try{
    if(admin != null){
    admin.close();
    }
    if(null != connection){
    connection.close();
    }
    }catch (IOException e){
    e.printStackTrace();
    }
    }
    //(3)想已经创建好的表添加和删除指定的列族或列
    //向表添加数据
    public static void insterRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Put put = new Put(rowKey.getBytes());
    put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
    table.put(put);
    table.close();
    close();
    }
    //删除数据
    public static void deleRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Delete delete = new Delete(rowKey.getBytes());
    //删除指定列族
    delete.addFamily(Bytes.toBytes(colFamily));
    //删除指定列
    delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
    table.delete(delete);
    table.close();
    close();
    }
    public static void main(String[] args) throws IOException {
    insterRow("s1","1001","score","english","33");
    deleRow("s1","zhangsan","score","Math");
    }
    }

运行截图

(4)清空指定的表的所有记录数据;

Java代码
package homework;

import java.io.IOException;
import java.util.Scanner;

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.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.HBaseAdmin;
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;
import org.apache.hadoop.hbase.util.Bytes;

public class Test_4 {

public static Configuration configuration;
public static Connection connection;
public static Admin admin;

/*** @paramargs
 */

//建立连接
public static void init() {
configuration = HBaseConfiguration.create();
// configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
configuration.set("hbase.zookeeper.quorum","node1,node2,node3");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}

//关闭连接
public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}


/*** 清空制定的表的所有记录数据
 * @paramargs
 * @throwsIOException
 */
public static void clearRows(String tableName) throws IOException {

    init();
    HBaseAdmin admin1 = (HBaseAdmin) connection.getAdmin();
    HTableDescriptor tDescriptor = admin1.getTableDescriptor(TableName.valueOf(Bytes.toBytes(tableName)));//读取了之前表的表名 列簇等信息,然后再进行删除操作。 总思想是先将原表结构保留下来,然后进行删除,再重新依据保存的信息重新创建表。
    TableName tablename = TableName.valueOf(tableName);

    //删除表
    admin.disableTable(tablename);
    admin.deleteTable(tablename);

    //重新建表
    admin.createTable(tDescriptor);
    close();

}


/*** 根据表名查找表信息
 */
public static void getData(String tableName) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);

    for (Result result : scanner) {
        showCell((result));
    }
    close();
}

/*** 格式化输出
 * @paramresult
 */
public static void showCell(Result result) {
    Cell[] cells = result.rawCells();
    for (Cell cell : cells) {
        System.out.println("RowName(行键):" + new String(CellUtil.cloneRow(cell)) + " ");
        System.out.println("Timetamp(时间戳):" + cell.getTimestamp() + " ");
        System.out.println("column Family(列簇):" + new String(CellUtil.cloneFamily(cell)) + " ");
        System.out.println("column Name(列名):" + new String(CellUtil.cloneQualifier(cell)) + " ");
        System.out.println("value:(值)" + new String(CellUtil.cloneValue(cell)) + " ");
        System.out.println();
    }
}

public static void main(String[] args) {
    //TODO Auto-generated method stub
    Test_4 test_4 = new Test_4();

    Scanner scan = new Scanner(System.in);
    System.out.println("请输入要清空的表名");
    String tableName = scan.nextLine();

    try {
        System.out.println("表原来的信息:");
        test_4.getData(tableName);
        test_4.clearRows(tableName);
        System.out.println("表已清空:");
    } catch (IOException e) {
        //TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

运行截图

(5)统计表的行数。
Hbase Shell

  Java代码

package homework;

import java.io.IOException;
import java.util.Scanner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;

public class Test_5 {

public static Configuration configuration;
public static Connection connection;
public static Admin admin;

//建立连接
public static void init() {
    configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");
    try {
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//关闭连接
public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void countRows(String tableName) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);
    int num = 0;
    for (Result result = scanner.next(); result != null; result = scanner.next()) {
        num++;
    }
    System.out.println("行数:" + num);
    scanner.close();
    close();
}

/*** @paramargs
 * @throwsIOException
 */
public static void main(String[] args) throws IOException {
    //TODO Auto-generated method stub
    Test_5 test_5 = new Test_5();
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入要统计行数的表名");
    String tableName = scan.nextLine();
    test_5.countRows(tableName);
}

}

 运行截图

(二)HBase数据库操作

  1. 现有以下关系型数据库中的表和数据(见表14-3到表14-5),要求将其转换为适合于HBase存储的表并插入数据:
    表14-3 学生表(Student)
    学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age)
    2015001 Zhangsan male 23
    2015002 Mary female 22
    2015003 Lisi male 24

表14-4 课程表(Course)
课程号(C_No) 课程名(C_Name) 学分(C_Credit)
123001 Math 2.0
123002 Computer Science 5.0
123003 English 3.0

表14-5 选课表(SC)
学号(SC_Sno) 课程号(SC_Cno) 成绩(SC_Score)
2015001 123001 86
2015001 123003 69
2015002 123002 77
2015002 123003 99
2015003 123001 98
2015003 123002 95

(1)学生Student表
(2)创建表的HBase Shell命令语句如下:

插入数据的HBase Shell命令如下:

(3)课程Course表
(4)创建表的HBase Shell命令语句如下:

插入数据的HBase Shell命令如下:

(5)选课表
(6)创建表的HBase Shell命令语句如下:

插入数据的HBase Shell命令如下:

  1. 请编程实现以下功能:
    (1)createTable(String tableName, String[] fields)
    创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
    Java代码
    package HbaseAPI;
    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.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class CreateTable {

public static Configuration configuration;
public static Connection connection;
public static Admin admin;

public static void createTable(String tableName,String[] fields) throws IOException {

    init();
    TableName tablename = TableName.valueOf(tableName);

    if(admin.tableExists(tablename)){
        System.out.println("table is exists!");
        admin.disableTable(tablename);
        admin.deleteTable(tablename);//删除原来的表
    }

    TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
    for(String str : fields){
        tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build());
        admin.createTable(tableDescriptor.build());
    }
    close();
}

//建立连接
public static void init() {
    configuration = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");
    try {
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
//关闭连接
public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    String[] fields = {"Score"};
    try {
        createTable("person", fields);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

运行截图

(2)addRecord(String tableName, String row, String[] fields, String[] values)
向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。

Java代码

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

public class addRecord {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    for (int i = 0; i != fields.length; i++) {
        Put put = new Put(row.getBytes());
        String[] cols = fields[i].split(":");
        put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
        table.put(put);
    }
    table.close();
    close();
}

public static void init() {
    configuration = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

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

public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};
    String[] values = {"99", "80", "100"};
    try {
        addRecord("person", "Score", fields, values);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

运行截图

(3)scanColumn(String tableName, String column)
浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。

java代码
package HbaseAPI;
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.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class scanColumn {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

public static void scanColumn(String tableName, String column) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Scan scan = new Scan();
    scan.addFamily(Bytes.toBytes(column));
    ResultScanner scanner = table.getScanner(scan);
    for (Result result = scanner.next(); result != null; result = scanner.next()) {
        showCell(result);
    }
    table.close();
    close();
}

public static void showCell(Result result) {
    Cell[] cells = result.rawCells();
    for (Cell cell : cells) {
        System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
        System.out.println("Timetamp:" + cell.getTimestamp() + " ");
        System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
        System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
        System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
    }
}

public static void init() {
    configuration = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

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

// 关闭连接
public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    try {
        scanColumn("person", "Score");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

运行截图

(4)modifyData(String tableName, String row, String column)
修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。

Java代码
package HbaseAPI;
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 java.io.IOException;

public class modifyData {

public static long ts;
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

public static void modifyData(String tableName, String row, String column, String val) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Put put = new Put(row.getBytes());
    Scan scan = new Scan();
    ResultScanner resultScanner = table.getScanner(scan);
    for (Result r : resultScanner) {
        for (Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())) {
            ts = cell.getTimestamp();
        }
    }
    put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
    table.put(put);
    table.close();
    close();
}

public static void init() {
    configuration = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

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

public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    try {
        modifyData("person", "Score", "Math", "33");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

运行截图

(5)deleteRow(String tableName, String row)
删除表tableName中row指定的行的记录。
Java代码
package HbaseAPI;
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 deleteRow {

public static long ts;
public static Configuration configuration;
public static Connection connection;
public static Admin admin;

public static void deleteRow(String tableName, String row) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Delete delete=new Delete(row.getBytes());
    table.delete(delete);
    table.close();
    close();
}

public static void init() {
    configuration = HBaseConfiguration.create();
    //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
    configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

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

public static void close() {
    try {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    try {
        deleteRow("person", "Score");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

运行截图

出现的问题:
问题1: HBase 服务无法启动,或者在连接时遇到问题。
问题2: 在创建表、插入数据或查询数据时遇到问题。
问题3: 在执行某些操作时,遇到权限不足的问题。
问题4: HBase 依赖 ZooKeeper,连接 ZooKeeper 时出现问题。
问题5: 使用的 HBase 版本与其他组件(例如 Hadoop、ZooKeeper)不兼容。
问题6: 数据写入后,读取时出现一致性问题。
解决方案(列出遇到的问题和解决办法,列出没有解决的问题):
解决办法1: 检查 HBase 的日志文件,通常位于 HBase 安装目录下的 logs 文件夹。查看错误信息,可能涉及到端口占用、配置错误等。确保 HBase 配置文件中的参数正确,并检查 HDFS 和 ZooKeeper 等相关服务的状态。
解决办法2: 确保表的定义符合预期,列族、列名等都正确。使用 HBase Shell 或者编程语言的 HBase 客户端 API 进行操作时,检查语法是否正确。在遇到插入或查询数据问题时,确保数据的格式和类型匹配。
解决办法3: 检查 HBase 的权限设置,确保当前用户或应用程序有执行相应操作的权限。有时需要修改 HBase 的权限配置文件,并重新加载配置。
解决办法4: 确保 ZooKeeper 服务正在运行,并且 HBase 配置中的 ZooKeeper 地址是正确的。检查网络配置,确保 HBase 能够访问 ZooKeeper。
解决办法5: 确保你使用的 HBase 版本与其他大数据组件版本兼容。查看官方文档或者社区支持信息,了解组件之间的兼容性。
解决办法6: 确保 HBase 表的副本数量和写入/读取的一致性级别设置正确。了解 HBase 的一致性模型,以便更好地理解数据一致性的保证。

posted @ 2023-12-07 16:13  lmyyyy  阅读(41)  评论(0)    收藏  举报