首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HBaseTestingUtility的单元测试

使用HBaseTestingUtility的单元测试
EN

Stack Overflow用户
提问于 2020-03-31 22:44:37
回答 1查看 894关注 0票数 0

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

代码语言:javascript
复制
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()在这一行代码中的错误

代码语言:javascript
复制
put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1.1"), System.currentTimeMillis(), Bytes.toBytes("val1.1"));

  1. 这一行代码上的第二个错误:

代码语言:javascript
复制
 Result converted = HTableUtil.convert(put);

  1. Java找不到这3种方法的符号-- put()、get()、raw()

代码语言:javascript
复制
table.put(put);
Result readFromTable = table.get(new Get(key));
        Assert.assertArrayEquals(readFromTable.raw(), converted.raw());

  1. 我还注意到一些关于类HTableDescriptor、HColumnDescriptor的警告已经被否决。我在网上查了一下,他们建议我用"TableDescriptorBuilder“来代替,但我不知道该如何使用。(参考文献:https://github.com/apache/hbase/blob/master/hbase-client/src/main/java/org/apache/hadoop/hbase/HTableDescriptor.java)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-01 01:45:06

1.函数put.add().在这一行代码中的错误

我认为您可以像这样使用addColumn()来添加列。

代码语言:javascript
复制
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”,但我认为它有效。

代码语言:javascript
复制
Result converted = (Result) HTableUtil.convert(put, Result.class);

3. Java找不到这3种方法的符号-- put()、get()、

这是因为您一直使用'HTableDescriptor‘来放置()、get()或raw()。“HTableDescriptor”用于创建像DDL这样的表。您需要使用Table类来使用put()、get()或raw()来操作。

代码语言:javascript
复制
Table createdTable = connection.getTable(TableName.valueOf(tableName));
createdTable.put(put);
Result readFromTable = createdTable.get(new Get(key));

而且,我认为类‘结果’不能提供raw()。因此,您可以像这样使用Result.compareResults()比较这两个结果。

代码语言:javascript
复制
Result.compareResults(readFromTable, converted);

4.如何使用“TableDescriptorBuilder”

正如我前面所说的,“描述符”是定义表、列家族、列等的类。因此,您需要在make/create时使用它。

代码语言:javascript
复制
//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());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60961066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档