2017-07-21 14 views
1

私はFirebaseデータベースに接続しているアプリケーションで作業していますが、My Appは基本的にマーケットプレイスアプリケーションです。私はすべてのユーザ検索履歴をFirebase Analyticsにカスタムイベントとして記録しています。これは、分析や提案の目的でBigクエリにリンクしています。このデータはapp_eventsテーブルにBIQクエリに保存されますFirebase Big query - カスタムイベントテーブルからデータを取得する方法

FirebaseAnalytics mFireBaseAnalytics = FirebaseAnalytics.getInstance(context); 
     Bundle params = new Bundle(); 
     params.putString("SEARCHER_ID", "xxxx_id"); 
     params.putString("KEYWORDS", "Shoe"); 
     params.putDouble("LATITUDE", 12.00); 
     params.putDouble("LONGITUDE", 80.00); 
     mFireBaseAnalytics.logEvent("SEARCH_PRODUCT", params); 

は、ここに私のカスタムイベントログ例です。

このイベントから、私のローカルエリアで人気のあるキーワード検索のリストをたとえば5 Kmの範囲内で照会する必要があります。私は本当にこれを達成する方法がわかりません。実際にBigクエリにどのように格納されているかを下の結果から確認してください。この形式の

"event_dim": [  
     { 
     "date": "20170718", 
     "name": "SEARCH_PRODUCT", 
     "params": [ 

      { 
      "key": "LATITUDE", 
      "value": { 
       "string_value": null, 
       "int_value": null, 
       "float_value": null, 
       "double_value": "12.9632747" 
      } 
      }, 

      { 
      "key": "SEARCHER_ID", 
      "value": { 
       "string_value": "-KkphUBI3_v3kdGq6qTa", 
       "int_value": null, 
       "float_value": null, 
       "double_value": null 
      } 
      }, 

      { 
      "key": "LONGITUDE", 
      "value": { 
       "string_value": null, 
       "int_value": null, 
       "float_value": null, 
       "double_value": "80.2131347" 
      } 
      }, 

      { 
      "key": "KEYWORDS", 
      "value": { 
       "string_value": "shoe cloths", 
       "int_value": null, 
       "float_value": null, 
       "double_value": null 
      } 
      }, 

     ], 

     } 
     ] 

私はこのクエリでは、標準のSQLクエリ

SELECT 
SQRT(POW(69.1*((
    SELECT 
    params.value.double_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'LATITUDE') - 12.00), 2) + POW(69.1*(80.098 - (SELECT 
    params.value.double_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'LONGITUDE')) * COS((SELECT 
    params.value.double_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'LATITUDE')/57.3), 2)) AS distance, 
    (
    SELECT 
    params.value.string_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'KEYWORDS') AS KEYWORDS, 
    (
    SELECT 
    params.value.string_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'SEARCHER_ID') AS SEARCHER_ID, 
    (
    SELECT 
    params.value.string_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'LATITUDE') AS LATITUDE, 
    (
    SELECT 
    params.value.string_value 
    FROM 
    x.params 
    WHERE 
    params.key = 'LONGITUDE') AS LONGITUDE 
FROM 
    `app_name.app_events_*`, 
    UNNEST(event_dim) AS x 
WHERE 
    x.name = 'SEARCH_PRODUCT' 
    AND (
    SELECT 
    params.value.string_value As SearchId 
    FROM 
    x.params 
    WHERE 
    params.key = 'SEARCHER_ID') = '-xx_id' 

を使用して以下のようなクエリを作った私は2つの問題に直面しています。 1)距離がNULLになっています 2)5 KMの範囲内でこの結果をフィルタリングする方法がわかりません。

この問題を解決するお手伝いをしてください。

PS: 私は直接それにデータを挿入していますし、それから、私は、クエリの下に使用して期待される結果を得ることができます別の一時Biqqueryテーブルを持っています。

SELECT 
    SQRT(POW(111.1*(MAX(Lattitude) - 12.9632691), 2) + POW(111.1*(80.2140816 - MAX(Longitude)) * COS(MAX(Lattitude)/57.3), 2)) AS distance, 
    MAX(SearchKeyword) as SearchKeyword, 
    SearcherArea, 
    COUNT(SearcherArea) AS totalCount 
FROM 
    `HNF.LastFewHoursData` 
WHERE 
SearchKeyword LIKE '%%' 
GROUP BY 
    SearcherArea 

HAVING 
    distance <= 5 
ORDER BY 
    totalCount DESC 
LIMIT 
    5 

Firebase Analyticsによって更新されるイベントテーブルのクエリには問題があります。

答えて

2

このクエリは、あなたのために働くかもしれない:

私がやったこと
CREATE TEMP FUNCTION distance(lat1 FLOAT64, lat2 FLOAT64, lon1 FLOAT64, lon2 FLOAT64) AS((
WITH data AS(
SELECT POW(SIN((ACOS(-1)/180 * (lat1 -lat2))/2), 2) + COS(ACOS(-1)/180 * (lat1)) * COS(ACOS(-1)/180 * (lat2)) * POW(SIN((ACOS(-1)/180 * (lon1 -lon2))/2), 2) a 
) 
SELECT 6371 * 2 * ATAN2(SQRT((SELECT a FROM data)), SQRT(1 - (SELECT a FROM data))) 
)); 

