欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Superset實現(xiàn)動態(tài)SQL查詢功能

 更新時間:2021年08月09日 11:50:19   作者:數(shù)據(jù)行者  
這篇文章給大家介紹使用自定義參數(shù)方式實現(xiàn) superset 實現(xiàn)SQL動態(tài)查詢功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

使用自定義參數(shù)方式實現(xiàn) superset 實現(xiàn)SQL動態(tài)查詢

1、啟用參數(shù):config.py 設(shè)置"ENABLE_TEMPLATE_PROCESSING": True

2、當(dāng)前superset v1.2版本支持的參數(shù)包括:

{{ current_username() }}     當(dāng)前登錄用戶名
{{ current_username(add_to_cache_keys=False) }}   不從緩存中獲取登錄用戶名,默認(rèn)從緩存獲取
{{ current_user_id()}}    當(dāng)前登錄用戶ID
{{ current_user_id(add_to_cache_keys=False) }}  不從緩存中獲取登錄用戶ID,默認(rèn)從緩存獲取
{{ url_param('custom_variable') }} url 參數(shù),比如127.0.0.1:8001\dashboard?abc=123,參數(shù)就是{{ url_param('abc') }}  結(jié)果就是123
{{ cache_key_wrapper() }}   還沒有弄明白啥用
{{ filter_values("字段名") }}  獲取dashboard filter_box組件對某個字段的篩選結(jié)果
{{ from_dttm }}  獲取dashboard filter_box組件日期篩選的開始時間
{{ to_dttm }}   獲取dashboard filter_box組件日期篩選的結(jié)束時間
{{ get_filters() }}  暫時沒有弄明白

除此之外,還可以自定義參數(shù),自定義參數(shù)方法:

①修改superset/jinja_context.py文件,修改三個地方:

regex = re.compile(
        r"\{\{.*("
        r"current_user_id\(.*\)|"
        r"current_username\(.*\)|"
        r"current_userroles\(.*\)|"
        r"isadmin\(.*\)|"
        r"cache_key_wrapper\(.*\)|"
        r"url_param\(.*\)"
        r").*\}\}"
    )

↑↑↑↑注意此處的 current_userroles 和 isadmin 是我自定義的,源文件沒有

def current_user_id(self, add_to_cache_keys: bool = True) -> Optional[int]:
        """
        Return the user ID of the user who is currently logged in.

        :param add_to_cache_keys: Whether the value should be included in the cache key
        :returns: The user ID
        """

        if hasattr(g, "user") and g.user:
            if add_to_cache_keys:
                self.cache_key_wrapper(g.user.get_id())
            return g.user.get_id()
        return None

    def current_username(self, add_to_cache_keys: bool = True) -> Optional[str]:
        """
        Return the username of the user who is currently logged in.

        :param add_to_cache_keys: Whether the value should be included in the cache key
        :returns: The username
        """

        if g.user and hasattr(g.user, "username"):
            if add_to_cache_keys:
                self.cache_key_wrapper(g.user.username)
            return g.user.username
        return None
    def current_userroles(self, add_to_cache_keys: bool = True) -> Optional[str]:
        """
        Return the roles of the user who is currently logged in.

        :param add_to_cache_keys: Whether the value should be included in the cache key
        :returns: The userroles
        """

        if g.user and hasattr(g.user, "roles"):
            if add_to_cache_keys:
                user_roles = "/".join([role.name.lower() for role in list(g.user.roles)])
                self.cache_key_wrapper(user_roles)
                print(user_roles)
                return user_roles
                """admin in user_roles"""
        return None

    def isadmin(self, add_to_cache_keys: bool = True) -> Optional[str]:
        """
        Return the roles of the user who is currently logged in.

        :param add_to_cache_keys: Whether the value should be included in the cache key
        :returns: The userroles
        """

        if g.user and hasattr(g.user, "roles"):
            if add_to_cache_keys:
                user_roles = [role.name.lower() for role in list(g.user.roles)]
                return "admin" in user_roles
        return None

↑↑↑↑仿照系統(tǒng)自帶的 current_username 編造自己的函數(shù),我寫了current_userroles 和 isadmin

class JinjaTemplateProcessor(BaseTemplateProcessor):
    def set_context(self, **kwargs: Any) -> None:
        super().set_context(**kwargs)
        extra_cache = ExtraCache(self._extra_cache_keys)
        self._context.update(
            {
                "url_param": partial(safe_proxy, extra_cache.url_param),
                "current_user_id": partial(safe_proxy, extra_cache.current_user_id),
                "current_username": partial(safe_proxy, extra_cache.current_username),
                "current_userroles": partial(safe_proxy, extra_cache.current_userroles),
                "isadmin": partial(safe_proxy, extra_cache.isadmin),
                "cache_key_wrapper": partial(safe_proxy, extra_cache.cache_key_wrapper),
                "filter_values": partial(safe_proxy, filter_values),
            }
        )

↑↑↑↑仿照系統(tǒng)自帶的 current_username 編造自己的函數(shù),我寫了current_userroles 和 isadmin

就是這3個地方,但是注意,自己在第二步早的函數(shù),返回值必須是:

ALLOWED_TYPES = (
    NONE_TYPE,
    "bool",
    "str",
    "unicode",
    "int",
    "long",
    "float",
    "list",
    "dict",
    "tuple",
    "set",
)

否則會提示錯誤,或者自己修改這個types,我是轉(zhuǎn)換,比如上面那個g.user.roles 返回的結(jié)果就不是上面類型,導(dǎo)致我一直不成功,最后修改了下,才可以

3、判斷是否自定義成功:

在superset sql lab中執(zhí)行如下代碼,如果能被解析,就說明成功

