Spark拉取Kafka的流資料,轉插入HBase中
Spark拉取Kafka的流資料,轉插入HBase中
pom.xml檔案樣例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yys.spark</groupId>
<artifactId>spark</artifactId>
<version>1.0</version>
<inceptionYear>2008</inceptionYear>
<properties>
<scala.version>2.11.12</scala.version>
<kafka.version>0.9.0.1</kafka.version>
<spark.version>2.2.0</spark.version>
<hadoop.version>2.7.5</hadoop.version>
<hbase.version>1.4.0</hbase.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- Kafka 依賴-->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>${kafka.version}</version>
</dependency>
<!-- Hadoop 依賴-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!-- HBase 依賴-->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
<!-- Spark Streaming 依賴-->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- Spark Streaming整合Flume 依賴-->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-flume_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-flume-sink_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- Spark SQL 依賴-->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-scala_2.11</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.apache.flume.flume-ng-clients</groupId>
<artifactId>flume-ng-log4jappender</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
<build>
<!--
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
-->
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<buildcommands>
<buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
</buildcommands>
<additionalProjectnatures>
<projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
</additionalProjectnatures>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
scala程式碼:
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.HColumnDescriptor
import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.client.HTable
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.client.Result
import org.apache.hadoop.hbase.client.Scan
import org.apache.hadoop.hbase.util.Bytes
import org.apache.spark._
import org.apache.spark.SparkContext
import org.apache.hadoop.hbase.client.Get
import org.apache.spark.serializer.KryoSerializer
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapreduce.TableInputFormat
import org.apache.hadoop.mapred.JobConf
import org.apache.hadoop.hbase.mapred.TableOutputFormat
import org.apache.spark.rdd.RDD.rddToPairRDDFunctions
import org.apache.hadoop.hbase.util.Bytes
//拉取kafka的資料流,轉插入hbase中
object Kafka2Hbase {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setMaster("local").setAppName("HBaseTest")
.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
val sc = new SparkContext(sparkConf)
//hbase中的表名稱
var table_name = "create_table_at_first"
val conf = HBaseConfiguration.create()
//hbase的配置資訊可以從/home/hadoop/HBase/hbase/conf/hbase-site.xml得到
conf.set("hbase.rootdir", "hdfs://master:9000/hbase_db")
conf.set("hbase.zookeeper.quorum", "master,Slave1,Slave2")
conf.set("hbase.zookeeper.property.clientPort", "2181")
conf.set("hbase.master", "60000")
conf.set(TableInputFormat.INPUT_TABLE, table_name)
//初始化jobconf,TableOutputFormat必須是org.apache.hadoop.hbase.mapred包下的!
val jobConf = new JobConf(conf)
jobConf.setOutputFormat(classOf[TableOutputFormat])
jobConf.set(TableOutputFormat.OUTPUT_TABLE, table_name)
val indataRDD = sc.makeRDD(Array("1,jack15,15", "2,mike16,16"))
val rdd = indataRDD.map(_.split(',')).map { arr => {
/*一個Put物件就是一行記錄,在構造方法中指定主鍵
* 所有插入的資料必須用org.apache.hadoop.hbase.util.Bytes.toBytes方法轉換
* Put.add方法接收三個引數:列族,列名,資料
* myfamily:為列族名
*/
val put = new Put(Bytes.toBytes(arr(0).toInt))
put.add(Bytes.toBytes("myfamily"), Bytes.toBytes("name"), Bytes.toBytes(arr(1)))
put.add(Bytes.toBytes("myfamily"), Bytes.toBytes("age"), Bytes.toBytes(arr(2).toInt))
//轉化成RDD[(ImmutableBytesWritable,Put)]型別才能呼叫saveAsHadoopDataset
(new ImmutableBytesWritable, put)
}
}
rdd.saveAsHadoopDataset(jobConf)
sc.stop()
//之後在hbase中,可以get 'create_table_at_first','jack15','myfamily' 查詢這條資料即可
}
}
相關文章
- spark讀取hbase的資料Spark
- 槓上Spark、Flink?Kafka為何轉型流資料平臺SparkKafka
- 槓上 Spark、Flink?Kafka 為何轉型流資料平臺SparkKafka
- Spark Streaming讀取Kafka資料兩種方式SparkKafka
- Spark 如何寫入HBase/Redis/MySQL/KafkaSparkRedisMySqlKafka
- MapReduce和Spark讀取HBase快照表Spark
- Flume將 kafka 中的資料轉存到 HDFS 中Kafka
- 三種大資料流處理框架選擇比較:Apache Kafka流、Apache Spark流和Apache Flink - quora大資料框架ApacheKafkaSpark
- Mysql在資料插入後立即獲取插入的IdMySql
- spark streaming執行kafka資料來源SparkKafka
- Spark讀取MySQL資料SparkMySql
- spark與hbaseSpark
- MySQL 資料庫表格建立、資料插入及獲取插入的 ID:Python 教程MySql資料庫Python
- eazyexcel 讀取excel資料插入資料庫Excel資料庫
- Spark讀取elasticsearch資料指南SparkElasticsearch
- goim 中的 data flow 資料流轉及思考Go
- Spark Streaming使用Kafka保證資料零丟失SparkKafka
- kafka+storm+hbaseKafkaORM
- Java中使用FFmpeg拉取RTSP流Java
- mongodb資料庫中插入資料MongoDB資料庫
- HBase實操:HBase-Spark-Read-Demo 分享Spark
- 獲取Wireshark資料流
- 【重製版】全網最詳細ubuntu虛擬機器搭建hadoop+spark+zookeeper+hbase+kafka大資料環境Ubuntu虛擬機HadoopSparkKafka大資料
- Canal1.1.4獲取資料後直接傳送到kafka的Topic中Kafka
- Spark+Kafka實時監控Oracle資料預警SparkKafkaOracle
- Spark讀取MongoDB資料的方法與優化SparkMongoDB優化
- Firedac 在資料表中插入BLOB資料的方法
- RocketMq 拉取資料流程原始碼分析MQ原始碼
- mybatis插入資料、批量插入資料MyBatis
- spark-streaming-kafka透過KafkaUtils.createDirectStream的方式處理資料SparkKafka
- HBase2實戰:HBase Flink和Kafka整合Kafka
- Spark(16) -- 資料讀取與儲存的主要方式Spark
- 資料流中的中位數
- Hive 如何快速拉取大批量資料Hive
- Spark Streaming,Flink,Storm,Kafka Streams,Samza:如何選擇流處理框架SparkORMKafka框架
- 雲小課|MRS資料分析-透過Spark Streaming作業消費Kafka資料SparkKafka
- Spark雙流join-延遲資料--double_happySparkAPP
- Spark 讀取 Hbase 優化 --手動劃分 region 提高並行數Spark優化並行