gpt4 book ai didi

java - 使用 HBaseTestingUtility 进行单元测试

转载 作者:行者123 更新时间:2023-12-02 08:47:59 28 4
gpt4 key购买 nike

我正在尝试使用 HBaseTestingUtility 库调试 java 代码。我已经创建了表。我需要:- 在“myTable”中插入带有键的值- 使用键从“myTable”获取值- 验证返回的值是否等于我创建的值这是我填写的代码:

package HbaseUniteTest;

import jdk.nashorn.api.scripting.ScriptUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.junit.Assert;

import static org.junit.Assert.assertEquals;

public class TestCreateTableClass
{
private final static String tableName = "myTable";
private static ScriptUtils HTableUtil;

public static void main( String[] args ) throws Exception {

//Start the "mini cluster"
HBaseTestingUtility testingUtility = new HBaseTestingUtility();
testingUtility.startMiniCluster();

//Get the configuration
//Configuration conf = ...
Configuration conf = testingUtility.getConfiguration();

//Instantiate a connection
Connection connection = ConnectionFactory.createConnection(conf);

//Define table "myTable"
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
table.addFamily(new HColumnDescriptor("cf1").setCompressionType(Compression.Algorithm.NONE));

//Create table "myTable"
connection.getAdmin().createTable(table);

//Get the first (and only) table name
String first_table = connection.getAdmin().listTableNames()[0].getNameAsString();

//Verify the returned Table name is equal to the table name we provided
assertEquals(tableName,first_table);

//Insert a value with a key in "myTable"
byte[] key = Bytes.toBytes("some-key");
Put put = new Put(key);
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1.1"), System.currentTimeMillis(), Bytes.toBytes("val1.1"));
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1.2"), System.currentTimeMillis(), Bytes.toBytes("val1.2"));
put.add(Bytes.toBytes("colfam2"), Bytes.toBytes("qual2.1"), System.currentTimeMillis(), Bytes.toBytes("val2.1"));
Result converted = HTableUtil.convert(put);
table.put(put);
Result readFromTable = table.get(new Get(key));
Assert.assertArrayEquals(readFromTable.raw(), converted.raw());


//Get the value from "myTable" with the key

//Verify the returned value is equal to the value you created


//Stop the mini cluster
testingUtility.shutdownMiniCluster();
System.out.println("END OF TEST");
}

public static void setHTableUtil(ScriptUtils HTableUtil) {
TestCreateTableClass.HTableUtil = HTableUtil;
}
}

但是,我收到以下错误:1、函数put.add()这行代码出错

put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1.1"), System.currentTimeMillis(), Bytes.toBytes("val1.1"));

javaerr1

  • 这行代码的第二个错误:
  •  Result converted = HTableUtil.convert(put);

    javaErr2

  • Java 找不到 put()、get()、raw() 这 3 个方法的符号
  • table.put(put);
    Result readFromTable = table.get(new Get(key));
    Assert.assertArrayEquals(readFromTable.raw(), converted.raw());

    javaErr3

  • 我还注意到一些有关 HTableDescriptor、HColumnDescriptor 类的警告已被弃用。我在互联网上查了一下,他们建议使用例如“TableDescriptorBuilder”,但我不确定如何使用它。 (引用:https://github.com/apache/hbase/blob/master/hbase-client/src/main/java/org/apache/hadoop/hbase/HTableDescriptor.java)
  • javaErr4

    最佳答案

    <强>1。这行代码与函数 put.add() 的错误。
    我认为您可以像这样使用 addColumn() 来添加列。

    put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1.1"), System.currentTimeMillis(), Bytes.toBytes("val1.1"));
    put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1.2"), System.currentTimeMillis(), Bytes.toBytes("val1.2"));
    put.addColumn(Bytes.toBytes("colfam2"), Bytes.toBytes("qual2.1"), System.currentTimeMillis(), Bytes.toBytes("val2.1"));


    <强>2。这行代码的第二个错误:
    我不熟悉“ScriptUtils”,但我认为它有效。

    Result converted = (Result) HTableUtil.convert(put, Result.class);


    <强>3。 Java找不到put()、get()、raw()这3个方法的符号
    这是因为您一直使用“HTableDescriptor”来 put()、get() 或 raw()。 'HTableDescriptor' 用于像 DDL 一样创建表。您需要使用 Table 类来使用 put()、get() 或 raw() 进行操作。

    Table createdTable = connection.getTable(TableName.valueOf(tableName));
    createdTable.put(put);
    Result readFromTable = createdTable.get(new Get(key));

    此外,我相信“Result”类不提供 raw()。因此,您可以像这样使用 Result.compareResults() 比较两个结果。

    Result.compareResults(readFromTable, converted);


    <强>4。如何使用“TableDescriptorBuilder”
    正如我上面所说,“描述符”是用于定义表、列族、列等的类。因此,当您制作/创建它们时需要使用它。

    //Define table "myTable"
    TableDescriptorBuilder table = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    table.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1")).setCompressionType(Compression.Algorithm.NONE).build());

    //Create table "myTable"
    connection.getAdmin().createTable(table.build());

    关于java - 使用 HBaseTestingUtility 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60961066/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com