pom内容:

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>0.98.6-cdh5.2.0</version>
<exclusions>
<exclusion>
<artifactId>javax.servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
<exclusion>
<artifactId>javax.servlet</artifactId>
<groupId>org.eclipse.jetty.orbit</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api-2.5</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.5.2-hdh3.1.0</version>
<exclusions>
<exclusion>
<artifactId>hadoop-client</artifactId>
<groupId>org.apache.hadoop</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.5.2-hdh3.1.0</version>
<exclusions>
<exclusion>
<artifactId>javax.servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>

一、Hbase API获取hbase表数据

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException;
import java.util.List; /**
* 通过HbaseApi获取数据
*/
public class DataAchieveFromHbaseApi {
public static void main(String[] args) throws IOException {
//Hbase配置
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");//端口
conf.set("hbase.zookeeper.quorum","hdh1,hdh2,hdh3");//hbase zookeeper地址
//扫描配置
Scan scan=new Scan();
scan.addFamily(Bytes.toBytes("cf"));//列族,可添加多个
//hbase表
HTable hTable=new HTable(conf, Bytes.toBytes("test"));//表明
//获取扫描数据
ResultScanner rs= hTable.getScanner(scan);
//hbase表的列族信息
HColumnDescriptor[] hColDes=hTable.getTableDescriptor().getColumnFamilies();
for (HColumnDescriptor hColDe : hColDes) {
System.out.println(Bytes.toString(hColDe.getName()));
}
//展示每一行的每一列(这个只有一列)信息
for (Result r : rs) {
byte [] bytes= r.getValue(Bytes.toBytes("cf"),Bytes.toBytes("SSID"));//列族和列名
String str=new String(bytes,"UTF-8");
if(null!=str&&str.trim().length()>0) {
System.out.println(str.trim());
}
}
System.out.println("end<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
}

二、Spark提供接口获取Hbase表数据:

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
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.spark.api.java.function.VoidFunction;
import scala.Tuple2; import java.io.IOException; /**
* 通过hfile形式获取数据
*/
public class DataAchieveFromHfile {
private static JavaPairRDD<ImmutableBytesWritable, Result> rdd; public static void main(String[] args) throws IOException {
Configuration conf= HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum","hdh1,hdh2,hdh3");
conf.set(TableInputFormat.INPUT_TABLE, "test");
SparkConf conf1=new SparkConf().setAppName("test").setMaster("local");//设置spark app名称和运行模式(此为local模式)
JavaSparkContext sc=new JavaSparkContext(conf1);
//加载数据
rdd=sc.newAPIHadoopRDD(conf,TableInputFormat.class, ImmutableBytesWritable.class, Result.class);
System.out.println("读取数据条数:"+rdd.count());
rdd.foreach(new VoidFunction<Tuple2<ImmutableBytesWritable, Result>>() {
@Override
public void call(Tuple2<ImmutableBytesWritable, Result> result) throws Exception {
byte [] bytes= result._2().getValue(Bytes.toBytes("cf"), Bytes.toBytes("SSID"));//列族和列名
String str= new String(bytes,"UTF-8");
if(null!=str&&str.trim().length()>0) {
System.out.println(str.trim());
}
}
});
}
}

1、Spark 通过api,hfile两种形式获取hbase数据,简单样例的更多相关文章

  1. Python第十四天 序列化 pickle模块 cPickle模块 JSON模块 API的两种格式

    Python第十四天 序列化  pickle模块  cPickle模块  JSON模块  API的两种格式 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Py ...

  2. C++:一般情况下,设计函数的形参只需要两种形式

    C++:一般情况下,设计函数的形参只需要两种形式.一,是引用形参,例如 void function (int &p_para):二,是常量引用形参,例如 void function(const ...

  3. jquery插件的两种形式

    这里总结一下jquery插件的两种形式,一种是通过字面量的形式组织代码,另一种是通过构造函数的方式.下面就两种形式来分析俩个例子. 例子1: ;(function ($,window,document ...

  4. SQL 关于apply的两种形式cross apply 和 outer apply(转)

    转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...

  5. SQL 关于apply的两种形式cross apply 和 outer apply

    SQL 关于apply的两种形式cross apply 和 outer apply 例子: CREATE TABLE [dbo].[Customers]( ) COLLATE Chinese_PRC_ ...

  6. SQL关于apply的两种形式cross apply和outer apply(转载)

    SQL 关于apply的两种形式cross apply 和 outer apply   apply有两种形式: cross apply 和 outer apply   先看看语法:   <lef ...

  7. Spark on YARN的两种运行模式

    Spark on YARN有两种运行模式,如下 1.yarn-cluster:适合于生产环境.        Spark的Driver运行在ApplicationMaster中,它负责向YARN Re ...

  8. 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编

    在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...

  9. Controller@实现Controller的两种形式

    实现Controller的两种形式 形式1:仅仅实现IController接口,自定义Controller对Request的实现.形式2:在实现IController接口以后,继承Controller ...

随机推荐

  1. MySQL STRAIGHT_JOIN

    问题 最近在调试一条查询耗时5s多的sql语句,这条sql语句用到了多表关联(inner join),按时间字段排序(order by),时间字段上已经创建了索引(索引名IDX_published_a ...

  2. Java学习笔记--PriorityQueue(优先队列)(堆)

    PriorityQueue(优先队列)实际上是一个堆(不指定Comparator时默认为最小堆)队列既可以根据元素的自然顺序来排序,也可以根据 Comparator来设置排序规则.队列的头是按指定排序 ...

  3. 手动替换GCC版本

    当我们的系统里面会有2个以上版本的gcc时,系统会缺省的默认一个gcc版本,当然我们可以更改系统的默认配置,来降低gcc.g++的版本以满足不同的需求. 1.查看GCC的版本信息 $ls /usr/b ...

  4. Debian/Ubuntu 安装bcm43142无线网卡驱动

    Drivers for Broadcom BCM43142 wireless card of Ubuntu/Debian 64-bit Linux 1.Check the wireless card ...

  5. Qt creator 常用的快捷健

    Qt creator 常用的快捷健 F1        查看帮助F2        跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2    声明和定义之间切换F4        头文件 ...

  6. an error occured during the file system check

    打开虚拟机的时候,报错: 出错原因: 我之前修改了/etc/fstab文件, 原先/etc/fstab文件中有一行是这样的: LABEL=/i01              /u01          ...

  7. gitosis使用笔记

    gitosis是Git下的权限管理工具,通过一个特殊的仓库(gitosis-admin.git)对Git权限进行管理. 1:服务端安装并配置gitosis (1)通过以下方式获取到安装包 root@w ...

  8. cf Sereja and Array

    http://codeforces.com/contest/315/problem/B #include <cstdio> #include <cstring> #includ ...

  9. 带KEY的SCP命令,老是要查,这次写在这里吧,

    有些东东记不住,急要用时老是想不起,放在这里吧, scp -r -i /xxx/rsa.key -P port user@ip:/source/ /target/

  10. 【转】android 5.0 64bit系统加载库文件失败问题浅析

    原文网址:http://blog.csdn.net/andrewblog/article/details/43601303 最近公司的一个项目使用android 5.0 64 bit平台,相对以前版本 ...