簡體   English   中英

從 Amazon Ion 文件中提取“數據”

[英]Extracting “data” from Amazon Ion file

有沒有人使用過 Amazon Quantum Ledger Database (QLDB) Amazon ion文件? 如果是這樣,您知道如何提取“數據”部分來制定表格嗎? 也許使用 python 來抓取數據? 我正在嘗試從存儲在 s3 中的這些文件中獲取“數據”信息(我無權訪問 QLDB,因此無法直接查詢),然后將結果上傳到 Glue。

我正在嘗試使用 GLue 執行 ETL 作業,但 Glue 不喜歡 Amazon Ion 文件,因此我需要從這些文件中查詢數據或抓取文件以獲取相關信息。

謝謝。 PS :“數據”信息是指:

{
    PersonId:"4tPW8xtKSGF5b6JyTihI1U",
    LicenseNumber:"LEWISR261LL",
    LicenseType:"Learner",
    ValidFromDate:2016–12–20,
    ValidToDate:2020–11–15
}

參考https://docs.aws.amazon.com/qldb/latest/developerguide/working.userdata.html

您是否嘗試過使用Amazon Ion庫?

假設問題中提到的數據存在於名為“myIonFile.ion”的文件中,並且如果文件中只有離子對象,我們可以從文件中讀取數據,如下所示:

from amazon.ion import simpleion

file = open("myIonFile.ion", "rb")                    # opening the file
data = file.read()                                    # getting the bytes for the file
iondata = simpleion.loads(data, single_value=False)   # Loading as ion data
print(iondata['PersonId'])                            # should print "4tPW8xtKSGF5b6JyTihI1U"

Ion Cookbook中提供了有關使用離子庫的更多指導

此外,我不確定您的用例,但與 QLDB 的交互也可以通過直接依賴於 Ion 庫的QLDB 驅動程序來完成。

諾西菲韋,

AWS Glue能夠讀取 Amazon Ion 輸入。 但是,許多其他服務和應用程序不能,因此最好使用 Glue 將 Ion 數據轉換為 JSON。 請注意,Ion 是 JSON 的超集,將一些數據類型添加到 JSON,因此將 Ion 轉換為 JSON 可能會導致一些向下轉換

從 QLDB S3 導出訪問 QLDB 文檔的一種好方法是使用 Glue 提取文檔數據,將其作為 JSON 存儲在 S3 中,然后使用 Amazon Athena 進行查詢。 該過程將 go 如下:

  1. 將您的分類帳數據導出到 S3
  2. 創建Glue 爬網程序以對導出的數據進行爬網和編目。
  3. 運行Glue ETL 作業以從導出文件中提取修訂數據,將其轉換為 JSON,然后將其寫入 S3。
  4. 創建一個Glue 爬蟲來對提取的數據進行爬取和編目。
  5. 使用 Amazon Athena 查詢提取的文檔修訂數據。

看看下面的 PySpark 腳本。 它僅從 QLDB 導出文件中提取修訂元數據和數據負載。

QLDB 導出映射每個文檔的表,但與修訂數據分開。 您必須進行一些額外的編碼才能在 output 的修訂數據中包含表名。 下面的代碼不會這樣做,因此您最終會在 output 的一個表中獲得所有修訂。

另請注意,您將獲得導出數據中發生的任何修訂。 也就是說,您可能會獲得給定文檔 ID 的多個文檔修訂版本。 根據您對數據的預期用途,您可能需要弄清楚如何僅獲取每個文檔 ID 的最新版本。

from awsglue.transforms import *
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from pyspark.sql.functions import explode
from pyspark.sql.functions import col
from awsglue.dynamicframe import DynamicFrame

# Initializations
sc = SparkContext.getOrCreate()
glueContext = GlueContext(sc)

# Load data.  'vehicle-registration-ion' is the name of your database in the Glue catalog for the export data.  '2020' is the name of your table in the Glue catalog.
dyn0 = glueContext.create_dynamic_frame.from_catalog(database = "vehicle-registration-ion", table_name = "2020", transformation_ctx = "datasource0")

# Only give me exported records with revisions
dyn1 = dyn0.filter(lambda line: "revisions" in line)

# Now give me just the revisions element and convert to a Spark DataFrame.
df0 = dyn1.select_fields("revisions").toDF()

# Revisions is an array, so give me all of the array items as top-level "rows" instead of being a nested array field.
df1 = df0.select(explode(df0.revisions))

# Now I have a list of elements with "col" as their root node and the revision 
# fields ("data", "metadata", etc.) as sub-elements.  Explode() gave me the "col"
# root node and some rows with null "data" fields, so filter out the nulls.
df2 = df1.where(col("col.data").isNotNull())

# Now convert back to a DynamicFrame
dyn2 = DynamicFrame.fromDF(df2, glueContext, "dyn2")

# Prep and send the output to S3
applymapping1 = ApplyMapping.apply(frame = dyn2, mappings = [("col.data", "struct", "data", "struct"), ("col.metadata", "struct", "metadata", "struct")], transformation_ctx = "applymapping1")
datasink0 = glueContext.write_dynamic_frame.from_options(frame = applymapping1, connection_type = "s3", connection_options = {"path": "s3://YOUR_BUCKET_NAME_HERE/YOUR_DESIRED_OUTPUT_PATH_HERE/"}, format = "json", transformation_ctx = "datasink0")

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM