首页 > 解决方案 > 如何在 Jython Evaluator 中获取 StreamSets 记录字段类型

问题描述

我有一个 StreamSets 管道,我使用 JDBC 组件作为源从远程 SQL Server 数据库中读取数据,并将数据放入 Hive 和 Kudu 数据湖。

我在使用 Binary Columns 类型时遇到了一些问题,因为 Impala 中没有 Binary 类型支持,我用它来访问 Hive 和 Kudu。

我决定将 Binary 类型列(在管道中作为 Byte_Array 类型流动)转换为 String 并像这样插入它。

我尝试使用字段类型转换器元素将所有 Byte_Array 类型转换为字符串,但它不起作用。所以我使用了一个 Jython 组件将所有 arr.arr 类型转换为 String。它工作正常,直到我在该字段上获得 Null 值,因此 Jython 类型为 None.type 并且我无法检测到 Byte_Array 类型并且无法将其转换为 String。所以我无法将它插入 Kudu。

任何帮助如何在 Jython Evaluator 中获取 StreamSets 记录字段类型?或者针对我面临的问题有什么建议的解决方法?

标签: hivebigdatajythonkudustreamsets

解决方案


您需要使用sdcFunctions.getFieldNull()来测试该字段是否为NULL_BYTE_ARRAY. 例如:

import array

def convert(item):
  return ':-)'

def is_byte_array(record, k, v):
  # getFieldNull expect a field path, so we need to prepend the '/'
  return (sdcFunctions.getFieldNull(record, '/'+k) == NULL_BYTE_ARRAY 
          or (type(v) == array.array and v.typecode == 'b'))

for record in records:
  try:
    record.value = {k: convert(v) if is_byte_array(record, k, v) else v 
                    for k, v in record.value.items()}
    output.write(record)

  except Exception as e:
    error.write(record, str(e))

在此处输入图像描述


推荐阅读