4、應(yīng)用案例:

 在dataset里面,動態(tài)訪問數(shù)據(jù)源,數(shù)據(jù)源添加where語句:select * from sales where salesname =' {{current_username()}}'

dashboard里面,通過獲取篩選器的結(jié)果,然后獲取其他表應(yīng)當(dāng)顯示的數(shù)據(jù)范圍:

select  DATE,risktype,sum(num) as num from
(SELECT date , customerid,product,risktype ,count(*) as num
from v_superset_forecast_risk group by date , customerid,product,risktype ) a
join
(select distinct customer_code,product from v_superset_access
where name='{{ current_username() }}' )access
on a.customerid=access.customer_code
and a.product=access.product
and DATE_FORMAT(date,'%Y-%m')> DATE_FORMAT(date_sub(STR_TO_DATE(concat( {{ "'" + "', '".join(filter_values('yearmonthend')) + "'" }},'-01'), '%Y-%m-%d'), interval 12 month),'%Y-%m')
and DATE_FORMAT(date,'%Y-%m')<={{ "'" + "', '".join(filter_values('yearmonthend')) + "'" }}
group by DATE,risktype

因為sql里面可以使用jinja 表達(dá)式,比如判斷篩選當(dāng)前沒有篩選的時候,獲取什么數(shù)據(jù)

 注意{%   %} 內(nèi)部使用參數(shù)的時候,不需要加{{}},否則報錯

通過篩選器實現(xiàn)模糊查詢

 

 5、官方參考文檔:

https://superset.apache.org/docs/installation/sql-templating

官方?jīng)]有那么詳細(xì),但是里面有一些我這里可能也沒有消化吸收掉,可以參考看下

總之,通過上面的自定義參數(shù)方法,和jinja表達(dá)式在sql中的應(yīng)用,可以實現(xiàn)動態(tài)查詢,解決一些無法通過頁面直接交互查詢結(jié)果顯示的內(nèi)容

另外如果你有其他應(yīng)用或者自定義上的思考,歡迎留言,相互學(xué)習(xí)

到此這篇關(guān)于Superset實現(xiàn)動態(tài)SQL查詢的文章就介紹到這了,更多相關(guān)Superset動態(tài)SQL查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SQL Server無日志恢復(fù)數(shù)據(jù)庫(2種方法)

    SQL Server無日志恢復(fù)數(shù)據(jù)庫(2種方法)

    SQL Server數(shù)據(jù)庫中的日志文件可能會由于一些突發(fā)事件或者失誤造成丟失的嚴(yán)重后果,大家都知道,SQL Server數(shù)據(jù)庫中日志文件是很重要的,所以要及時的將丟失的日志文件給找回來。下文就為大家介紹一種恢復(fù)數(shù)據(jù)庫日志文件的方法。
    2015-08-08
  • SQL Server2008數(shù)據(jù)庫導(dǎo)入導(dǎo)出兼容性處理方案

    SQL Server2008數(shù)據(jù)庫導(dǎo)入導(dǎo)出兼容性處理方案

    SQL Server 的高版本數(shù)據(jù)庫恢復(fù)到低版本則可能會有兼容性問題,下面為大家介紹的是如何解決此類問題
    2014-05-05
  • SQL Server中通用數(shù)據(jù)庫角色權(quán)限的處理詳解

    SQL Server中通用數(shù)據(jù)庫角色權(quán)限的處理詳解

    這篇文章主要給大家介紹了關(guān)于SQL Server中通用數(shù)據(jù)庫角色權(quán)限處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • SQL Server 2005降級到2000的正確操作步驟分享

    SQL Server 2005降級到2000的正確操作步驟分享

    這篇文章主要和大家一起分享的是SQL Server 2005導(dǎo)入到SQL Server 2000的正確操作步驟,下面就是文章的主要內(nèi)容描述
    2014-04-04
  • CentOS 7.3上SQL Server vNext CTP 1.2安裝教程

    CentOS 7.3上SQL Server vNext CTP 1.2安裝教程

    這篇文章主要為大家詳細(xì)介紹了CentOS 7.3上SQL Server vNext CTP 1.2安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • SQL中去除重復(fù)數(shù)據(jù)的幾種方法匯總(窗口函數(shù)對數(shù)據(jù)去重)

    SQL中去除重復(fù)數(shù)據(jù)的幾種方法匯總(窗口函數(shù)對數(shù)據(jù)去重)

    以某電商公司的銷售報表為例,常見的去重方法我們用到distinct 或者group by 語句, 今天介紹一種新的方法,利用窗口函數(shù)對數(shù)據(jù)去重,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • windows?sql?server如何徹底卸載干凈

    windows?sql?server如何徹底卸載干凈

    很多人在面對SQL Server出現(xiàn)的一些無法解決的問題時,會選擇卸載重裝,但是SQL Server卸載不干凈的話,后續(xù)安裝過程會出現(xiàn)很多問題,下面這篇文章主要給大家介紹了關(guān)于windows?sql?server如何徹底卸載干凈的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • 用SQL統(tǒng)計SQLServe表存儲空間大小的代碼

    用SQL統(tǒng)計SQLServe表存儲空間大小的代碼

    當(dāng)SQLServer數(shù)據(jù)庫越來越龐大,而其中的表有非常多的時候,想要知道到底是哪些表最耗存儲空間,到底該怎樣統(tǒng)計各個表的存儲大小呢
    2012-05-05
  • MSSQL事務(wù)的存儲過程

    MSSQL事務(wù)的存儲過程

    這篇文章主要介紹了MSSQL事務(wù)的存儲過程,需要的朋友可以參考下
    2014-08-08
  • SQL Server服務(wù)啟動的實現(xiàn)步驟

    SQL Server服務(wù)啟動的實現(xiàn)步驟

    本文主要介紹了SQL Server服務(wù)啟動的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論