Unit test using HBaseTestingUtility

Shuti :

I am trying to debug the java code using HBaseTestingUtility library. I already have table created. I need to: - Insert a value with a key in "myTable" - Get the value from "myTable" with the key - Verify the returned value is equal to the value I created Here is the code that I filled out:

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;
    }
}

However, I got the following error: 1. The error at this line of code with the function put.add()

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

javaerr1

  1. The 2nd error on this line of code:
 Result converted = HTableUtil.convert(put);

javaErr2

  1. Java cannot find symbol for these 3 methods put(), get(), raw()
table.put(put);
Result readFromTable = table.get(new Get(key));
        Assert.assertArrayEquals(readFromTable.raw(), converted.raw());

javaErr3

  1. I also notice some warnings regarding the class HTableDescriptor, HColumnDescriptor have been deprecated. I checked on internet and they advice to use for example "TableDescriptorBuilder" instead but I am not sure how to use it. (Ref: https://github.com/apache/hbase/blob/master/hbase-client/src/main/java/org/apache/hadoop/hbase/HTableDescriptor.java)

javaErr4

Kideok Kim :

1. The error at this line of code with the function put.add().
I think you can use addColumn() like this for adding column.

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. The 2nd error on this line of code:
I'm not familiar with 'ScriptUtils', But I think It works.

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


3. Java cannot find symbol for these 3 methods put(), get(), raw()
It because you keep using 'HTableDescriptor' to put(), get(), or raw(). 'HTableDescriptor' is used to create table like DDL. You need to use Table class to manipulate using put(), get(), or raw().

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

Also, I believe class 'Result' doesn't provide raw(). So, you can compare both Results using Result.compareResults() like this.

Result.compareResults(readFromTable, converted);


4. How to use 'TableDescriptorBuilder'
Like I said above, 'Descriptor' is the class for defining your table, column family, column, and so on. So, you need to use it when you make/create them.

//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());

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=385066&siteId=1
Recommended