WITH data AS(
    SELECT ARRAY<STRUCT<name STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING, int_value INT64, float_value FLOAT64, double_value FLOAT64> > > > > [STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.9632747 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 80.2131347 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-KkphUBI3_v3kdGq6qTa" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params), 
                                              STRUCT("SEARCH_RANDOM" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.9632747 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 80.2131347 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-KkphUBI3_v3kdGq6qTa" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params), 
                                              STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.9632747 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 80.2131347 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-xx_id" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params)] event_dim UNION ALL 

    SELECT ARRAY<STRUCT<name STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING, int_value INT64, float_value FLOAT64, double_value FLOAT64> > > > > [STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.9632747 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 80.2131347 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-KkphUBI3_v3kdGq6qTa" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params), 
                                              STRUCT("SEARCH_RANDOM" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.9632747 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 80.2131347 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-KkphUBI3_v3kdGq6qTa" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params), 
                                              STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.9632747 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 80.2131347 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-xxx_id" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params)] event_dim UNION ALL 

    SELECT ARRAY<STRUCT<name STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING, int_value INT64, float_value FLOAT64, double_value FLOAT64> > > > > [STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.01 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.01 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-xx_id" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("pants" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params), 
                                              STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.01 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.01 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-xx_id" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params), 
                                              STRUCT("SEARCH_PRODUCT" AS name, [STRUCT("LATITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.01 AS double_value) AS value), STRUCT("LONGITUDE" AS key, STRUCT("" as string_value, 0 AS int_value, 0.0 AS float_value, 12.01 AS double_value) AS value), STRUCT("SEARCHER_ID" AS key, STRUCT("-xx_id" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value), STRUCT("KEYWORDS" AS key, STRUCT("shoes cloths" as string_value, 0 AS int_value, 0.0 AS float_value, 0.0 AS double_value) AS value)] AS params)] event_dim 



) 

SELECT 
    keyword, 
    count(1) freq 
FROM(
    SELECT 
    ARRAY(SELECT AS STRUCT (SELECT value.string_value FROM UNNEST(params) WHERE key = 'KEYWORDS' AND EXISTS(SELECT 1 FROM UNNEST(params) WHERE key = 'SEARCHER_ID' and value.string_value = '-xx_id')) AS keyword, (SELECT value.double_value FROM UNNEST(params) WHERE key = 'LATITUDE' AND EXISTS(SELECT 1 FROM UNNEST(params) WHERE key = 'SEARCHER_ID' and value.string_value = '-xx_id')) latitude, (SELECT value.double_value FROM UNNEST(params) WHERE key = 'LONGITUDE' AND EXISTS(SELECT 1 FROM UNNEST(params) WHERE key = 'SEARCHER_ID' and value.string_value = '-xx_id')) longitude FROM UNNEST(event_dim) WHERE name = 'SEARCH_PRODUCT') data 
    FROM data 
    WHERE EXISTS(select 1 FROM UNNEST(event_dim) WHERE (SELECT COUNTIF(value.string_value = '-xx_id') FROM UNNEST(params) WHERE key = 'SEARCHER_ID') > 0) 
), 
UNNEST(data) 
WHERE keyword IS NOT NULL AND latitude IS NOT NULL AND longitude IS NOT NULL 
AND distance(latitude, X, longitude, Y) < 5 -- change X and Y to your desired location 
GROUP BY keyword 
ORDER BY freq DESC 

まず、我々は度で与え2つの空間点間キロで距離を(計算する関数distanceの定義を持っています、例えば、(12°Lat、13°Long))。

dataは、Firebaseベースにある同じデータのシミュレーションです。クエリを実行するときは、dataを実際のtableと置き換えてください。

最後に、クエリ自体は、最初名前キーワード、その緯度と経度を持つ配列を構築しながらSEARCH_PRODUCTない(また、フィルタがあります-xx_idに等しくないSEARCHER_ID Sを除去するため)イベントをフィルタ。

その後、緯度と経度があなたの照会先の希望よりも少ないキーワードのみを選択するためのselectとWHERE節があります(これをクエリのXとYと呼びます。あなたの現在の位置、例えば12.09°、39.01°など)。

これがうまくいくかどうか教えてください。

+0

残念なことに、このクエリはFirebase上で実行されません。エラーXが未定義ですが、修正しようとしていますが、私が編集するのが難しいクエリを理解できないため、クエリも非常に大きく見えます。あなたの入力は、簡単で簡単なソリューションを与えることができれば素晴らしいだろう。 だから、基本的には私の主な問題は、私キーワード Kewords直接ではなく、AS AS ( x.params params.key = 'KEYWORDS' から params.value.string_value を選択)に割り当てられている値であり、どこの条件で使用されているか、条件を持っているか、その理由を知っていますか? – Lakshmanan

+0

'X'と' Y'の値を分析したい緯度と経度に変更する必要があります。たとえば緯度10度と経度20度に位置する場合は、X = 10とY = 20を置き換え、クエリはX、Y(10,20)から半径5kmのキーワード頻度を返します。 –