2016-02-29 68 views
1

我在查詢中定義並註冊了UDF函數,但它看起來像大查詢不喜歡函數定義字符串中的轉義報價\",有誰知道如何在bigquery中使用轉義引號?如何在BigQuery查詢文本中使用轉義引號?

這是我的例子:

SELECT 
    Social_Connection, 
    Device_Type 
FROM 
    js(
    -- input table 
    (
    SELECT 
     user_attribute.Name, 
     user_attribute.Value 
    FROM 
     UDF_TESTING.testing_src), 
    -- input vars 
    user_attribute.Name, 
    user_attribute.Value, 
    -- output schema 
    "[{name: 'Social_Connection', type: 'string'}, 
    {name: 'Device_Type', type: 'string'}]", 
    -- the function 
    "function(row, emit) { 
    var social_connection_index = 0; 
    var device_type_index = 0; 
    for (var i = 0; i < row.user_attribute.length; i++) { 
    if (row.user_attribute[i].Name == \"Social_Connection\") { // <------big query complains about the escape quote 
     social_connection_index = i; 
    } 
    if (row.user_attribute[i].Name == \"Device_Type\") { // <----- same as here 
     device_type_index = i; 
    } 
    } 
    emit({Social_Connection: row.user_attribute[social_connection_index].Value, 
     Device_Type: row.user_attribute[device_type_index].Value}) 
}") 
+0

可你只需要使用單引號? row.user_attribute [i] .Name =='Social_Connection' –

+0

在這個例子中,我可以,但我有一些嵌套的字符串同時使用單引號和雙引號,我必須使用轉義字符。 –

+0

在您的示例中引號不是因此您可以安全地爲它們使用單​​引號。 \「和\'應該用於當你在字符串中使用它們時,如果不是這種情況,將會很高興看到示例,我只是做了快速的虛擬測試,並且它完美地工作 –

回答

2

下面是僞例子,只是爲了展示如何在這兩個BQ SELECT本身以及內JS功能使用轉義。
希望這會幫助你!

SELECT Name, HasSingleQuote, HasDoubleQuote 
FROM (JS(
    -- input table 
    (SELECT 
     Name 
    FROM 
     (SELECT 'abc' AS Name), 
     (SELECT 'a\'bc' AS Name), 
     (SELECT 'a\"bc' AS Name), 
     (SELECT 'a\"b\'c' AS Name) 
    ), 
    -- input vars 
    Name, 
    -- output schema 
    "[{name: 'Name', type: 'STRING'}, 
    {name: 'HasSingleQuote', type: 'BOOLEAN'}, 
    {name: 'HasDoubleQuote', type: 'BOOLEAN'}]", 
    -- the function 
    "function(row, emit) { 
     var hasSingleQuote = false; 
     var hasDoubleQuote = false; 
     if (row.Name.indexOf('\'') > -1) hasSingleQuote = true; 
     if (row.Name.indexOf('\"') > -1) hasDoubleQuote = true; 
     emit({Name: row.Name, 
     HasSingleQuote: hasSingleQuote, 
     HasDoubleQuote: hasDoubleQuote 
     }) 
}")) 

輸出是:

Name HasSingleQuote HasDoubleQuote 
abc  false   false  
a'bc true   false  
a"bc false   true 
a"b'c true   true 
+0

感謝您的示例! –

相關問題