使用Java

时间:2016-12-28 10:29:17

标签: java extract extraction adobe-analytics data-extraction

这是我的Java代码,用于从Adobe Analytics中提取数据:(从GitHub存储库克隆)

public static AnalyticsClient SecretAuthentication(String endpoint,String username,String password){
    AnalyticsClient client = new AnalyticsClientBuilder()
    .setEndpoint(endpoint)
    .authenticateWithSecret(username, password)        
    .build();    

    return client;
}  

public static void main(String[] args) throws IOException, InterruptedException{        

    AnalyticsClient client = SecretAuthentication("api.omniture.com","username","my_secret_pass");        
    ReportDescription desc = new ReportDescription();
    String rsid="my_rs_id";
    desc.setReportSuiteID(rsid);
    desc.setDateFrom("2016-10-12"); // YYYY-MM-DD
    desc.setDateTo("2016-10-13");
    desc.setMetricIds("entries","orders","pageviews","visits","visitors");

    String[] elements = new String[2];
    elements[0]="prop3";
    elements[1]="prop33";
    desc.setElementIds(elements);
    //Pass the description to the API queue method, which will start the process of preparing the report:

    ReportMethods reportMethods = new ReportMethods(client);
    int reportId = reportMethods.queue(desc);

    System.out.println(reportId);

//The received integer is a report id, which can be used to receive the actual report using the get() method. 
//Preparing report takes some time, and the get() method will throw an exception with appropriate message if the report is not ready yet. 
//Following code runs the get() method in a loop, waiting until the report is ready:

   ReportResponse response = null;
    while (response == null) {               
        try {
            response = reportMethods.get(reportId);
                            //System.out.println(response.toString());
        } catch (ApiException e) {

            System.out.println(e.toString());
                            Thread.sleep(3000);
                continue;
        }
    }

    List<ReportData> responseData = response.getReport().getData();
    System.out.println("Is there data in the report? "+responseData.size());

    for (int j = 0; j < responseData.size(); j++) 
    {
        System.out.println(responseData.get(j).getName()+ " has :");
        System.out.println(responseData.get(j).getCounts());
    }               
} 

最后一个“for”语句的示例输出是:

FR has :
[35732.0, 0.0, 115146.0, 36402.0, 32111.0]

5大小的向量包括度量值(“条目”,“订单”,“网页浏览量”,“访问次数”,“访问者”) “FR”(法国)是第一个元素(prop3)的值,它实际上是“Country”变量。 问题是我没有关于第二个元素prop33的信息(prop33是“设备类型”)。

String[] elements = new String[2];
elements[0]="prop3";
elements[1]="prop33";

最重要的是,Adobe似乎忽略了第二个元素(prop33),并且只考虑第一个元素(prop3)进行搜索。我可以通过改变elements数组中两个元素的顺序来证明这一点。

String[] elements = new String[2];
elements[0]="prop33";
elements[1]="prop3";

如果我首先放置prop33,输出线是不同的,Adobe响应就好像prop33(设备类型)是唯一的标准。例如:

iPhone has :
[47636.0, 6.0, 107440.0, 47729.0, 42330.0]

那么,我如何发送两个或多个元素作为匹配标准?

2 个答案:

答案 0 :(得分:2)

我明白了。 “问题”与参数格式无关!! Adobe响应也遵循json格式。为了查看所有响应数据,您需要调用“getBreakdown()”方法以发现json响应树的“较低”层!在我的附加代码中,“for”语句仅打印prop3 json元素的数据,因为这是Adobe响应的第一层。如果有人想看prop33元素应该执行以下操作:

   for (int j = 0; j < responseData.size(); j++) 
    {
        System.out.println(responseData.get(j).getName()+ " has :");
        System.out.println(responseData.get(j).getCounts());
        List<ReportData>reportData;
        reportData = responseData.get(j).getBreakdown();//<---Here's what is needed!!
        for (int i = 0; i < reportData.size(); i++) 
        {
            System.out.println("  "+reportData.get(i).getName());
            System.out.println("  "+reportData.get(i).getCounts());                           
        } 
        System.out.println("===============================================");
    }

一般来说,你需要众多方便的json reader java库中的一个来遍历json树!!

答案 1 :(得分:0)

