cool hit counter HBase Learning_Intefrankly

HBase Learning


HBase add data with the put command, but in HBase can only add one column of data for one row of a table at a time, that is, a cell to add a data, this is different from the relational database, in the relational database is directly inserted into the entire row of data, so HBase direct shell command to insert data is very inefficient, in practice, are generally used to operate HBase programmatically.

1. HBase Operation Commands

  • a table (listing information) columnsa table (listing information): Order format.list
  • trauma tabulate: Command format (do not specify the default number of versions to save, the default value is 3).create ' table name',' columns race1',' columns race2' Command format (specify the number of versions saved by default).create ' table name',{NAME=>' columns race1',VERSIONS=> Number of versions saved}, {NAME=>' columns race2',VERSIONS=> Number of versions saved}
  • View table description. Order format.describe ' table name'
  • Delete table. Command format (first make the table unavailable).disable ' table name' Command format (delete table).drop ' table name'
  • Add data. Command format (column families have only a single column name).put ' table name',' row number',' columns race',' columns happen to' Command Format( columns There are multiple clans listings):put ' table name',' row number',' columns race: listings',' columns happen to'
  • Delete data: Command format (delete a cell's data).delete ' table name',' row number',' columns race' Command format (delete data for a line number).deleteall ' table name',' row number'
  • Modified data. Command format (there is actually no modify data command in HBase).put ' table name',' row number',' columns race',' new cell value (math.)'
  • View data. Command Format( View a particular columns Data on ethnicity):get ' table name',' row number',{COLUMN=>' columns race',VERSIONS=> Number of versions viewed} Command Format (View data for a line number).get ' table name',' row number' Command format (view data for the entire table).scan ' table name'

2. Java programming to manipulate HBase

manipulate the jar packages used by HBase, import them using Maven, and introduce the dependencies hbase-it , the pom.xml file dependency section is as follows.

Note: The HBase I installed is version 1.1.0, so the dependency version used here is also 1.1.0, please keep the jar package version and HBase version the same.

<dependencies>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-it</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>

The jar package provided with HBase is just a basic wrapper for HBase command operations, and can be invoked with further wrappers on top of that for ease of use.

Here are the HBase wrapped tool classes HBaseUtil

package com.weizhiwen.util;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

public class HBaseUtil {
  public static Configuration configuration;
  public static Connection connection;
  public static Admin admin;
  /**
   *  tabulate。HBase table will have a system default attribute as the primary key,
   *  The primary key does not need to be created by itself, default isput The first data after the table name in the command operation,
   *  So there is no need to createid columns
   * @param myTableName  table name
   * @param columnFamily  family array
   * @throws IOException 
   */
  @SuppressWarnings("all")
  public static void createTable(String myTableName, String[] columnFamily) throws IOException {
    init();
    TableName tableName = TableName.valueOf(myTableName);
    if(admin.tableExists(tableName)) {
      System.out.println(myTableName+" is exists");
    } else {
      HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
      for(String columnName : columnFamily) {
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnName);
        hTableDescriptor.addFamily(hColumnDescriptor);
      }
      admin.createTable(hTableDescriptor);
      System.out.println("create "+myTableName+" success");
    }
    close();
  }
  
  /**
   *  Delete the specified table
   * @param myTableName  table name
   * @throws IOException
   */
  public static void deleteTable(String myTableName) throws IOException {
    init();
    TableName tableName = TableName.valueOf(myTableName);
    if (admin.tableExists(tableName)) {
        admin.disableTable(tableName); //  Invalidate the table first
        admin.deleteTable(tableName); //  After deleting the table
    }
    close();
  }
  
  /**
   *  View existing table
   * @throws IOException
   */
  public static void listTables() throws IOException {
      init();
      HTableDescriptor hTableDescriptors[] = admin.listTables();
      for(HTableDescriptor hTableDescriptor : hTableDescriptors){
          System.out.println(hTableDescriptor.getNameAsString());
      }
      close();
  }
  
  /**
   *  Insert data into a column of a row
   * @param myTableName  table name
   * @param rowKey  line key
   * @param colFamily  family name
   * @param column  listings( If there are no sub-list names under the column family, This parameter can be empty)
   * @param value  cell value (math.)
   * @throws IOException
   */
  public static void insertRowData(String tableName, String rowKey, String colFamily, String column, String value) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Put put = new Put(rowKey.getBytes());
    put.addColumn(colFamily.getBytes(), column.getBytes(), value.getBytes());
    table.put(put);
    table.close();
    close();
  }
  
  /**
   *  Delete data
   * @param tableName  table name
   * @param rowKey  line key
   * @param colFamily  family name
   * @param col  listings
   * @throws IOException
   */
  public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Delete delete = new Delete(rowKey.getBytes());
    // Delete all data for the specified column family
    //delete.addFamily(colFamily.getBytes());
    // Deletes data from a specified column
    //delete.addColumn(colFamily.getBytes(), col.getBytes());

    table.delete(delete);
    table.close();
    close();
  }
  
  /**
   *  Querying data by row key
   * @param tableName  table name
   * @param rowKey  line key
   * @param colFamily  family name
   * @param col  listings
   * @throws IOException
   */
  public static void getRowData(String tableName,String rowKey,String colFamily,String col)throws  IOException{
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Get get = new Get(rowKey.getBytes());
    get.addColumn(colFamily.getBytes(),col.getBytes());
    Result result = table.get(get);
    showCell(result);
    table.close();
    close();
  }
  
  /**
   *  Formatted output
   * @param result
   */
  public static void showCell(Result result){
      Cell[] cells = result.rawCells();
      for(Cell cell:cells){
          System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
          System.out.println("Timetamp:"+cell.getTimestamp()+" ");
          System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
          System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
          System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
      }
  }
  
  
  //  Establish connection
  public static void init() {
    configuration = HBaseConfiguration.create();
    configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
    try {
      connection = ConnectionFactory.createConnection(configuration);
      admin = connection.getAdmin();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  
  //  Close the connection
  public static void close() {
    try {
      if(admin != null) {
        admin.close();
      }
      if(connection != null) {
        connection.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Test classes for tools HBaseUtilTest

package com.weizhiwen.test;

import java.io.IOException;

import com.weizhiwen.util.HBaseUtil;

public class HBaseTest {
  public static void main(String[] args) throws IOException {
     // View all tables in the HBase database
    HBaseUtil.listTables();
     // Delete the results table (existing data table in HBase database)
    HBaseUtil.deleteTable("chengji");
     // Re-create the results table
    HBaseUtil.createTable("chengji", new String[]{"score"});
     // Insert data
    HBaseUtil.insertRowData("chengji", "zhangsan", "score", "English", "98");
    HBaseUtil.insertRowData("chengji", "zhangsan", "score", "Math", "94");
    HBaseUtil.insertRowData("chengji", "zhangsan", "score", "Computer", "98");
     // View single row of data
    HBaseUtil.getRowData("chengji", "zhangsan", "score", "Math");
  }
}

on top of HBaseUtil Class encapsulation is also just basic encapsulation, It can also be used according to the actual HBase Before coming to the encapsulation you need, For example, a single line with multiple columns insertion, Query the whole table, etc.。

Personal GitHub address.https://github.com/weizhiwen We welcome visits.


Recommended>>
1、苹果或推新工具两名iPhone用户可共享AR体验
2、This chess blockchain project turnover has broken 10 million
3、Digital government to enhance management effectiveness Foshan Chancheng District promotes social governance modernization with big data
4、Google removes accounts linked to Iranled misinformation campaign
5、Japanese company officially launches quantum computer development to push commercialization

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号