longhaofeixia

put 操作样例

//Example application inserting data into HBase
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
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;
// ^^ PutExample
import util.HBaseHelper;
// vv PutExample

import java.io.IOException;
public class PutExample {

  public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create(); //  Create the required configuration.
    conf.set("hbase.master", "192.168.1.11:60000");
    conf.set("hbase.zookeeper.quorum", "192.168.1.11");
    conf.set("hbase.zookeeper.property.clientPort", "2181");

    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("testtable");
    helper.createTable("testtable", "colfam1");

    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf("testtable")); //Instantiate a new client.

    Put put = new Put(Bytes.toBytes("row1")); //Create put with specific row.

    put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
      Bytes.toBytes("val1")); //Add a column, whose name is "colfam1:qual1", to the put.
    put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
      Bytes.toBytes("val2")); //Add another column, whose name is "colfam1:qual2", to the put.

    table.put(put); //Store row with column into the HBase table.
    table.close(); // Close table and connection instances to free resources.
    connection.close();
    helper.close();
  }
}

分类:

技术点:

相关文章: