コード例 #1
0
ファイル: bigquery.py プロジェクト: amitsela/incubator-beam
 def convert_row_to_dict(self, row, schema):
   """Converts a TableRow instance using the schema to a Python dict."""
   result = {}
   for index, field in enumerate(schema.fields):
     value = None
     if isinstance(schema, bigquery.TableSchema):
       cell = row.f[index]
       value = from_json_value(cell.v) if cell.v is not None else None
     elif isinstance(schema, bigquery.TableFieldSchema):
       cell = row['f'][index]
       value = cell['v'] if 'v' in cell else None
     if field.mode == 'REPEATED':
       if value is None:
         # We receive 'None' for repeated fields without any values when
         # 'flatten_results' is 'False'.
         # When 'flatten_results' is 'True', we receive individual values
         # instead of a list of values hence we do not hit this condition.
         # We return an empty list here instead of 'None' to be consistent with
         # other runners and to be backwards compatible to users.
         result[field.name] = []
       else:
         result[field.name] = [self._convert_cell_value_to_dict(x['v'], field)
                               for x in value]
     elif value is None:
       if not field.mode == 'NULLABLE':
         raise ValueError('Received \'None\' as the value for the field %s '
                          'but the field is not NULLABLE.', field.name)
       result[field.name] = None
     else:
       result[field.name] = self._convert_cell_value_to_dict(value, field)
   return result
コード例 #2
0
ファイル: bigquery_tools.py プロジェクト: eralmas7/beam
 def convert_row_to_dict(self, row, schema):
   """Converts a TableRow instance using the schema to a Python dict."""
   result = {}
   for index, field in enumerate(schema.fields):
     value = None
     if isinstance(schema, bigquery.TableSchema):
       cell = row.f[index]
       value = from_json_value(cell.v) if cell.v is not None else None
     elif isinstance(schema, bigquery.TableFieldSchema):
       cell = row['f'][index]
       value = cell['v'] if 'v' in cell else None
     if field.mode == 'REPEATED':
       if value is None:
         # Ideally this should never happen as repeated fields default to
         # returning an empty list
         result[field.name] = []
       else:
         result[field.name] = [self._convert_cell_value_to_dict(x['v'], field)
                               for x in value]
     elif value is None:
       if not field.mode == 'NULLABLE':
         raise ValueError('Received \'None\' as the value for the field %s '
                          'but the field is not NULLABLE.' % field.name)
       result[field.name] = None
     else:
       result[field.name] = self._convert_cell_value_to_dict(value, field)
   return result
コード例 #3
0
 def encode(self, table_row):
   if self.table_schema is None:
     raise AttributeError(
         'The TableRowJsonCoder requires a table schema for '
         'encoding operations. Please specify a table_schema argument.')
   try:
     return json.dumps(
         collections.OrderedDict(
             zip(self.field_names,
                 [from_json_value(f.v) for f in table_row.f])),
         allow_nan=False)
   except ValueError as e:
     raise ValueError('%s. %s' % (e, JSON_COMPLIANCE_ERROR))
コード例 #4
0
 def test_true_from(self):
   self.assertEquals(True, from_json_value(to_json_value(True)))
コード例 #5
0
 def test_string_from(self):
   self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
コード例 #6
0
 def test_long_value(self):
   self.assertEquals(long(27), from_json_value(to_json_value(long(27))))
コード例 #7
0
ファイル: json_value_test.py プロジェクト: zhoubh/beam
 def test_none_from(self):
     self.assertIsNone(from_json_value(to_json_value(None)))
コード例 #8
0
 def test_long_value(self):
   num = 1 << 63 - 1
   self.assertEquals(num, from_json_value(to_json_value(num)))
コード例 #9
0
 def test_none_from(self):
   self.assertIsNone(from_json_value(to_json_value(None)))
コード例 #10
0
 def test_float_from(self):
   self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
コード例 #11
0
 def test_true_from(self):
   self.assertEquals(True, from_json_value(to_json_value(True)))
コード例 #12
0
 def test_string_from(self):
   self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
コード例 #13
0
ファイル: json_value_test.py プロジェクト: zhoubh/beam
 def test_long_value(self):
     num = 1 << 63 - 1
     self.assertEquals(num, from_json_value(to_json_value(num)))
コード例 #14
0
ファイル: json_value_test.py プロジェクト: zhoubh/beam
 def test_large_integer(self):
     num = 1 << 35
     self.assertEquals(num, from_json_value(to_json_value(num)))
コード例 #15
0
 def test_false_from(self):
   self.assertEquals(False, from_json_value(to_json_value(False)))
コード例 #16
0
 def test_int_from(self):
   self.assertEquals(-27, from_json_value(to_json_value(-27)))
コード例 #17
0
 def test_false_from(self):
   self.assertEquals(False, from_json_value(to_json_value(False)))
コード例 #18
0
 def test_with_type(self):
   rt = from_json_value(to_json_value('abcd', with_type=True))
   self.assertEquals('http://schema.org/Text', rt['@type'])
   self.assertEquals('abcd', rt['value'])
コード例 #19
0
 def test_int_from(self):
   self.assertEquals(-27, from_json_value(to_json_value(-27)))
コード例 #20
0
 def test_large_integer(self):
   num = 1 << 35
   self.assertEquals(num, from_json_value(to_json_value(num)))
コード例 #21
0
 def test_float_from(self):
   self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
コード例 #22
0
 def test_with_type(self):
   rt = from_json_value(to_json_value('abcd', with_type=True))
   self.assertEquals('http://schema.org/Text', rt['@type'])
   self.assertEquals('abcd', rt['value'])
コード例 #23
0
ファイル: json_value_test.py プロジェクト: qq840873731/beam-1
 def test_long_value(self):
     self.assertEquals(long(27), from_json_value(to_json_value(long(27))))