【数据库】时序数据库部署方案

数据库部署方案

数据库持久化存储Cassandra,后端驱动 GeoMesa,可视化界面 GeoServer

Quick_Start 部署方案

尝试配置一下官方提供的快速入门指南,过程如下,参考链接GeoMesa Cassandra Quick Start

版本描述

  • Cassandra版本号:3.11.3,此处版本使用默认版本,避免后续会出现的很多问题;并且注意GeoMesa目前只支持Cassandra3
  • Geomesa-cassandra版本号:2.12-5.1.0
  • GeoServer版本号:2.26.0
  • Ubuntu: 22.04.5 LTS

操作步骤

  1. 安装Cassandra, GeoMesa, GeoServer

  2. 数据写入 + 查询

    java -cp geomesa-tutorials-cassandra/geomesa-tutorials-cassandra-quickstart/target/geomesa-tutorials-cassandra-quickstart-5.2.0-SNAPSHOT.jar \ org.geomesa.example.cassandra.CassandraQuickStart \ --cassandra.contact.point localhost:9042 \ --cassandra.keyspace store \ --cassandra.catalog store_test1

    image-20241103214033131

    image-20241103214100395

  3. 可视化工具一:切换到geomesa-cassandra目录,采用 geomesa-cassandra 导出,【该方法我并不能成功显示】

bin/geomesa-cassandra export 
--output-format leaflet \
--contact-point localhost:9042 \
--key-space store \
--catalog store_test \
-f gdelt-quickstart \
-o www.store_test.html --自定义导出路径

image-20241103214730986

image-20241103215301629

  1. 可视化工具二:配置GeoServer,并查询数据,新建Workspace -- 新建Store(重要,下图) -- 新建Layer,这一步完成会自动跳出页面,出现上述写入的数据结果,点击Publish -- 查看Layer Preview

    新建Store

  2. 可视化结果展示

    image-20241103220944553

手动部署方案

Quick Start中给出的GeoMesa版本为geomesa-tutorials,此处安装的版本为geomesa-cassandra

版本描述

  • Geomesa-cassandra 4.0.4
  • Geoserver 2.22.2
  • cassandra 3.11.3

