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可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...
随机推荐
- 【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
前言:随着工业化的进一步发展,制造业.工业自动化等多领域,都可能用到上位监控系统.而WPF在上位监控系统方面,应该算是当下最流行的前端框架之一了.而随着监控体系的不断完善与更新迭代,监控画面会变得越来 ...
- 搭建uipath
我对windows也不太熟,也是第一次安装Uipath Orchestrator,希望有问题指出一起交流,可以留言,Uipath中文qq交流群:4656303241. 下载镜像 windows ser ...
- Mybatis+SpringBoot 项目All elements are null
xml文件内容如下 查出来的集合长度是有的,但是会出现All elements are null 解决方案: 注意我的xml文件全部是这样的,并且我调用的sql返回值是 resultType=&qu ...
- adb工具
ADB:全称为Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具. 首先,下载ADB工具并安装: 下载:百度就有.下载后是个压缩包,将其拷贝到cm ...
- 关于 k 进制线性基
本质还是高斯消元,使其成为上三角矩阵.但是 \(k\) 不一定是质数. 但我们不需要保证已有数字不改变,只要维护的是一个上三角矩阵就行.所以我们可以利用更相减损让其中一个向量的最高位 \(= 0\) ...
- List集合_介绍&常用方法和ArrayList集合
List集合 我们掌握了Collection接口的使用后,再来看看Collection接口中的子类,他们都具备那些特性呢? 接下来,我们一起学习Collection中的常用几个子类(java.util ...
- Object类和Dome的新媒体类型
Object类 所有的类都是继承自Object的 Java Object 类是所有类的父类,也就是说 Java 的所有类都继承了 Object,子类可以使用 Object 的所有方法 Object 类 ...
- 牛客SQL刷题第三趴——SQL必知必会
01检索数据 SQL60 从 Customers 表中检索所有的 ID 编写 SQL 语句,从 Customers 表中检索所有的cust_id select * from Customers; SQ ...
- MySQL--用通配符进行过滤(LIKE操作符)
1.LIKE操作符 怎样搜索产品名中包含文本anvil的所有产品?用简单的比较操作符肯定不行,必须使用通配符.利用通配符可创建比较特定数据的搜索模式.在这个例子中,如果你想找出名称包含anvil的所有 ...
- vue2升级vue3:Vue Demij打通vue2与vue3壁垒,构建通用组件
如果你的vue2代码之前是使用vue-class-component 类组件模式写的.选择可以使用 https://github.com/facing-dev/vue-facing-decorator ...