Hbase Big Data learning the basic operations and HbaseMR 29 JavaAPI

8: hbase API Basic Operation

Here I did not write the various cluster information in the code, but the cluster configuration file in the project resource in, you can read directly to

package hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dawn
 * @date 2019年5月30日22:36:41
 * @version 1.0
 * hbase的基本api操作
 */
public class HbaseDemo {
    private static Configuration conf;
    private static Connection connection;
    private static HBaseAdmin admin;



    //构造方法加载配置
    public HbaseDemo() throws IOException {
        conf= HBaseConfiguration.create();
        // table operations use HbaseAdmin 
       IF (this.isExistTable (tableName)) {
        = ConnectionFactory.createConnection Connection (the conf); 
        // management table 
        ADMIN = (HBaseAdmin) connection.getAdmin (); 

    } 

    . //. 1 determines whether there is a table 
   public Boolean isExistTable (String tableName) { 
       Boolean = RS to false; 
       the try { 
           admin.tableExists = RS (TableName.valueOf (tableName)); 
       } the catch (IOException E) { 
           e.printStackTrace (); 
       } 
       return RS; 
   } 

    . 2 // Create a table in hbase create cluster 'user', 'info', 'the INFO1' 
    public void the createTable (tableName String, String ... ColumnFamily) throws IOException { 
           System.out.println ( "nearly table exists, enter a different table"); 
       } the else { 
           // Note 2: Create a table, then you need to create a descriptor 
           HTableDescriptor HTD = new new HTableDescriptor (TableName.valueOf (tableName)); 

           // 3. Create a column group 
           for (CF2 String: ColumnFamily) { 
               // the same: to create columns, if desired, to create a family column family descriptor 
               htd.addFamily (new new HColumnDescriptor (CF2)); 
           } 

           // Create a table. 4. 
           admin.createTable (HTD); 
           System.out.println ( "table successfully created"); 
       } 



    } 

    // delete table 3 
    public void deleteTable (String tableName) throws IOException { 
        // 1 If the table does not exist exists delete or print 
        // required to delete the specified table is not available 
       if (this.isExistTable (tableName)) {
           // 2 specifies unavailable (col), Bytes.toBytes (value));
        // 3. Load table 
           admin.disableTable (TableName. valueOf (tableName));
            admin.deleteTable (tableName); 
           System.out.println ( "successfully deleted table" + tableName); 
       } the else { 
           System.out.println ( "You did not have to watch ??? delete the What"); 
       } 
    } 

    // 4 adding data PUT 'User', 'RowKey', '' 
    public void addRow (String tableName, RowKey String, String CF2, COL String, String value) throws IOException { 
        // the data in the table will get the table object operation, the operation of the above table, it is necessary to get the object HbaseAdmin 
        the table table Connection.GetTable = (TableName.valueOf (tableName)); 

        .. 1 // put joins with data 
        put p = new put (Bytes.toBytes ( rowkey)) ; 
        // Add 2 data. 
        p.addColumn (Bytes.toBytes (CF2), Bytes.toBytes (COL), Bytes.toBytes (value));

        table.put (P); 
        System.out.println ( "Successful insertion !!"); 
    } 

    // delete the table 5 in a row of data deleteall 'table', 'RowKey' 
    public void the deleteRow (tableName String, String RowKey) IOException {throws 
        the Table T = Connection.GetTable (TableName.valueOf (tableName)); 

        .. 1 // delete data in accordance RowKey 
        the delete the delete new new D = (Bytes.toBytes (RowKey)); 

        . 2 // delete 
        t.delete (d ); 
        System.out.println ( "deleted successfully !!"); 
    } 

    // delete the 6 rows of data. 
    public void deleteAllRow (tableName String, String ... rowkeys) throws IOException { 
        the Table T = Connection.GetTable (the tableName. valueOf (tableName)); 

        .. 1 // put the package to delete the collection 
        List <delete> list = new ArrayList <Delete>();
        // iterate 2 
        for (String Row: rowkeys) { 
            the Delete the Delete new new D = (Bytes.toBytes (Row)); 
            List.add (D); 
        } 

        t.delete (List); 
        System.out.println ( "Success delete rows of data! "); 
    } 


    // scan surface 7 scan data table full table scan 
    public void scanAll (String tableName) throws IOException { 
        the table T = Connection.GetTable (TableName.valueOf (tableName)); 

        // example 1. Scan 
        Scan Scan new new S = (); 
        // 2, the object to get Scanner 
        ResultScanner t.getScanner RS = (S);
 
        .. 3 // iterate 
        for (R & lt the Result: RS) { 
            // get each column group info1 info2
            The Cell [] = r.rawCells cells (); 
            // iterate specific data 
            for (the Cell C: cells) { 
                System.out.println ( "row key is:" + Bytes.toString (CellUtil.cloneRow (c ))); 
                System.out.println ( "column group is:" + Bytes.toString (CellUtil.cloneFamily (C))); 
                System.out.println ( "value:" + Bytes.toString (CellUtil.cloneValue (c ))) ; 
            } 
        } 

    } 

