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

时间:2018-09-19 10:21:45

标签: java api hbase

我正在编写一个简单的Java客户端代码以在HBase表中添加值。我正在使用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");

    }

}

1 个答案:

答案 0 :(得分:1)

使用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