Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带AWS胶水的目录嵌套XML_Xml_Aws Glue - Fatal编程技术网

带AWS胶水的目录嵌套XML

带AWS胶水的目录嵌套XML,xml,aws-glue,Xml,Aws Glue,我有一个XML源,我正试图用Glue对其进行编目: <?xml version="1.0"?> <catalog> <book id="bk101"> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</pri

我有一个XML源,我正试图用Glue对其进行编目:

    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications with XML.</description>
          <authors>
            <author>
              <name>Gambardella, Matthew</name>
            </author>
          </authors>
       </book>
       ...

XML开发人员指南
电脑类
44.95
2000-10-01
深入了解如何使用XML创建应用程序。
马修·甘巴德拉
...

图书条目被很好地拾取,但是我如何配置爬虫/分类器来识别
下的嵌套?

您不需要指定分类器,除非您希望它只捕获该行标记。然后您只需要将分类器设置为author。但是,您将无法获得其余的数据

如果您使用爬虫来推断数据的模式。它将以结构类型捕获authors中的内容。见下图:

现在,您可以在粘合作业中映射字段:

或者只需访问作业代码中的字段本身,如下所示:


我们在将嵌套的XML数据加载到
DynamicFrame
中时遇到了很多问题。问题是您不能使用标准的Spark(在我们的例子中是PySpark)
XPATH
hiveddl语句来加载
DataFrame
DynamicFrame
,在AWS GLUE的例子中)。 我们的解决方案是加载
动态框架
,只使用表属性中的简单且仅
行标记
参数(而不是如爬虫程序所建议的Serde参数)。这将为您提供一个
dynamicRecord['MySingleParsedField']
,然后您可以在其中一个Spark(GLUE)作业中迭代以填充新字段。这里有这样一个迭代代码的工作示例:

def Map_Inital_Fields(dynamicRecord):
    nested = []
    for item in dynamicRecord['MySingleParsedField']:
      nested.append(item)
    dynamicRecord['title'] = [item.get('title') for item in nested[0].get('book')][0]
    dynamicRecord['price'] = [item.get('price') for item in nested[0].get('book')][0]
    del dynamicRecord['MySingleParsedField']

    return dynamicRecord
mapfields01 = Map.apply(frame = selectfields2, f = Map_Inital_Fields, transformation_ctx = "mapfields01")
这只是一个示例,但基本上,一旦您将xml解析对象作为
DynamicFrame
中的字段,就可以将其视为Python对象(dic),然后根据需要对其进行修改