使用 Put.add() 方法在 HBase 中添加值

Adding values in HBase using Put.add() method

我正在编写一个简单的 Java 客户端代码以在 HBase table 中添加值。我正在使用 put.add(byte[] columnFamily, byte[] columnQualifier, byte[] value),但此方法在新 HBase API 中已弃用。任何人都可以请帮助使用新的 Put API?

的方法是什么

使用 maven 我已经下载了 HBase 版本 1.2.0 的 jar。

我正在使用以下代码:

package com.NoSQL;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
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.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;


public class PopulatingData {

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

        String table = "Employee";

        Logger.getRootLogger().setLevel(Level.WARN);
        Configuration conf = HBaseConfiguration.create();
        Connection con = ConnectionFactory.createConnection(conf);
        Admin admin = con.getAdmin();

        if(admin.tableExists(TableName.valueOf(table))) {
            Table htable = con.getTable(TableName.valueOf(table));


            /*********** adding a new row ***********/

            // adding a row key


            Put p = new Put(Bytes.toBytes("row1"));



            p.add(Bytes.toBytes("ContactDetails"), Bytes.toBytes("Mobile"), Bytes.toBytes("9876543210"));
            p.add(Bytes.toBytes("ContactDetails"), Bytes.toBytes("Email"), Bytes.toBytes("abhc@gmail.com"));

            p.add(Bytes.toBytes("Personal"), Bytes.toBytes("Name"), Bytes.toBytes("Abhinav Rawat"));
            p.add(Bytes.toBytes("Personal"), Bytes.toBytes("Age"), Bytes.toBytes("21"));
            p.add(Bytes.toBytes("Personal"), Bytes.toBytes("Gender"), Bytes.toBytes("M"));

            p.add(Bytes.toBytes("Employement"), Bytes.toBytes("Company"), Bytes.toBytes("UpGrad"));
            p.add(Bytes.toBytes("Employement"), Bytes.toBytes("DOJ"), Bytes.toBytes("11:06:2018"));
            p.add(Bytes.toBytes("Employement"), Bytes.toBytes("Designation"), Bytes.toBytes("ContentStrategist"));

            htable.put(p);


            /**********************/

            System.out.print("Table is Populated");`enter code here`            

        }else {

            System.out.println("The HBase Table named "+table+" doesn't exists.");
        }

        System.out.println("Returnning Main");

    }

}

使用 addColumn() 方法:

Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(NAME_FAMILY, NAME_COL_QUALIFIER, name);

请参阅下面的 javadoc 中的更多详细信息: https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Put.html