spark 写入数据到Geomesa(Hbase)
package com.grady.geomesa
import org.apache.spark.sql.jts.PointUDT
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.{SparkConf, sql}
import org.geotools.data.DataStoreFinder
import org.geotools.geometry.jts.JTSFactoryFinder
import org.locationtech.geomesa.utils.geotools.SchemaBuilder
import org.locationtech.jts.geom.Coordinate
import org.locationtech.geomesa.spark.jts._
import scala.collection.JavaConversions._
object SparkWriteGeomesa {
val ToGeomesaCatalog = "gradytest"
val ToGeomesaCatalogFeature = "student"
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("SparkWriteGeomesa")
val ss = SparkSession.builder().config(conf).getOrCreate().withJTS
// 组装要写入的数据
val inDataFrame = gainData(ss)
inDataFrame.show()
// 写入数据
saveData(inDataFrame, ToGeomesaCatalog, ToGeomesaCatalogFeature)
ss.stop()
}
def gainData(ss: SparkSession): sql.DataFrame = {
val inDataRDD = ss.sparkContext.makeRDD(Array("1,jack,15,11.10,12.10", "2,Lily,16,12.10,13.10", "3,mike,16,14.10,15.10"))
val rowRDD = inDataRDD.map(_.split(','))
.map(row => {
val id = row(0)
val name = row(1)
val age = row(2)
val lon = row(3).toDouble
val lat = row(4).toDouble
val factory = JTSFactoryFinder.getGeometryFactory()
val coordinate = new Coordinate(lon, lat)
val geom = factory.createPoint(coordinate)
Row(geom, id, name, age)
})
val structType = StructType(
Seq(
StructField("geom", PointUDT, nullable = true),
StructField("studentId", StringType, nullable = true),
StructField("name", StringType, nullable = true),
StructField("age", StringType, nullable = true)
)
)
ss.createDataFrame(rowRDD, structType)
}
def saveData(inDataFrame: sql.DataFrame, toGeomesaCatalog: String, toGeomesaCatalogFeature: String) = {
val sft = SchemaBuilder.builder()
.addPoint("geom", true)
// 不可用id, id为保留字段
.addString("studentId")
.addString("name")
.addString("age")
.build(toGeomesaCatalogFeature)
//geomesa.mix.geometries指定同时支持point 和non-point混合几何特性
sft.getUserData.put("geomesa.mixed.geometries",Boolean.box(true))
val params = Map(
"hbase.zookeepers" -> "10.82.232.64:2181",
"hbase.catalog" -> toGeomesaCatalog)
//创建feature
DataStoreFinder.getDataStore(params).createSchema(sft)
inDataFrame.write.format("geomesa")
.options(params)
.option("geomesa.feature", toGeomesaCatalogFeature)
.save()
}
}
hbase 查看表是否生成:
hbase(main):001:0> list
TABLE
gradytest
gradytest_student_id_v4
gradytest_student_z2_geom_v5
## geomesa 数据会序列化,这里是序列化后的数据
hbase(main):003:0> scan 'gradytest_student_z2_geom_v5'
ROW COLUMN+CELL
\x000\x85\xD7\x8C\x9B\xE0\xE7\xF column=d:, timestamp=2022-02-09T19:21:38.287, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
F000017ed-e37a-4e60-b90f-93fc81e x00\x00\x00\x00\x01\x01@&333333@(333333\x821jac\xEB1\xB5
0ab0e
\x000\x922Q\xB1\xD4\x1E\xFF00001 column=d:, timestamp=2022-02-09T19:19:35.603, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
7ed-e35c-4d77-99e7-c6918a06c008 x00\x00\x00\x00\x01\x01@(333333@*333333\x822Lil\xF91\xB6
\x000\x99\x8A\xA5\xB6\xEBQ\x0200 column=d:, timestamp=2022-02-09T19:21:38.332, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
0017ed-e37a-4ebd-b3a5-a9c7399a63 x00\x00\x00\x00\x01\x01@,333333@.333333\x823mik\xE51\xB6
5b
\x000\x99\x8A\xA5\xB6\xEBQ\x0200 column=d:, timestamp=2022-02-09T20:02:28.707, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
0017ed-e5d1-4257-a75d-b0e2372954 x00\x00\x00\x00\x01\x01@,333333@.333333\x823mik\xE51\xB6
2e
\x020\x85\xD7\x8C\x9B\xE0\xE7\xF column=d:, timestamp=2022-02-09T19:19:35.334, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
F000017ed-e35c-4d77-a841-b3bcf6f x00\x00\x00\x00\x01\x01@&333333@(333333\x821jac\xEB1\xB5
aa8ac
\x020\x922Q\xB1\xD4\x1E\xFF00001 column=d:, timestamp=2022-02-09T19:21:38.285, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
7ed-e37a-4e60-9d7f-66988be48234 x00\x00\x00\x00\x01\x01@(333333@*333333\x822Lil\xF91\xB6
\x020\x99\x8A\xA5\xB6\xEBQ\x0200 column=d:, timestamp=2022-02-09T19:19:35.335, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
0017ed-e35c-4e9a-8600-97ed8d92c4 x00\x00\x00\x00\x01\x01@,333333@.333333\x823mik\xE51\xB6
8b
\x030\x85\xD7\x8C\x9B\xE0\xE7\xF column=d:, timestamp=2022-02-09T20:02:28.622, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
F000017ed-e5d1-41f8-ae71-84db58b x00\x00\x00\x00\x01\x01@&333333@(333333\x821jac\xEB1\xB5
9478f
\x030\x922Q\xB1\xD4\x1E\xFF00001 column=d:, timestamp=2022-02-09T20:02:28.662, value=\x03\x00\x04\x02\x00\x0E\x00 \x00"\x00&\x00(\
7ed-e5d1-41f8-a308-efcee8b70bf9 x00\x00\x00\x00\x01\x01@(333333@*333333\x822Lil\xF91\xB6
9 row(s)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spark-practise</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>geomesa</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<geomesa.version>3.1.0</geomesa.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.locationtech.geomesa</groupId>
<artifactId>geomesa-hbase-spark-runtime-hbase2_2.12</artifactId>
<version>3.3.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.locationtech.geomesa</groupId>
<artifactId>geomesa-spark-core_2.12</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-yarn_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<artifactSet>
<excludes>
<exclude>org.slf4j:*</exclude>
</excludes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
PS: 一定要使用maven-shade-plugin 插件 或其他插件将相关依赖(比如:geomesa-hbase-spark-runtime-hbase2_2.12)打入,否则会报错
spark 写入数据到Geomesa(Hbase)的更多相关文章
- MapReduce和Spark写入Hbase多表总结
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 大家都知道用mapreduce或者spark写入已知的hbase中的表时,直接在mapreduc ...
- Spark Streaming实时写入数据到HBase
一.概述 在实时应用之中,难免会遇到往NoSql数据如HBase中写入数据的情景.题主在工作中遇到如下情景,需要实时查询某个设备ID对应的账号ID数量.踩过的坑也挺多,举其中之一,如一开始选择使用NE ...
- spark 读取Geomesa(Hbase)数据
package com.grady.geomesa import org.apache.hadoop.conf.Configuration import org.apache.spark.SparkC ...
- Spark Streaming接收Kafka数据存储到Hbase
Spark Streaming接收Kafka数据存储到Hbase fly spark hbase kafka 主要参考了这篇文章https://yq.aliyun.com/articles/60712 ...
- spark读取hdfs上的文件和写入数据到hdfs上面
def main(args: Array[String]): Unit = { val conf = new SparkConf() conf.set("spark.master" ...
- HBase BulkLoad批量写入数据实战
1.概述 在进行数据传输中,批量加载数据到HBase集群有多种方式,比如通过HBase API进行批量写入数据.使用Sqoop工具批量导数到HBase集群.使用MapReduce批量导入等.这些方式, ...
- spark(三)从hbase取数据
前言 通过spark获取hbase数据的过程中,遇到了InputFormat.文章主要围绕InputFormat介绍.会牵扯到spark,mapreduce,hbase相关内容 InputFormat ...
- 聊一聊 HBase 是如何写入数据的?
hi,大家好,我是大D.今天继续了解下 HBase 是如何写入数据的,然后再讲解一下一个比较经典的面试题. Region Server 寻址 HBase Client 访问 ZooKeeper: 获取 ...
- Spark:DataFrame批量导入Hbase的两种方式(HFile、Hive)
Spark处理后的结果数据resultDataFrame可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...
随机推荐
- UiPath程序设计文档
1. [RPA之家]添加数据列UiPath.Core.Activities.AddDataColumn 链接: https://pan.baidu.com/s/1RRMw4voqJru-fJSoC3W ...
- python线程池 ThreadPoolExecutor 的用法及实战
写在前面的话 (https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 文章来源于互联网从Python3.2开始,标准库为我们提供了 concurrent.future ...
- [MRCTF2020]Ezpop-1|php序列化
1.打开题目获取到源代码信息,如下: Welcome to index.php <?php //flag is in flag.php //WTF IS THIS? //Learn From h ...
- day11 - 多线程
1内容 进程.线程介绍 Java中 线程的实现方式 Thread 类 Runnable 接口 Callable 接口 线程相关的方法 线程安全问题 - 同步技术 线程等待唤醒机制 进程(Process ...
- springboot集成redis集群
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- antd vue 折叠面板 v-for 循环点击无效
问题描述 实现一个折叠面板点击展开,但是必须点击两次才能展开,第一次无效 <a-collapse-panel v-for="(item, index) in dataMap" ...
- day01 Java_JVM,JCR,JDK
精华笔记: java开发环境: 编译运行过程: 编译期:.java源文件,经过编译,生成.class字节码文件 运行期:JVM加载.class并运行.class(0和1) 特点:跨平台.一次编程到处使 ...
- JVM学习之 内存结构
目录 一.引言 1.什么是JVM? 2.学习JVM有什么用 3.常见的JVM 4.学习路线 二.内存结构 1. 程序计数器 1.1 定义 1.2作用 2. 虚拟机栈 2.1定义 2.2栈内存溢出 2. ...
- 【达人专栏】还不会用Apache Dolphinscheduler吗,大佬用时一个月写出的最全入门教学【二】
02 Master启动流程 2.1 MasterServer的启动 在正式开始前,笔者想先鼓励一下大家.我们知道启动Master其实就是启动MasterServer,本质上与其他SpringBoot项 ...
- Luogu3803 【模板】多项式乘法(FFT)
为什么我这么弱 其实FFT也挺水的,一点数学基础加上细心即可.细节·技巧挺多. 递归 在TLE的边缘苦苦挣扎 #include <iostream> #include <cstdio ...