hbase-spark组合使用

依赖



    4.0.0
    sparkTest
    sparkTest
    1.0-SNAPSHOT
    
        
            org.apache.spark
            spark-core_2.10
            1.6.0
        
        
            org.apache.hbase
            hbase-client
            1.4.6
        
        
            org.apache.hbase
            hbase-server
            1.4.6
        
    

 

 

代码

 

 

 

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.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
import org.apache.hadoop.hbase.client.Result;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

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

public class SparkTest {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("myTest").setMaster("local[2]");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "mini1:2181,mini2:2181,mini3:2181");
        conf.set(TableInputFormat.INPUT_TABLE, "one");//查询的表
        conf.set(TableInputFormat.SCAN_ROW_START, "001");//起始行键
        conf.set(TableInputFormat.SCAN_ROW_STOP, "103");//结束行键
        Scan scan = new Scan();
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator("".getBytes()));//过滤器
        scan.setFilter(rowFilter);
        conf.set(TableInputFormat.SCAN, convertScanToString(scan));
        JavaPairRDD rdd = sc.newAPIHadoopRDD(conf, TableInputFormat.class, ImmutableBytesWritable.class, Result.class);//获取rdd数据
        //处理数据
        JavaPairRDD tuple2JavaRDD = rdd.mapToPair(new PairFunction, String, Integer>() {
            @Override
            public Tuple2 call(Tuple2 immutableBytesWritableResultTuple2) throws Exception {
                Result result = immutableBytesWritableResultTuple2._2;
                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {//获取行键,列簇,列,时间戳与值
                    String row = Bytes.toString(CellUtil.cloneRow(cell));//行键
                    String value = "value=" + Bytes.toString(CellUtil.cloneValue(cell));//值
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));//列簇
                    String col = Bytes.toString(CellUtil.cloneQualifier(cell));//列名
                    String column = "column=" + family + ":" + col;//列簇与列
                    String timestamp = "timestamp=" + cell.getTimestamp();//时间戳
                    System.out.println(String.format("%s %s %s %s", row, column, timestamp, value));//格式化输出
                }
                return new Tuple2(new String("test"), 1);//返回数据
            }
        });
        List> collect = tuple2JavaRDD.collect();//算子计算
        sc.close();
    }

    /**
     * 字符串 过滤器
     *
     * @param scan
     * @return
     */
    public static String convertScanToString(Scan scan) {
        ClientProtos.Scan proto = null;
        try {
            proto = ProtobufUtil.toScan(scan);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Base64.encodeBytes(proto.toByteArray());
    }
}

 

 

 

 

运行结果hbase-spark组合使用_第1张图片

你可能感兴趣的:(spark,hbase)