操作步骤

  1. 安装必备软件Geomesa-cassandra, Geoserver, cassandra

  2. 开启后端服务Cassandra, GeoServer,接下来即将操作GeoMesa-cassandra,将数据通过GeoMesa-cassandra构建索引并写入Cassandra

  3. 研究数据存储格式,此处采用NGSIM-us101数据集,【修改时间戳,并添加经纬度(GeoMesa投影方式只能采用WGS84,即只能用经纬度进行映射)】,如下:

    Vehicle_ID,Frame_ID,Total_Frames,Global_Time,Local_X,Local_Y,Global_X,Global_Y,v_length,v_Width,v_Class,v_Vel,v_Acc,Lane_ID,Preceding,Following,Space_Headway,Time_Headway,Location,Longitude,Latitude
    515,2330,1123,2005-06-15T23:07:55.000,30.034,188.062,6451203.729,1873252.549,13.0,6.9,2,23.31,2.05,3,500,523,119.1,5.11,us-101,-68.71434225077688,11.415956719653371
    2127,6459,567,2005-06-15T23:00:24.800,19.632,1775.614,6452425.122,1872172.475,13.5,6.9,2,37.52,11.2,2,2124,2132,48.92,1.3,us-101,-68.70838834225913,11.407843920688174
    

    时间戳转化 + 坐标转化为经纬度格式

    '''
    坐标转化,将全球坐标转化为 4236 格式的坐标
    '''
    
    import pandas as pd
    from pyproj import Transformer
    from datetime import datetime
    
    # 输入和输出文件路径
    input_file = "../data/raw/us101_full.csv"
    output_file = "./us101_full_data_converted.csv"
    
    # 读取CSV文件
    df = pd.read_csv(input_file)
    
    # 将 时间戳 转化为 '%Y-%m-%dT%H:%M:%S.%f' 格式
    df['Global_Time'] = df['Global_Time'].apply(lambda x: datetime.fromtimestamp(x / 1000).strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3])
    
    # 创建坐标转换器,从 NAD83 / UTM Zone 11N (EPSG:26911) 转换到 WGS84 (EPSG:4326)
    transformer = Transformer.from_crs("EPSG:26911", "EPSG:4326", always_xy=True)
    
    # 使用转换器将 Global_X 和 Global_Y 转换为经纬度
    df[['Longitude', 'Latitude']] = df.apply(
        lambda row: pd.Series(transformer.transform(row['Global_X'], row['Global_Y'])),
        axis=1
    )
    
    # 将结果保存到新的CSV文件
    df.to_csv(output_file, index=False)
    
    print(f"转换完成,结果已保存到 {output_file}")
    
  4. Cassandra中的操作,启动 -- 准备好keyspace

    cqlsh:vehicle_test> CREATE KEYSPACE vehicle_us101 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
    
    cqlsh:vehicle_test> use vehicle_us101;
    

    image-20241107150141551

  5. GeoMesa-Cassandra中操作,【重要】

    新建sft文件 -- reference.conf 以及 converter文件 -- converter.conf,并设置两个文件的权限为可执行

    image-20241107151927498

    sft文件定义了Schema,表示数据的存储格式; converter文件用于将原先不符合Schema的数据,转化为存储的数据

    1. sft文件 -- reference.conf

      geomesa {
        sfts {
          vehicle_us101 = {
            type-name = "vehicle_us101"
            attributes = [
              { name = "vehicle_id", type = "Integer" }
              { name = "global_time", type = "Date" }               # 使用日期类型存储时间戳
              { name = "local_x", type = "Float" }                  # 局部坐标 X
              { name = "local_y", type = "Float" }                  # 局部坐标 Y
              { name = "global_x", type = "Float" }                 # 全球坐标 X
              { name = "global_y", type = "Float" }                 # 全球坐标 Y
              { name = "v_length", type = "Float" }                 # 车辆长度
              { name = "v_width", type = "Float" }                  # 车辆宽度
              { name = "v_class", type = "Integer" }                # 车辆类型
              { name = "v_vel", type = "Float" }                    # 车辆速度
              { name = "v_acc", type = "Float" }                    # 车辆加速度
              { name = "lane_id", type = "Integer" }                # 车道编号
              { name = "preceding", type = "Integer" }              # 前车 ID
              { name = "following", type = "Integer" }              # 后车 ID
              { name = "space_headway", type = "Float" }            # 空间间距
              { name = "time_headway", type = "Float" }             # 时间间隔
              { name = "location", type = "String" }                # 路段名称
              { name = "geom", type = "Point", srid = 4326 }        # 地理位置点,用于空间索引
            ]
          }
        }
      }
      
    2. converter.conf 文件内容,参考链接:日期函数分隔文本转换器_用法示例8.7. 定义简单特征转换器

      geomesa.converters.vehicle_us101 : {
          "fields" : [
              {
                  "name" : "vehicle_id",
                  "transform" : "toInt($1)"
              },
              {
                  "name" : "global_time",
                  "transform" : "dateHourMinuteSecondMillis($4)"
              },
              {
                  "name" : "local_x",
                  "transform" : "toFloat($5)"
              },
              {
                  "name" : "local_y",
                  "transform" : "toFloat($6)"
              },
              {
                  "name" : "global_x",
                  "transform" : "toFloat($7)"
              },
              {
                  "name" : "global_y",
                  "transform" : "toFloat($8)"
              },
              {
                  "name" : "v_length",
                  "transform" : "toFloat($9)"
              },
              {
                  "name" : "v_width",
                  "transform" : "toFloat($10)"
              },
              {
                  "name" : "v_class",
                  "transform" : "toInt($11)"
              },
              {
                  "name" : "v_vel",
                  "transform" : "toFloat($12)"
              },
              {
                  "name" : "v_acc",
                  "transform" : "toFloat($13)"
              },
              {
                  "name" : "lane_id",
                  "transform" : "toInt($14)"
              },
              {
                  "name" : "preceding",
                  "transform" : "toInt($15)"
              },
              {
                  "name" : "following",
                  "transform" : "toInt($16)"
              },
              {
                  "name" : "space_headway",
                  "transform" : "toFloat($17)"
              },
              {
                  "name" : "time_headway",
                  "transform" : "toFloat($18)"
              },
              {
                  "name" : "location",
                  "transform" : "$19"
              },
              {
                  "name" : "Longitude",
                  "transform" : "toDouble($20)"
              },
              {
                  "name" : "Latitude",
                  "transform" : "toDouble($21)"
              },
              {
                  "name" : "geom",
                  "transform" : "point($Latitude, $Longitude)"  # 使用经度和纬度生成几何点
              }
          ],
          "format" : "QUOTED",
          "id-field" : "md5(string2bytes($0))",
          "type" : "delimited-text",
          "options" : {
              "encoding" : "UTF-8",
              "error-mode" : "skip-bad-records",
              "parse-mode" : "incremental",
              "skip-lines" : 1,
              "validators" : [
                  "index"
              ]
          }
      }
      
    3. 为上述文件设置可执行权限,chmod +x ./文件路径,图文不符

      image-20241106000711229

  6. 直接插入数据,也可以先新建一个Scheme,插入数据之前会自动创建Schema

    ./geomesa-cassandra ingest -c vehicle_us101 -P localhost:9042 -k vehicle_us101 -s ../conf/vehicle-us101/reference.conf -C ../conf/vehicle-us101/converter.conf ~/Downloads/us101_full_data_converted.csv
    
    ## 解释说明
    ./geomesa-cassandra ingest    【插入数据】
    -c vehicle_us101  【新定义的Catelog名称,表示GeoMesa中的存储位置】
    -P localhost:9042  【Cassandra中的IP:端口号】
    -k vehicle_us101  【之前定义的Cassandra中的keyspace名称】
    -s ../conf/vehicle-us101/reference.conf 【sft文件路径】
    -C ../conf/vehicle-us101/converter.conf 【converter文件路径】
    ~/Downloads/us101_full_data_converted.csv  【插入文件】
    
    ## 这些参数需要记住,因为在GeoServer连接GeoMesa-cassandra时需要使用
    

    image-20241107153412306

    1. 创建 schema【该命令与上述逻辑不符,语法可参考】

      ./geomesa-cassandra create-schema -c vehicle_test -P localhost:9042 -k vehicle_data -s ../conf/sfts/vehicle-data/reference.conf

      image-20241105234622432

    2. 销毁 schema

      ./geomesa-cassandra remove-schema
      -c vehicle_us101
      -P localhost:9042
      -k vehicle_us101  【上面这几个参数与之前解释的含义相同】
      -f vehicle_us101  【-f表示sft的名称,sft文件定义是的type-name】
      

      image-20241107153334726

  7. 数据写入成功

    image-20241107225108230

  8. 在Cassandra数据库中查看写入数据,分别构建了三种索引,分别为 z2索引 , z3索引 以及 id索引

    image-20241107225223702

    任意查看一张数据表

    image-20241107225359582

  9. 使用GeoServer可视化方式查看

    image-20241108093058405

posted @ 2024-11-10 00:19  是你亦然  阅读(109)  评论(0)    收藏  举报