Skip to content

Commit

Permalink
Merge pull request #69 from zapr-oss/develop
Browse files Browse the repository at this point in the history
Support for distinctCount extension
  • Loading branch information
abhi-zapr authored Dec 3, 2018
2 parents 22cd402 + 6319df9 commit 246292f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Aggregation Queries
* LongFirst
* LongLast
* ThetaSketch
* DistinctCount

#### Filters

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>in.zapr.druid</groupId>
<artifactId>druidry</artifactId>
<version>2.9</version>
<version>2.10</version>

<name>Druidry - Druid Java Client</name>
<description>Druidry is an open-source Java based utility library which supports creating
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package in.zapr.druid.druidry.extensions.distinctcount.aggregator;

import in.zapr.druid.druidry.aggregator.DruidAggregator;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

@Getter
@EqualsAndHashCode(callSuper = true)
public class DistinctCountAggregator extends DruidAggregator {

private static final String DISTINCT_COUNT_AGGREGATOR_TYPE = "distinctCount";
private String fieldName;

public DistinctCountAggregator(@NonNull String name, @NonNull String fieldName) {
this.type = DISTINCT_COUNT_AGGREGATOR_TYPE;
this.name = name;
this.fieldName = fieldName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2017-present, Red Brick Lane Marketing Solutions Pvt. Ltd.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package in.zapr.druid.druidry.extensions.distinctcount.aggregator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class DistinctCountAggregatorTest {

private static ObjectMapper objectMapper;

@BeforeClass
public void init() {
objectMapper = new ObjectMapper();
}

@Test
public void testAllFields() throws JsonProcessingException, JSONException {

DistinctCountAggregator distinctCountAggregator =
new DistinctCountAggregator("uniqueStars", "allStars");

JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "distinctCount");
jsonObject.put("name", "uniqueStars");
jsonObject.put("fieldName", "allStars");

String actualJSON = objectMapper.writeValueAsString(distinctCountAggregator);
String expectedJSON = jsonObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test(expectedExceptions = NullPointerException.class)
public void testNullFields() {

DistinctCountAggregator distinctCountAggregator =
new DistinctCountAggregator(null, null);
}

@Test
public void testEqualsPositive() {

DistinctCountAggregator aggregator1 =
new DistinctCountAggregator("uniqueStars", "allStars");
DistinctCountAggregator aggregator2 =
new DistinctCountAggregator("uniqueStars", "allStars");

Assert.assertEquals(aggregator1, aggregator2);
}

@Test
public void testEqualsNegative() {

DistinctCountAggregator aggregator1 =
new DistinctCountAggregator("uniqueStars1", "allStars1");
DistinctCountAggregator aggregator2 =
new DistinctCountAggregator("uniqueStars2", "allStars2");

Assert.assertNotEquals(aggregator1, aggregator2);
}

}

0 comments on commit 246292f

Please sign in to comment.