Просмотр исходного кода

[doc2] Utility to convert beeswax Hive queries to the new Notebook format

Romain Rigaux 10 лет назад
Родитель
Сommit
ef8e68e

+ 31 - 0
desktop/core/src/desktop/models.py

@@ -36,6 +36,7 @@ from desktop import appmanager
 from desktop.lib.i18n import force_unicode
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.redaction import global_redaction_engine
+from notebook.models import make_notebook
 
 
 LOG = logging.getLogger(__name__)
@@ -993,3 +994,33 @@ def get_data_link(meta):
     link = '/metastore/table/%(database)s/%(table)s' % meta # Could also add col=val
 
   return link
+
+
+def import_beeswax_query(bquery):
+  design = bquery.get_design()
+
+  return make_notebook(
+      name=bquery.name,
+      description=bquery.desc,
+      editor_type=_convert_type(bquery.type),
+      statement=design.hql_query,
+      status='ready',
+      files=design.file_resources,
+      functions=design.functions,
+      settings=design.settings
+  )
+
+def _convert_type(btype):
+  from beeswax.models import HQL, IMPALA, RDBMS, SPARK
+
+  if btype == HQL:
+    return 'hive'
+  elif btype == IMPALA:
+    return 'impala'
+  elif btype == RDBMS: # We should instead get the 'type' (https://github.com/cloudera/hue/blob/master/desktop/libs/librdbms/src/librdbms/design.py#L47
+    return 'mysql' # postgres, sqlite, oracle
+  elif btype == SPARK: # We should not import
+    return 'spark'
+  else:
+    return 'hive'
+

+ 67 - 0
desktop/core/src/desktop/tests_doc2.py

@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from nose.tools import assert_equal
+from django.contrib.auth.models import User
+
+from desktop.lib.django_test_util import make_logged_in_client
+from desktop.lib.test_utils import grant_access
+
+from beeswax.models import SavedQuery
+from beeswax.design import hql_query
+from desktop.models import import_beeswax_query
+
+
+class TestDocument2(object):
+
+  def setUp(self):
+    self.client = make_logged_in_client(username="doc2", groupname="doc2", recreate=True, is_superuser=False)
+    self.user = User.objects.get(username="doc2")
+    grant_access("doc2", "doc2", "beeswax")
+
+
+  def test_document_create(self):
+    sql = 'SELECT * FROM sample_07'
+
+    design = hql_query(sql)
+
+    # is_auto
+    # is_trashed
+    # is_redacted
+    old_query = SavedQuery.objects.create(
+        type=SavedQuery.TYPES_MAPPING['hql'],
+        owner=self.user,
+        data=design.dumps(),
+        name='See examples',
+        desc='Example of old format'
+    )
+
+    new_query = import_beeswax_query(old_query)
+    new_query_data = new_query.get_data()
+
+    assert_equal('query-hive', new_query_data['type'])
+    assert_equal('See examples', new_query_data['name'])
+    assert_equal('Example of old format', new_query_data['description'])
+
+    assert_equal('ready', new_query_data['snippets'][0]['status'])
+    assert_equal('See examples', new_query_data['snippets'][0]['name'])
+    assert_equal('SELECT * FROM sample_07', new_query_data['snippets'][0]['statement_raw'])
+
+    assert_equal([], new_query_data['snippets'][0]['properties']['settings'])
+    assert_equal([], new_query_data['snippets'][0]['properties']['files'])
+    assert_equal([], new_query_data['snippets'][0]['properties']['functions'])

+ 4 - 4
desktop/libs/notebook/src/notebook/models.py

@@ -50,11 +50,12 @@ def escape_rows(rows, nulls_only=False):
   return data
 
 
-def make_notebook(name='Browse', editor_type='hive', statement='', status='ready', files=None, functions=None, settings=None):
+def make_notebook(name='Browse', description='', editor_type='hive', statement='', status='ready', files=None, functions=None, settings=None):
   editor = Notebook()
 
   editor.data = json.dumps({
-    'description': '',
+    'name': name,
+    'description': description,
     'sessions': [
       {
          'type': editor_type,
@@ -83,8 +84,7 @@ def make_notebook(name='Browse', editor_type='hive', statement='', status='ready
          'database': 'default',
          'result': {}
       }
-    ],
-    'name': name
+    ]
   })
   
   return editor