BigQuery UDF减少所有行

时间:2015-11-10 11:50:58

标签: google-bigquery udf

我定义了以下UDF(注意我的表格有一个' Id'以及'阅读'对象与子字段' RawHex'):

// UDF definition
function hexdecode(row, emit) {
  emit({
    Id: row.Id,
    converted: decodeHelper(row.Reading.Raw)
  });
}

// Helper function with error handling
function decodeHelper(s) {
  try {
    return parseInt(s, 16);
  } catch (ex) {
    return s;
  }
}

// UDF registration
bigquery.defineFunction(
  'hexdecode',  // Name used to call the function from SQL

  ['Id', 'Reading.Raw'],  // Input column names

  // JSON representation of the output schema
  [{name: 'Id', type: 'STRING'},
   {name: 'converted', type: 'INTEGER'}],

  hexdecode  // The function reference
);

我的查询是:

select Id, converted from 
hexdecode(
select r.Id, r.Reading.Raw from Example.TagRaw2 r
)

从将以下JSON上传到表Example.TagRaw2

生成
{"Id":"ABC","Reading":{"Raw":"0004"}}
{"Id":"CDE","Reading":{"Raw":"000b"}}

我希望这会将我的列从Hex转换为Integer,但是如果返回一个几乎没有意义的单个条目。

知道我做错了吗?

编辑:我添加了一个示例JSON上传以尝试重现问题..但它现在似乎工作。如果我能弄清楚最初的错误,我会再次更新,但上面的UDF似乎完全符合我的要求。

1 个答案:

答案 0 :(得分:2)

问题 - 你的阅读对象是一个重复的列吗?

如果是这样,你需要做这样的事情:

function hexdecode(r, emit) {
  for (var i = 0; i < r.reading.length; ++i) {
    emit({ tag: r.Id, num: parseInt(r.reading[i].Raw, 16) });
  }
}

bigquery.defineFunction(
  'hexdecode',
  ['Id', 'reading.Raw'],
  [{name: 'tag', type: 'string'},
   {name: 'num', type: 'integer'}],
  hexdecode
);

如果您尝试访问r.reading.Raw,则会尝试访问JavaScript数组的Raw属性。这是完全有效的JavaScript,但它肯定不是你想要的,因为值是未定义的(null)。

一个方便的技巧是打印出输入记录的JSON字符串,以查看UDF看到的输入内容:

bigquery.defineFunction(
  'jsonifyObj',
  ['Id', 'reading.Raw'],
  [{name: 'obj', type: 'string'}],
  function(r, emit) { emit({obj: JSON.stringify(r)}); }
);

这有助于调试问题。我怀疑在这种情况下的问题是你的数据看起来更像是

[{"Id":"ABC","Reading":[{"Raw":"0004"}, {"Raw": "00ff"}]},
 {"Id":"CDE","Reading":[{"Raw":"000b"}, {"Raw": "0012"}]}]

--------更新2015-11-17 --------

您的代码有一些问题,请注意以下注释:

function hexdecode(row, emit) {
 for (var i = 0; i < row.reading.length; ++i) {
   // NOTE: tag and row.Id are wrong, this must be TagId and row.TagId based on your input and output specifications
   emit({ tag: row.Id,
          times: row.reading[i].Timestamp,
          // NOTE: You're making a recursive call here!  You should be calling decodeHelper() not hexdecode().
          convert: hexdecode(row.reading[i].RawCO) });
  }
}


// Helper function with error handling
function decodeHelper(s) {
  try {
    return parseInt(s, 16);
  } catch (ex) {
    return s;
  }
}

// UDF registration
bigquery.defineFunction(
  'hexdecode',  // Name used to call the function from SQL

  ['TagId', 'reading.Timestamp', 'reading.RawCO'],  // Input column names

  // JSON representation of the output schema
  [{name: 'TagId', type: 'STRING'},
   {name: 'times', type: 'INTEGER'},
   {name: 'convert', type: 'INTEGER'}],

  hexdecode  // The function reference
);

您的嵌套选择返回0行,因此我更新到以下SQL:

select 
  TagID, times, convert 
from  hexdecode(
select r.TagId, r.Reading.Timestamp, r.Reading.RawCO from [table.Readings] r where r.Reading.RawCO is not NULL and r.Reading.PPM is
 not NULL
 and r.TagId = 'Tag_00000000' 
 )

以下是更正后的代码:

function hexdecode(row, emit) {
  for (var i = 0; i < row.reading.length; ++i) {
    emit({TagId: row.TagId, times: row.reading[i].Timestamp, convert: decodeHelper(row.reading[i].RawCO)});
  }
}

// Helper function with error handling
function decodeHelper(s) {
  try {
    return parseInt(s, 16);
  } catch (ex) {
    return s;
  }
}

// UDF registration
bigquery.defineFunction(
  'hexdecode',  // Name used to call the function from SQL

  ['TagId', 'reading.Timestamp', 'reading.RawCO'],  // Input column names

  // JSON representation of the output schema
  [{name: 'TagId', type: 'STRING'},
   {name: 'times', type: 'INTEGER'},
   {name: 'convert', type: 'INTEGER'}],

  hexdecode  // The function reference
);