浏览代码

HUE-7703 [solr] Add collection sampling to the notebook API

Do not list dynamic fields values currently.

e.g

     $.post("/notebook/api/sample/sfdc/yelp_demo", {
        snippet: ko.mapping.toJSON({"type":"solr"})
      }, function(data) {
        console.log(ko.mapping.toJSON(data));
      });

{"status":0,"headers":["_root_","_text_","_version_","business_id","cool","date","full_address","funny","id","latitude","longitude","name","neighborhoods","open","review_count","stars","state","text","type","useful","user_id"],"rows":[["","",1580263808127991800,"9yKzy9PApeiPPOUJEtnvkg",2,"2011-01-26T08:00:00Z","[u'6106 S 32nd St Phoenix, AZ 85042']",0,"fWKvX83p0-ka4JS3dc6E5A",33.3907928467,-112.012504578,"[u'Morning Glory Cafe']","[]",true,116,4,"AZ","[u'My wife took me here on my birthday for breakfast and it was excellent.  The weather was perfect which made sitting outside overlooking their grounds an absolute pleasure.  Our waitress was excellent and our food arrived quickly on the semi-busy Saturday morning.
...

(cherry picked from commit 47f332063a1dc85fbc845c5e7e92c8a76063b99b)
Romain Rigaux 8 年之前
父节点
当前提交
c9a3f79a11
共有 1 个文件被更改,包括 24 次插入2 次删除
  1. 24 2
      desktop/libs/notebook/src/notebook/connectors/solr.py

+ 24 - 2
desktop/libs/notebook/src/notebook/connectors/solr.py

@@ -164,11 +164,15 @@ class SolrApi(Api):
     assist = Assist(self, self.user, db)
     response = {'status': -1}
 
-    sample_data = assist.get_sample_data(database, table, column)
+    if snippet.get('source') == 'sql':
+      sample_data = assist.get_sample_data_sql(database, table, column)
+    else:
+      sample_data = assist.get_sample_data(database, table, column)
 
     if sample_data:
       response['status'] = 0
       response['headers'] = sample_data['headers']
+      response['full_headers'] = sample_data.get('full_headers')
       response['rows'] = sample_data['rows']
     else:
       response['message'] = _('Failed to get sample data.')
@@ -200,6 +204,24 @@ class Assist():
     return [{'name': field['name'], 'type': field['type'], 'comment': ''} for field in self.db.schema_fields(table)['fields']]
 
   def get_sample_data(self, database, table, column=None):
+    # Note: currently ignores dynamic fields
+    full_headers = self.get_columns(database, table)
+    headers = [col['name'] for col in full_headers]
+
+    records = self.db.select(table, rows=100)['response']['docs']
+    rows = [[record.get(col, '') for col in headers] for record in records]
+
+    response = {'status': -1}
+
+    response['status'] = 0
+    response['full_headers'] = full_headers
+    response['headers'] = headers
+    response['rows'] = escape_rows(rows, nulls_only=True)
+
+    return response
+
+  def get_sample_data_sql(self, database, table, column=None):
+
     if column is None:
       column = ', '.join([col['name'] for col in self.get_columns(database, table)])
 
@@ -218,4 +240,4 @@ class Assist():
     else:
       response['message'] = _('Failed to get sample data.')
 
-    return response
+    return response