    // 8 scan data designated scan 'User', {StartRow => '101', STOPROW => '101'} 
    public void scanRow (tableName String, String RowKey) throws IOException { 
        the Table T = Connection. getTable (TableName.valueOf (tableName)); 

        .. 1 // need to scan the specified data instance the Get 
        the Get the Get new new G = (Bytes.toBytes(rowkey));
        // 2 can only add filters to find where the data column family info1  
        g.addFamily (Bytes.toBytes ( "info1")); 

        the Result t.get RS = (G);
        Cell[] cells = rs.rawCells();
        // 3 traverse. 
        // iterate specific data 
        for (the Cell C: cells) { 
            System.out.println ( "row key is:" + Bytes.toString (CellUtil.cloneRow (C))); 
            System.out.println ( "column group is:" + Bytes.toString (CellUtil.cloneFamily (C))); 
            System.out.println ( "value:" + Bytes. toString (CellUtil.cloneValue (C))); 
        } 
    } 

    public static void main (String [] args) throws IOException { 
        HbaseDemo new new HbaseDemo HBase = (); 
// System.out.println (hbase.isExistTable ( "Dawn") ); 
// hbase.createTable ( "emp1", "INFO1", "INFO2"); 
// hbase.deleteTable ( "the TEMP");
//        hbase.addRow("emp1","003","info2","age","20");
//        hbase.deleteRow("emp1","001");
//        hbase.deleteAllRow("emp1","001","002");
//        hbase.scanAll("emp1");
        hbase.scanRow("emp1","003");

    }
}

  

9:hbase-MR

Official Hbase-Mapreduce,

Case I: on a table rowkey be counted

View package required (due hbase is based on the hdfs, so we have to run the task, then we should import MR hbase package to the yarn)

 

 

1) into the environment variable ( Not added ~ / .bash_profile, the environment here is a temporary variable)

export HBASE_HOME=/root/training/hbase

export HADOOP_HOME=/root/training/hadoop-2.8.4

export HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase mapredcp`

 

 

1) Start hbase-mr task (here using a package provided by the official)

/root/training/hadoop-2.8.4/bin/yarn jar lib/hbase-server-1.3.0.jar rowcounter dawn

 

 

result:

 

Case two columns: Import data into local hbase in

Ideas?

hbase underlying storage is hdfs, the first data into the corresponding HDFS hbase mr import data created with a table to the table

 

1:hdfs中创建文件夹 导入本地数据

 hdfs dfs -mkdir /hbasetest

 hdfs dfs -put love.tsv /hbasetest

 

2:创建表 create 'love','info'

 

3:导入操作:

/root/training/hadoop-2.8.4/bin/yarn jar lib/hbase-server-1.3.0.jar importtsv -Dimporttsv.columns=HBASE_ROW_KEY,info:name,info:description love hdfs://bigdata11:9000/hbasetest

 

运行结果:

 

案列三:hbaselove表进行指定列的筛选然后倒入到lovemr

提前在hbase中创建好lovemr

 

 

1) 构建Mapper类,读取love表中数据 

package hbasemr;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author Dawn
 * @date 2019年5月31日17:31:47
 * @version 1.0
 * hbase提供了mr的API
 * 需求?见readme文件
 */
public class ReadLoveMapper extends TableMapper<ImmutableBytesWritable,Put>{
    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context)
            throws IOException, InterruptedException {
        //1.读取数据 拿到一个rowkey的数据
        Put put = new Put(key.get());

        //2.遍历column
        for (Cell c:value.rawCells()){
            //3.加入列族数据 当前列族是info要 不是info列族的不要 是info数据才导入lovemr表中
            if ("info".equals(Bytes.toString(CellUtil.cloneFamily(c)))){
                //4.拿到指定列的数据
                if ("name".equals(Bytes.toString(CellUtil.cloneQualifier(c)))){
                    put.add(c);
                }
            }
        }

        context.write(key,put);
    }
}

  

2) 构建Reducer类,将love表中数据写入到lovemr表中

package hbasemr;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.NullWritable;


import java.io.IOException;

public class WriteLoveReducer extends TableReducer<ImmutableBytesWritable,Put,NullWritable> {
    @Override
    protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context)
            throws IOException, InterruptedException {
        for (Put p:values){
            //遍历数据
            context.write(NullWritable.get(),p);
        }
    }
}

  

3) 构建driver驱动类 

package hbasemr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * @author Dawn
 * @date 2019年5月31日17:44:13
 * @version 1.0
 * 驱动类
 */
public class LoveDriver implements Tool {

    private Configuration conf;

    //业务逻辑
    public int run(String[] strings) throws Exception {
        //1.创建任务
        Job job=Job.getInstance(conf);
        //2.指定运行的主类
        job.setJarByClass(LoveDriver.class);
        //3.配置job 采用scan方式扫描该表
        Scan scan = new Scan();

        //4.设置mapper类
        TableMapReduceUtil.initTableMapperJob("love",
                scan,
                ReadLoveMapper.class,
                ImmutableBytesWritable.class,
                Put.class,
                job);

        //5.设置reducer类
        TableMapReduceUtil.initTableReducerJob("lovemr",
                WriteLoveReducer.class,
                job);

        //设置reduceTask个数
        job.setNumReduceTasks(1);

        boolean rs = job.waitForCompletion(true);
        return rs?0:1;
    }

    //设置配置
    public void setConf(Configuration configuration) {
        this.conf= HBaseConfiguration.create(configuration);
    }

    //拿到配置
    public Configuration getConf() {
        return this.conf;
    }

    public static void main(String[] args) {
        try {
            int status = ToolRunner.run(new LoveDriver(), args);
            System.exit(status);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  

4) 打包 放入集群中运行这个任务

运行结果:

 

Guess you like

Origin www.cnblogs.com/hidamowang/p/10960611.html