コード例 #1
0
    def testReadSQLInstance(self):
        test_table = tn('pyodps_t_tmp_read_sql_instance')
        self.odps.delete_table(test_table, if_exists=True)
        table = self.odps.create_table(test_table,
                                       schema=Schema.from_lists(['size'],
                                                                ['bigint']),
                                       if_not_exists=True)
        self.odps.write_table(table, 0,
                              [table.new_record([1]),
                               table.new_record([2])])
        self.odps.write_table(table, [
            table.new_record([3]),
        ])

        instance = self.odps.execute_sql('select * from %s' % test_table)
        with instance.open_reader(table.schema) as reader:
            self.assertEqual(len(list(reader[::2])), 2)
        with instance.open_reader(table.schema) as reader:
            self.assertEqual(len(list(reader[1::2])), 1)

        hints = {'odps.sql.mapper.split.size': '16'}
        instance = self.odps.run_sql('select sum(size) as count from %s' %
                                     test_table,
                                     hints=hints)

        while len(instance.get_task_names()) == 0 or \
                compat.lvalues(instance.get_task_statuses())[0].status == Instance.Task.TaskStatus.WAITING:
            continue

        while True:
            progress = instance.get_task_progress(instance.get_task_names()[0])
            if len(progress.stages) == 0:
                continue
            self.assertGreater(
                len(progress.get_stage_progress_formatted_string().split()), 2)
            break

        instance.wait_for_success()
        self.assertEqual(json.loads(instance.tasks[0].properties['settings']),
                         hints)
        self.assertIsNotNone(instance.tasks[0].summary)

        with instance.open_reader(Schema.from_lists(['count'], ['bigint']),
                                  tunnel=False) as reader:
            records = list(reader)
            self.assertEqual(len(records), 1)
            self.assertEqual(records[0]['count'], 6)

        with instance.open_reader(tunnel=True) as reader:
            records = list(reader)
            self.assertEqual(len(records), 1)
            self.assertEqual(records[0]['count'], 6)

        with instance.open_reader(tunnel=False) as reader:
            records = list(reader)
            self.assertEqual(len(records), 1)
            self.assertEqual(records[0]['count'], '6')

        table.drop()
コード例 #2
0
    def testReadSQLInstance(self):
        test_table = tn('pyodps_t_tmp_read_sql_instance')
        self.odps.delete_table(test_table, if_exists=True)
        table = self.odps.create_table(
            test_table, schema=Schema.from_lists(['size'], ['bigint']), if_not_exists=True)
        self.odps.write_table(
            table, 0, [table.new_record([1]), table.new_record([2])])
        self.odps.write_table(table, [table.new_record([3]), ])

        instance = self.odps.execute_sql('select * from %s' % test_table)
        with instance.open_reader(table.schema) as reader:
            self.assertEqual(len(list(reader[::2])), 2)
        with instance.open_reader(table.schema) as reader:
            self.assertEqual(len(list(reader[1::2])), 1)

        hints = {'odps.sql.mapper.split.size': 16}
        instance = self.odps.run_sql('select sum(size) as count from %s' % test_table, hints=hints)

        while len(instance.get_task_names()) == 0 or \
                compat.lvalues(instance.get_task_statuses())[0].status == Instance.Task.TaskStatus.WAITING:
            continue

        while True:
            progress = instance.get_task_progress(instance.get_task_names()[0])
            if len(progress.stages) == 0:
                continue
            self.assertGreater(len(progress.get_stage_progress_formatted_string().split()), 2)
            break

        instance.wait_for_success()

        with instance.open_reader(Schema.from_lists(['count'], ['bigint'])) as reader:
            records = list(reader)
            self.assertEqual(len(records), 1)
            self.assertEqual(records[0]['count'], 6)

        with instance.open_reader() as reader:
            records = list(reader)
            self.assertEqual(len(records), 1)
            self.assertEqual(records[0]['count'], '6')

        table.drop()