这不是对您上一次评论的回答,而是对评论过长的答案,这些评论应该有助于您找出问题所在。再次声明我不是一个真正的java编码器,所以把它当作它的价值。但是..

首先,为了清楚起见,你做了试试这个,对吧?

desc.setElementIds("prop3", "prop33");

而且你说这不起作用?因为看setElementIds我看到了

public void setElementIds(String... elementIds) { .. }

我对java {5}的理解String...基本上是String[](数组)的语法糖,但它接受字符串为多个参数传递,不是单个字符串数组,所以我认为传递多个args确实是要走的路。

但总的来说,您应该检查请求中实际发送给Adobe的内容。我希望soap / xml版本的要求类似,但我不知道真的知道soap / xml版本,所以这里是JSON版本。根据您发布的内容(Report.Queue),JSON对象有效负载应如下所示:

{
    "reportDescription":{
        "reportSuiteID":"my_rs_id",
        "dateFrom":"2016-10-12",
        "dateTo":"2016-10-13",
        "metrics":[
            {
                "id":"entries"
            },
            {
                "id":"orders"
            },
            {
                "id":"pageviews"
            },
            {
                "id":"visits"
            },
            {
                "id":"visitors"
            }
        ],
        "elements":[
            {
                "id":"prop3"
            },
            {
                "id":"prop33"
            }
        ]
    }
}

因此请检查http(s)请求以确保它看起来像(或soap / xml equiv)。

您的(JSON)响应(Report.Get)应如下所示:

{
    "report":{
        "type":"ranked",
        "elements":[
            {
                "id":"prop3",
                "name":"prop3 name here"
            },
            {
                "id":"prop33",
                "name":"prop33 name here"
            }
        ],
        "reportSuite":{
            "id":"my_rs_id",
            "name":"rsid name here"
        },
        "period":"Wed. 12 Oct. 2016 - Thu. 13 Oct. 2016",
        "metrics":[
            {
                "id":"entries",
                "name":"Entries",
                "type":"number",
                "decimals":0,
                "latency":4599,
                "current":false
            },
            {
                "id":"orders",
                "name":"Orders",
                "type":"number",
                "decimals":0,
                "latency":4599,
                "current":false
            },
            {
                "id":"pageviews",
                "name":"Page Views",
                "type":"number",
                "decimals":0,
                "latency":4599,
                "current":false
            },
            {
                "id":"visits",
                "name":"Visits",
                "type":"number",
                "decimals":0,
                "latency":4599,
                "current":false
            },
            {
                "id":"visitors",
                "name":"Visitors",
                "type":"number",
                "decimals":0,
                "latency":4599,
                "current":false
            }
        ],
        "data":[
            {
                "name":"<first prop3 value>",
                "url":"",
                "counts":[
                    "246944",
                    "0",
                    "494509",
                    "251168",
                    "200670"
                ],
                "breakdown":[
                    {
                        "name":"<first breakdown prop33 value>",
                        "url":"",
                        "counts":[
                            "226556",
                            "0",
                            "460021",
                            "231637",
                            "184294"
                        ]
                    },
                    {
                        "name":"<second breakdown prop33 value>",
                        "url":"",
                        "counts":[
                            "17058",
                            "0",
                            "23930",
                            "17628",
                            "15085"
                        ]
                    } //, etc...
                ]
            },
            {
                "name":"<second prop3 value>",
                "url":"",
                "counts":[
                    "246944",
                    "0",
                    "494509",
                    "251168",
                    "200670"
                ],
                "breakdown":[
                    {
                        "name":"<first breakdown prop33 value>",
                        "url":"",
                        "counts":[
                            "226556",
                            "0",
                            "460021",
                            "231637",
                            "184294"
                        ]
                    },
                    {
                        "name":"<second breakdown prop33 value>",
                        "url":"",
                        "counts":[
                            "17058",
                            "0",
                            "23930",
                            "17628",
                            "15085"
                        ]
                    } //, etc...
                ]
            } //,etc.. 
        ],
        "totals":[
            "253490",
            "0",
            "503495",
            "253490",
            "201190"
        ],
        "version":"1.4.16.10"
    },
    "waitSeconds":0,
    "runSeconds":0
}