├── .github ├── ISSUE_TEMPLATE │ └── bug_report.md └── workflows │ └── build.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── codestyle └── forbidden-apis.txt ├── conf └── tsdb.conf ├── pom.xml └── src ├── main └── java │ └── com │ └── aliyun │ └── hitsdb │ └── client │ ├── AbstractConfig.java │ ├── BalHiTSDBClient.java │ ├── BalTSDBClient.java │ ├── Config.java │ ├── HAPolicy.java │ ├── HiTSDB.java │ ├── HiTSDBClient.java │ ├── HiTSDBClientFactory.java │ ├── HiTSDBConfig.java │ ├── IotClient.java │ ├── IotClientFactory.java │ ├── LindormTSDBClient.java │ ├── LindormTSDBClientFactory.java │ ├── TSDB.java │ ├── TSDBClient.java │ ├── TSDBClientFactory.java │ ├── TSDBConfig.java │ ├── callback │ ├── AbstractBatchPutCallback.java │ ├── AbstractCallback.java │ ├── AbstractMultiFieldBatchPutCallback.java │ ├── BatchPutCallback.java │ ├── BatchPutDetailsCallback.java │ ├── BatchPutIgnoreErrorsCallback.java │ ├── BatchPutSummaryCallback.java │ ├── Callback.java │ ├── MultiFieldBatchPutCallback.java │ ├── MultiFieldBatchPutDetailsCallback.java │ ├── MultiFieldBatchPutIgnoreErrorsCallback.java │ ├── MultiFieldBatchPutSummaryCallback.java │ ├── QueryCallback.java │ └── http │ │ ├── AbstractPutHttpResponseCallback.java │ │ ├── BaseHttpFutureCallback.java │ │ ├── BatchPutHttpResponseCallback.java │ │ ├── HttpResponseCallbackFactory.java │ │ ├── MultiFieldBatchPutHttpResponseCallback.java │ │ └── QueryHttpResponseCallback.java │ ├── consumer │ ├── AbstractBatchPutRunnable.java │ ├── BatchPutRunnable.java │ ├── BatchPutThreadFactory.java │ ├── Consumer.java │ ├── ConsumerFactory.java │ ├── DefaultBatchPutConsumer.java │ ├── MultiFieldBatchPutRunnable.java │ └── PointsCollectionPutRunnable.java │ ├── exception │ ├── BufferQueueFullException.java │ ├── NotImplementedException.java │ └── http │ │ ├── HttpClientConnectionRefusedException.java │ │ ├── HttpClientException.java │ │ ├── HttpClientInitException.java │ │ ├── HttpClientSocketTimeoutException.java │ │ ├── HttpServerErrorException.java │ │ ├── HttpServerNotSupportException.java │ │ ├── HttpServerUnauthorizedException.java │ │ └── HttpUnknowStatusException.java │ ├── http │ ├── Host.java │ ├── HttpAPI.java │ ├── HttpAddressManager.java │ ├── HttpClient.java │ ├── HttpClientFactory.java │ ├── TSDBHttpAsyncCallbackExecutor.java │ ├── request │ │ ├── HttpDeleteWithEntity.java │ │ └── HttpGetWithEntity.java │ ├── response │ │ ├── HttpStatus.java │ │ └── ResultResponse.java │ └── semaphore │ │ └── SemaphoreManager.java │ ├── queue │ ├── DataPointQueue.java │ ├── DataQueue.java │ └── DataQueueFactory.java │ ├── util │ ├── FileWatcher.java │ ├── HealthManager.java │ ├── HealthWatcher.java │ ├── HttpUtil.java │ ├── LinkedHashMapUtils.java │ ├── Objects.java │ ├── Pair.java │ ├── PropKit.java │ ├── WKTParser.java │ ├── WatchManager.java │ └── guava │ │ ├── RateLimiter.java │ │ └── base │ │ ├── Preconditions.java │ │ └── Ticker.java │ └── value │ ├── JSONValue.java │ ├── Result.java │ ├── request │ ├── AbstractPoint.java │ ├── ByteArrayValue.java │ ├── ComplexValue.java │ ├── DeleteMetaRequest.java │ ├── DeltaOptions.java │ ├── DumpMetaValue.java │ ├── FieldsSerializer.java │ ├── Filter.java │ ├── GeoPointValue.java │ ├── LastLimit.java │ ├── LastPointQuery.java │ ├── LastPointSubQuery.java │ ├── LookupRequest.java │ ├── LookupTagFilter.java │ ├── MetricTimeRange.java │ ├── MultiFieldPoint.java │ ├── MultiFieldQuery.java │ ├── MultiFieldSubQuery.java │ ├── MultiFieldSubQueryDetails.java │ ├── MultiValuedPoint.java │ ├── MultiValuedQuery.java │ ├── MultiValuedQueryLastRequest.java │ ├── MultiValuedQueryMetricDetails.java │ ├── MultiValuedSubQuery.java │ ├── Point.java │ ├── PointType.java │ ├── PointsCollection.java │ ├── Query.java │ ├── RateOptions.java │ ├── SQLValue.java │ ├── SubQuery.java │ ├── SuggestValue.java │ ├── TTLValue.java │ ├── TagsAddInfo.java │ ├── TagsRemoveInfo.java │ ├── TagsShowInfo.java │ ├── Timeline.java │ ├── UniqueUtil.java │ └── ValueSerializer.java │ ├── response │ ├── KeyValue.java │ ├── LastDataValue.java │ ├── LookupDetailedResult.java │ ├── LookupResult.java │ ├── MultiFieldQueryLastResult.java │ ├── MultiFieldQueryResult.java │ ├── MultiFieldQueryValuesSerializer.java │ ├── MultiValuedQueryLastResult.java │ ├── MultiValuedQueryResult.java │ ├── MultiValuedTupleComparator.java │ ├── QueryResult.java │ ├── QueryResultDpsSerializer.java │ ├── SQLResult.java │ ├── TTLResult.java │ ├── TagResult.java │ ├── TagsAddResult.java │ ├── TagsShowResult.java │ ├── UserResult.java │ └── batch │ │ ├── DetailsResult.java │ │ ├── ErrorPoint.java │ │ ├── IgnoreErrorsResult.java │ │ ├── MultiFieldDetailsResult.java │ │ ├── MultiFieldErrorPoint.java │ │ ├── MultiFieldIgnoreErrorsResult.java │ │ └── SummaryResult.java │ └── type │ ├── Aggregator.java │ ├── DownsampleDataSource.java │ ├── FilterType.java │ ├── Granularity.java │ ├── HttpResponseLevel.java │ ├── QueryType.java │ ├── Suggest.java │ └── UserPrivilege.java └── test ├── java └── com │ └── aliyun │ └── hitsdb │ └── client │ ├── MultiFieldPointAsyncPutSamples.java │ ├── MultiFieldPointAsyncPutTest.java │ ├── TestClientHA.java │ ├── TestClientQueryHA.java │ ├── TestHiTSDBClientBatchPut.java │ ├── TestHiTSDBClientBatchPutNoLogic.java │ ├── TestHiTSDBClientBatchPutStringValue.java │ ├── TestHiTSDBClientDeleteData.java │ ├── TestHiTSDBClientDeleteMeta.java │ ├── TestHiTSDBClientDumpMeta.java │ ├── TestHiTSDBClientLast.java │ ├── TestHiTSDBClientLastDataPointQuery.java │ ├── TestHiTSDBClientLookup.java │ ├── TestHiTSDBClientMultiFieldFeatures.java │ ├── TestHiTSDBClientPutOnErrorURL.java │ ├── TestHiTSDBClientQuery.java │ ├── TestHiTSDBClientQueryLast.java │ ├── TestHiTSDBClientQueryLimitAndDpValue.java │ ├── TestHiTSDBClientQueryRate.java │ ├── TestHiTSDBClientQueryReverse.java │ ├── TestHiTSDBClientQueryStringValue.java │ ├── TestHiTSDBClientSingleTimePointQuery.java │ ├── TestHiTSDBClientSuggest.java │ ├── TestHiTSDBClientSyncPut.java │ ├── TestHiTSDBClientTTL.java │ ├── TestHiTSDBClientTableOp.java │ ├── TestHiTSDBClientVersion.java │ ├── TestIotClient.java │ ├── TestLastResultTimestampReverse.java │ ├── TestLindormTSDBClient.java │ ├── TestLindormTSDBClientFactory.java │ ├── TestLinkedHashMapUtils.java │ ├── TestTSDBSQL.java │ ├── balance │ ├── TestBalClient.java │ ├── TestFileWatchManager.java │ ├── TestHealthManager.java │ └── TestPoint.java │ ├── callback │ ├── TestBatchPutSummaryCallback.java │ ├── TestBatchPutSummaryCallbackWithFlush.java │ ├── TestNoLogicCallback.java │ └── TestPutWithTemperaryCallback.java │ ├── compress │ ├── TestHiTSDBClientPut.java │ └── TestHiTSDBClientQuery.java │ ├── configuration │ └── TestConfiguration.java │ ├── connection │ ├── TestConnectionLiveTime.java │ └── TestKeepaliveConnection.java │ ├── error │ └── TestHiTSDBClientBatchPutExceptionCallback.java │ ├── example │ ├── ExampleOfMultiField.java │ ├── TestRead.java │ └── TestWrite.java │ ├── http │ ├── TestHiTSDBRequestTime.java │ └── TestHttpAsyncClient.java │ ├── keepalive │ └── TestKeepalive.java │ ├── log │ └── TestLog.java │ ├── performance │ ├── TestBlockingQueue.java │ ├── TestFastJSON.java │ ├── TestQueryPerformance.java │ └── TestWritePerformance.java │ ├── retry │ └── TestHiTSDBClientBatchPutRetryCallback.java │ ├── util │ ├── DateUtils.java │ └── UI.java │ └── value │ ├── FlushTest.java │ ├── TSDBClientFactoryTest.java │ ├── TSDBDataTypeTest.java │ ├── TestConfig.java │ ├── TestLastPointQuery.java │ ├── TestMetricPoint.java │ ├── TestMetricPointStringValue.java │ ├── TestQuery.java │ ├── TestTagResult.java │ ├── request │ ├── DeleteMetaRequestTest.java │ ├── FilterTest.java │ ├── LastPointQueryTest.java │ ├── LastPointSubQueryTest.java │ ├── MultiFieldQueryRequestTest.java │ ├── MultiFieldSubQueryRequestTest.java │ ├── MultiPointTest.java │ ├── PointTest.java │ ├── PointsCollectionTest.java │ ├── QueryTest.java │ ├── SubQueryTest.java │ ├── TagsTest.java │ └── UniqueUtilTest.java │ └── response │ ├── TestDetailsResult.java │ ├── TestMultiFieldQueryResult.java │ ├── TestPutMPutResult.java │ ├── TestQueryResult.java │ └── TestSummaryResult.java └── resources └── log4j.properties /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Version information** 11 | * SDK version 12 | The version of the aliyun-tsdb-java-sdk 13 | 14 | * TSDB engine version 15 | The version of the version of Aliyun TSDB in use 16 | which can be retrieved by the following command 17 | 18 | ````sh 19 | curl http://{TSDB host}:{TSDB port}/api/version 20 | ```` 21 | 22 | **Bug description** 23 | A clear and concise description of what the bug is. 24 | 25 | **Reproduce case** 26 | Codes to reproduce the behavior: 27 | 28 | ````java 29 | ```` 30 | 31 | **Expected behavior** 32 | A clear and concise description of what you expected to happen. 33 | 34 | **Exception stacktrace (Optional)** 35 | If applicable, add the error stack help describe the details. 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: 9 | - master 10 | - 'release/*.*.*' 11 | pull_request: 12 | branches: 13 | - master 14 | - 'release/*.*.*' 15 | 16 | jobs: 17 | build: 18 | 19 | runs-on: ubuntu-latest 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Set up JDK 8 24 | uses: actions/setup-java@v2 25 | with: 26 | java-version: '8' 27 | distribution: 'adopt' 28 | - name: Build with Maven 29 | run: mvn clean package -Dmaven.test.skip=true 30 | - name: Unit Test 31 | run: mvn cobertura:cobertura -Dmaven.test.skip=false -Dtest=**/value/**/**.java 32 | - name: Upload 33 | run: bash <(curl -s https://codecov.io/bash) 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | hitsdb-client.iml 2 | /.idea/ 3 | /target/ 4 | /.settings/ 5 | /.classpath 6 | /.project 7 | .idea/ 8 | .DS_Store 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | dist: trusty 3 | jdk: 4 | - oraclejdk8 5 | 6 | stages: 7 | - compile 8 | - test 9 | 10 | branches: 11 | only: 12 | - master 13 | 14 | jobs: 15 | include: 16 | - stage: compile 17 | script: mvn clean package -Dmaven.test.skip=true 18 | - stage: test 19 | script: mvn cobertura:cobertura -Dmaven.test.skip=false -Dtest=**/value/**/**.java 20 | 21 | after_success: 22 | - bash <(curl -s https://codecov.io/bash) 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Aliyun TSDB SDK for Java 3 | 4 | [![Master Build](https://github.com/aliyun/aliyun-tsdb-java-sdk/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/aliyun/aliyun-tsdb-java-sdk/actions) 5 | [![codecov](https://codecov.io/gh/aliyun/aliyun-tsdb-java-sdk/branch/master/graph/badge.svg?token=EOGQWWFDDT)](https://codecov.io/gh/aliyun/aliyun-tsdb-java-sdk) 6 | [![Maven Central](https://img.shields.io/maven-central/v/com.aliyun/hitsdb-client.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.aliyun%22%20AND%20a:%22hitsdb-client%22) 7 | 8 | The Aliyun TSDB SDK for Java enables Java developers to easily work with Aliyun TSDB, compatible OpenTSDB protocol. You can get started in minutes using ***Maven***. 9 | 10 | - [Developer Guide](https://help.aliyun.com/document_detail/61634.html) 11 | - [Issues](https://github.com/aliyun/aliyun-tsdb-java-sdk/issues) 12 | - [Release](https://github.com/aliyun/aliyun-tsdb-java-sdk/releases) 13 | 14 | ## Requirements 15 | 16 | - Java 1.6 or later 17 | - Maven 18 | 19 | ## Install 20 | 21 | The recommended way to use the Aliyun TSDB SDK for Java in your project is to consume it from Maven. Import as follows: 22 | 23 | ``` 24 | 25 | com.aliyun 26 | hitsdb-client 27 | 0.3.8 28 | 29 | ``` 30 | 31 | See the Setup section of [the SDK Reference](https://help.aliyun.com/document_detail/61634.html) for more information about installing the SDK through other means. 32 | 33 | 34 | ## Build 35 | 36 | Once you check out the code from GitHub, you can build it using Maven. Use the following command to build: 37 | 38 | ``` 39 | mvn clean install -DskipTests 40 | ``` 41 | 42 | -------------------------------------------------------------------------------- /conf/tsdb.conf: -------------------------------------------------------------------------------- 1 | 2 | # 3 | #host = 127.0.0.1 4 | # 5 | #port = 8242 6 | 7 | # 8 | address = 127.0.0.1:8242 9 | 10 | # 11 | batchPutSize = 500 12 | 13 | # 14 | batchPutTimeLimit = 300 15 | # 16 | batchPutBufferSize = 3000 17 | # 18 | batchPutRetryCount = 1 19 | # 20 | httpConnectionPool = 64 21 | # 22 | httpConnectTimeout = 90 23 | # 24 | putRequestLimitSwitch = true 25 | # 26 | putRequestLimit = -1 27 | # 28 | batchPutConsumerThreadCount = 1 29 | # 30 | httpCompress = true 31 | # 32 | ioThreadCount = 1 33 | # 34 | backpressure = true 35 | # 36 | httpConnectionLiveTime = 0 37 | # 38 | httpKeepaliveTime = -1 39 | # 40 | maxTPS = -1 41 | # 42 | asyncPut = true -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/BalHiTSDBClient.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.callback.AbstractBatchPutCallback; 4 | import com.aliyun.hitsdb.client.callback.QueryCallback; 5 | import com.aliyun.hitsdb.client.exception.http.HttpUnknowStatusException; 6 | import com.aliyun.hitsdb.client.http.Host; 7 | import com.aliyun.hitsdb.client.util.*; 8 | import com.aliyun.hitsdb.client.value.Result; 9 | import com.aliyun.hitsdb.client.value.request.*; 10 | import com.aliyun.hitsdb.client.value.response.*; 11 | import com.aliyun.hitsdb.client.value.type.Suggest; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | 15 | import java.io.File; 16 | import java.io.FileReader; 17 | import java.io.IOException; 18 | import java.util.*; 19 | import java.util.concurrent.ConcurrentHashMap; 20 | import java.util.concurrent.TimeUnit; 21 | import java.util.concurrent.atomic.AtomicBoolean; 22 | 23 | 24 | /** 25 | * @since 0.2.1 26 | * @deprecated use {@link BalTSDBClient} instead. 27 | */ 28 | @Deprecated 29 | public class BalHiTSDBClient extends BalTSDBClient implements HiTSDB { 30 | 31 | public BalHiTSDBClient(File configFile) throws IOException { 32 | super(configFile); 33 | } 34 | 35 | public BalHiTSDBClient(String configFilePath) throws IOException { 36 | super(configFilePath); 37 | } 38 | 39 | public BalHiTSDBClient(File configFile, AbstractBatchPutCallback callback) throws IOException { 40 | super(configFile, callback); 41 | } 42 | 43 | public BalHiTSDBClient(HiTSDBConfig config) { 44 | super(config); 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/Config.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.callback.AbstractBatchPutCallback; 4 | import com.aliyun.hitsdb.client.callback.AbstractMultiFieldBatchPutCallback; 5 | import com.aliyun.hitsdb.client.http.Host; 6 | 7 | import java.util.List; 8 | 9 | public interface Config { 10 | String BASICTYPE = "Basic"; // it should be "Basic" according to RFC 2617 11 | String ALITYPE = "alibaba-signature"; 12 | 13 | boolean isSslEnable(); 14 | 15 | String getAuthType(); 16 | 17 | String getInstanceId(); 18 | 19 | String getTsdbUser(); 20 | 21 | String getBasicPwd(); 22 | 23 | byte[] getCertContent(); 24 | 25 | int getPutRequestLimit(); 26 | 27 | int getBatchPutBufferSize(); 28 | 29 | int getMultiFieldBatchPutBufferSize(); 30 | 31 | AbstractBatchPutCallback getBatchPutCallback(); 32 | 33 | void setBatchPutCallback(AbstractBatchPutCallback callback); 34 | 35 | AbstractMultiFieldBatchPutCallback getMultiFieldBatchPutCallback(); 36 | 37 | void setMultiFieldBatchPutCallback(AbstractMultiFieldBatchPutCallback callback); 38 | 39 | boolean isAsyncPut(); 40 | 41 | int getBatchPutConsumerThreadCount(); 42 | 43 | int getMultiFieldBatchPutConsumerThreadCount(); 44 | 45 | int getBatchPutRetryCount(); 46 | 47 | int getBatchPutSize(); 48 | 49 | int getBatchPutTimeLimit(); 50 | 51 | String getHost(); 52 | 53 | int getPort(); 54 | 55 | List getAddresses(); 56 | 57 | int getHttpConnectionPool(); 58 | 59 | int getHttpConnectTimeout(); 60 | 61 | int getHttpSocketTimeout(); 62 | 63 | int getHttpConnectionRequestTimeout(); 64 | 65 | int getIoThreadCount(); 66 | 67 | boolean isPutRequestLimitSwitch(); 68 | 69 | boolean isHttpCompress(); 70 | 71 | boolean isBackpressure(); 72 | 73 | int getHttpConnectionLiveTime(); 74 | 75 | int getHttpKeepaliveTime(); 76 | 77 | int getMaxTPS(); 78 | 79 | Config copy(String host, int port); 80 | 81 | HAPolicy getHAPolicy(); 82 | 83 | boolean isDeduplicationEnable(); 84 | 85 | boolean isLastResultReverseEnable(); 86 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/HiTSDB.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | /** 4 | * @since 0.2.1 5 | * @deprecated use {@link TSDB} instead. 6 | */ 7 | @Deprecated 8 | public interface HiTSDB extends TSDB { 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/HiTSDBClient.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.NotImplementedException; 4 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 5 | import com.aliyun.hitsdb.client.exception.http.HttpUnknowStatusException; 6 | import com.aliyun.hitsdb.client.value.response.SQLResult; 7 | 8 | /** 9 | * @since 0.2.1 10 | * @deprecated use {@link TSDBClient} instead. 11 | */ 12 | @Deprecated 13 | public class HiTSDBClient extends TSDBClient implements HiTSDB { 14 | 15 | public HiTSDBClient(Config config) throws HttpClientInitException { 16 | super(config); 17 | } 18 | 19 | @Override 20 | public SQLResult queryBySQL(String sql) throws HttpUnknowStatusException { 21 | throw new NotImplementedException(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/HiTSDBClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | 5 | /** 6 | * @since 0.2.1 7 | * @deprecated use {@link TSDBClientFactory} instead. 8 | */ 9 | @Deprecated 10 | public class HiTSDBClientFactory { 11 | public static HiTSDB connect(String host, int port) throws HttpClientInitException { 12 | HiTSDBConfig config = HiTSDBConfig.address(host, port).config(); 13 | HiTSDB client = new HiTSDBClient(config); 14 | return client; 15 | } 16 | 17 | public static HiTSDB connect(HiTSDBConfig config) throws HttpClientInitException { 18 | HiTSDB client = new HiTSDBClient(config); 19 | return client; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/IotClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | 5 | /** 6 | * @author johnnyzou 7 | * @description create client for IoT(Super-tag mode) user 8 | */ 9 | public class IotClientFactory { 10 | public static TSDB connect(String host, int port) throws HttpClientInitException { 11 | TSDBConfig config = TSDBConfig.address(host, port).config(); 12 | TSDB client = new IotClient(config); 13 | return client; 14 | } 15 | 16 | public static TSDB connect(Config config) throws HttpClientInitException { 17 | TSDB client = new IotClient(config); 18 | return client; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/LindormTSDBClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | 5 | public class LindormTSDBClientFactory { 6 | 7 | public static LindormTSDBClient connect(String host, int port) throws HttpClientInitException { 8 | TSDBConfig config = TSDBConfig.address(host, port).config(); 9 | LindormTSDBClient client = new LindormTSDBClient(config); 10 | return client; 11 | } 12 | 13 | public static LindormTSDBClient connect(Config config) throws HttpClientInitException { 14 | LindormTSDBClient client = new LindormTSDBClient(config); 15 | return client; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/TSDBClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | 5 | public class TSDBClientFactory { 6 | 7 | public static TSDB connect(String host, int port) throws HttpClientInitException { 8 | TSDBConfig config = TSDBConfig.address(host, port).config(); 9 | TSDB client = new TSDBClient(config); 10 | return client; 11 | } 12 | 13 | public static TSDB connect(Config config) throws HttpClientInitException { 14 | TSDB client = new TSDBClient(config); 15 | return client; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/AbstractBatchPutCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.aliyun.hitsdb.client.value.Result; 8 | import com.aliyun.hitsdb.client.value.request.Point; 9 | 10 | public abstract class AbstractBatchPutCallback extends AbstractCallback,R > { 11 | 12 | public static Map getPutQueryParamMap(AbstractBatchPutCallback batchPutCallback) { 13 | Map paramsMap = new HashMap(); 14 | if (batchPutCallback != null) { 15 | if (batchPutCallback instanceof BatchPutCallback) { 16 | } else if (batchPutCallback instanceof BatchPutSummaryCallback) { 17 | paramsMap.put("summary", "true"); 18 | } else if (batchPutCallback instanceof BatchPutDetailsCallback) { 19 | paramsMap.put("details", "true"); 20 | } else if (batchPutCallback instanceof BatchPutIgnoreErrorsCallback) { 21 | paramsMap.put("ignoreErrors", "true"); 22 | } 23 | } 24 | return paramsMap; 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/AbstractCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | public abstract class AbstractCallback implements Callback { 4 | 5 | @Override 6 | public void failed(String address, REQ request, Exception ex) { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/AbstractMultiFieldBatchPutCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 5 | 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | public abstract class AbstractMultiFieldBatchPutCallback extends AbstractCallback,R > { 11 | 12 | public static Map getMultiFieldPutQueryParamMap(AbstractMultiFieldBatchPutCallback multiFieldBatchPutCallback) { 13 | Map paramsMap = new HashMap(); 14 | if (multiFieldBatchPutCallback != null) { 15 | if (multiFieldBatchPutCallback instanceof MultiFieldBatchPutCallback) { 16 | } else if (multiFieldBatchPutCallback instanceof MultiFieldBatchPutSummaryCallback) { 17 | paramsMap.put("summary", "true"); 18 | } else if (multiFieldBatchPutCallback instanceof MultiFieldBatchPutDetailsCallback) { 19 | paramsMap.put("details", "true"); 20 | } else if (multiFieldBatchPutCallback instanceof MultiFieldBatchPutIgnoreErrorsCallback) { 21 | paramsMap.put("ignoreErrors", "true"); 22 | } 23 | } 24 | return paramsMap; 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/BatchPutCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.util.List; 4 | 5 | import com.aliyun.hitsdb.client.value.Result; 6 | import com.aliyun.hitsdb.client.value.request.Point; 7 | 8 | public abstract class BatchPutCallback extends AbstractBatchPutCallback{ 9 | 10 | @Override 11 | public abstract void response(String address, List points, Result result); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/BatchPutDetailsCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.util.List; 4 | 5 | import com.aliyun.hitsdb.client.value.request.Point; 6 | import com.aliyun.hitsdb.client.value.response.batch.DetailsResult; 7 | 8 | public abstract class BatchPutDetailsCallback extends AbstractBatchPutCallback { 9 | 10 | @Override 11 | public abstract void response(String address, List points, DetailsResult result); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/BatchPutIgnoreErrorsCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import com.aliyun.hitsdb.client.value.request.Point; 4 | import com.aliyun.hitsdb.client.value.response.batch.IgnoreErrorsResult; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Copyright @ 2020 alibaba.com 10 | * All right reserved. 11 | * Function:Batch Put IgnoreErrors Callback 12 | * 13 | * @author Benedict Jin 14 | * @since 2020/09/21 15 | */ 16 | public abstract class BatchPutIgnoreErrorsCallback extends AbstractBatchPutCallback { 17 | 18 | @Override 19 | public abstract void response(String address, List points, IgnoreErrorsResult result); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/BatchPutSummaryCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.util.List; 4 | 5 | import com.aliyun.hitsdb.client.value.request.Point; 6 | import com.aliyun.hitsdb.client.value.response.batch.SummaryResult; 7 | 8 | public abstract class BatchPutSummaryCallback extends AbstractBatchPutCallback { 9 | 10 | @Override 11 | public abstract void response(String address, List points, SummaryResult result); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/Callback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | public interface Callback { 4 | void response(String address, REQ input, RES output); 5 | 6 | void failed(String address, REQ input, Exception ex); 7 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/MultiFieldBatchPutCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 5 | 6 | import java.util.List; 7 | 8 | public abstract class MultiFieldBatchPutCallback extends AbstractMultiFieldBatchPutCallback{ 9 | 10 | @Override 11 | public abstract void response(String address, List points, Result result); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/MultiFieldBatchPutDetailsCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 4 | import com.aliyun.hitsdb.client.value.response.batch.MultiFieldDetailsResult; 5 | 6 | import java.util.List; 7 | 8 | public abstract class MultiFieldBatchPutDetailsCallback extends AbstractMultiFieldBatchPutCallback { 9 | 10 | @Override 11 | public abstract void response(String address, List points, MultiFieldDetailsResult result); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/MultiFieldBatchPutIgnoreErrorsCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 4 | import com.aliyun.hitsdb.client.value.response.batch.MultiFieldIgnoreErrorsResult; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Copyright @ 2020 alibaba.com 10 | * All right reserved. 11 | * Function:MultiField Batch Put IgnoreErrors Callback 12 | * 13 | * @author Benedict Jin 14 | * @since 2020/09/21 15 | */ 16 | public abstract class MultiFieldBatchPutIgnoreErrorsCallback extends AbstractMultiFieldBatchPutCallback { 17 | 18 | @Override 19 | public abstract void response(String address, List points, MultiFieldIgnoreErrorsResult result); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/MultiFieldBatchPutSummaryCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 4 | import com.aliyun.hitsdb.client.value.response.batch.SummaryResult; 5 | 6 | import java.util.List; 7 | 8 | public abstract class MultiFieldBatchPutSummaryCallback extends AbstractMultiFieldBatchPutCallback { 9 | 10 | @Override 11 | public abstract void response(String address, List points, SummaryResult result); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/QueryCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.util.List; 4 | 5 | import com.aliyun.hitsdb.client.value.request.Query; 6 | import com.aliyun.hitsdb.client.value.response.QueryResult; 7 | 8 | public abstract class QueryCallback extends AbstractCallback> { 9 | 10 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/http/BaseHttpFutureCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback.http; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | import org.apache.http.HttpResponse; 6 | import org.apache.http.concurrent.FutureCallback; 7 | 8 | public class BaseHttpFutureCallback implements FutureCallback { 9 | 10 | private final AtomicInteger unCompletedTaskNum; 11 | private final FutureCallback futureCallback; 12 | 13 | public BaseHttpFutureCallback(AtomicInteger unCompletedTaskNum, FutureCallback futureCallback) { 14 | super(); 15 | this.unCompletedTaskNum = unCompletedTaskNum; 16 | this.futureCallback = futureCallback; 17 | } 18 | 19 | @Override 20 | public void completed(HttpResponse result) { 21 | futureCallback.completed(result); 22 | // 任务处理完毕,再减数 23 | unCompletedTaskNum.decrementAndGet(); 24 | } 25 | 26 | @Override 27 | public void failed(Exception ex) { 28 | futureCallback.failed(ex); 29 | // 任务处理完毕,再减数 30 | unCompletedTaskNum.decrementAndGet(); 31 | } 32 | 33 | @Override 34 | public void cancelled() { 35 | futureCallback.cancelled(); 36 | // 任务处理完毕,再减数 37 | unCompletedTaskNum.decrementAndGet(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/callback/http/QueryHttpResponseCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback.http; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.http.HttpResponse; 6 | import org.apache.http.concurrent.FutureCallback; 7 | 8 | import com.alibaba.fastjson.JSON; 9 | import com.aliyun.hitsdb.client.callback.QueryCallback; 10 | import com.aliyun.hitsdb.client.exception.http.HttpServerErrorException; 11 | import com.aliyun.hitsdb.client.exception.http.HttpServerNotSupportException; 12 | import com.aliyun.hitsdb.client.exception.http.HttpUnknowStatusException; 13 | import com.aliyun.hitsdb.client.http.response.HttpStatus; 14 | import com.aliyun.hitsdb.client.http.response.ResultResponse; 15 | import com.aliyun.hitsdb.client.value.request.Query; 16 | import com.aliyun.hitsdb.client.value.response.QueryResult; 17 | 18 | import static com.aliyun.hitsdb.client.TSDBClient.setTypeIfNeeded; 19 | 20 | public class QueryHttpResponseCallback implements FutureCallback { 21 | 22 | private final String address; 23 | private final Query query; 24 | private final QueryCallback callback; 25 | private final boolean compress; 26 | 27 | public QueryHttpResponseCallback(final String address, final Query query, QueryCallback callback,boolean compress) { 28 | super(); 29 | this.address = address; 30 | this.query = query; 31 | this.callback = callback; 32 | this.compress = compress; 33 | } 34 | 35 | @Override 36 | public void completed(HttpResponse httpResponse) { 37 | ResultResponse resultResponse = ResultResponse.simplify(httpResponse,this.compress); 38 | HttpStatus httpStatus = resultResponse.getHttpStatus(); 39 | switch (httpStatus) { 40 | case ServerSuccessNoContent: 41 | callback.response(this.address, query, null); 42 | return; 43 | case ServerSuccess: 44 | String content = resultResponse.getContent(); 45 | List queryResultList = JSON.parseArray(content, QueryResult.class); 46 | setTypeIfNeeded(query, queryResultList); 47 | callback.response(this.address, query, queryResultList); 48 | return; 49 | case ServerNotSupport: 50 | callback.failed(this.address, query, new HttpServerNotSupportException(resultResponse)); 51 | case ServerError: 52 | callback.failed(this.address, query, new HttpServerErrorException(resultResponse)); 53 | default: 54 | callback.failed(this.address, query, new HttpUnknowStatusException(resultResponse)); 55 | } 56 | } 57 | 58 | @Override 59 | public void failed(Exception ex) { 60 | callback.failed(this.address, query, ex); 61 | } 62 | 63 | @Override 64 | public void cancelled() { 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/consumer/AbstractBatchPutRunnable.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.consumer; 2 | 3 | import com.aliyun.hitsdb.client.Config; 4 | import com.aliyun.hitsdb.client.callback.http.HttpResponseCallbackFactory; 5 | import com.aliyun.hitsdb.client.http.HttpAddressManager; 6 | import com.aliyun.hitsdb.client.http.HttpClient; 7 | import com.aliyun.hitsdb.client.http.semaphore.SemaphoreManager; 8 | import com.aliyun.hitsdb.client.queue.DataQueue; 9 | import com.aliyun.hitsdb.client.util.guava.RateLimiter; 10 | 11 | import java.util.concurrent.CountDownLatch; 12 | 13 | public abstract class AbstractBatchPutRunnable { 14 | /** 15 | * 缓冲队列 16 | */ 17 | protected final DataQueue dataQueue; 18 | /** 19 | * Http客户端 20 | */ 21 | protected final HttpClient tsdbHttpClient; 22 | protected final HttpClient secondaryClient; 23 | /** 24 | * 消费者队列控制器。 25 | * 在优雅关闭中,若消费者队列尚未结束,则CountDownLatch用于阻塞close()方法。 26 | */ 27 | protected final CountDownLatch countDownLatch; 28 | protected final Config config; 29 | protected final SemaphoreManager semaphoreManager; 30 | protected final HttpAddressManager httpAddressManager; 31 | /** 32 | * 回调包装与构造工厂 33 | */ 34 | protected final HttpResponseCallbackFactory httpResponseCallbackFactory; 35 | /** 36 | * 每批次数据点个数 37 | */ 38 | protected int batchSize; 39 | /** 40 | * 批次提交间隔,单位:毫秒 41 | */ 42 | protected int batchPutTimeLimit; 43 | protected final RateLimiter rateLimiter; 44 | 45 | public AbstractBatchPutRunnable(DataQueue dataQueue, HttpClient httpclient, HttpClient secondaryClient, CountDownLatch countDownLatch, Config config, RateLimiter rateLimiter) { 46 | this.dataQueue = dataQueue; 47 | this.tsdbHttpClient = httpclient; 48 | this.secondaryClient = secondaryClient; 49 | this.countDownLatch = countDownLatch; 50 | this.batchSize = config.getBatchPutSize(); 51 | this.batchPutTimeLimit = config.getBatchPutTimeLimit(); 52 | this.config = config; 53 | this.semaphoreManager = tsdbHttpClient.getSemaphoreManager(); 54 | this.httpAddressManager = tsdbHttpClient.getHttpAddressManager(); 55 | this.rateLimiter = rateLimiter; 56 | this.httpResponseCallbackFactory = tsdbHttpClient.getHttpResponseCallbackFactory(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/consumer/BatchPutThreadFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.consumer; 2 | 3 | import java.util.concurrent.ThreadFactory; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | class BatchPutThreadFactory implements ThreadFactory { 7 | private static final AtomicInteger poolNumber = new AtomicInteger(1); 8 | private ThreadGroup group; 9 | private String namePrefix; 10 | private AtomicInteger threadNumber; 11 | 12 | public BatchPutThreadFactory(String nameSuffix) { 13 | threadNumber = new AtomicInteger(1); 14 | SecurityManager s = System.getSecurityManager(); 15 | group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); 16 | namePrefix = "pool-" + poolNumber.getAndIncrement() + "-" + nameSuffix + "-"; 17 | } 18 | 19 | @Override 20 | public Thread newThread(Runnable r) { 21 | Thread thread = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); 22 | if (thread.isDaemon()) { 23 | // 作为守护线程存在 24 | thread.setDaemon(true); 25 | } 26 | 27 | if (thread.getPriority() != Thread.NORM_PRIORITY) { 28 | thread.setPriority(Thread.NORM_PRIORITY); 29 | } 30 | 31 | return thread; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/consumer/Consumer.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.consumer; 2 | 3 | public interface Consumer { 4 | void start(); 5 | void stop(); 6 | void stop(boolean force); 7 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/consumer/ConsumerFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.consumer; 2 | 3 | import com.aliyun.hitsdb.client.Config; 4 | import com.aliyun.hitsdb.client.http.HttpClient; 5 | import com.aliyun.hitsdb.client.queue.DataQueue; 6 | import com.aliyun.hitsdb.client.util.guava.RateLimiter; 7 | 8 | public class ConsumerFactory { 9 | 10 | public static Consumer createConsumer(DataQueue buffer, HttpClient httpclient, HttpClient secondaryClient, RateLimiter rateLimiter, Config config) { 11 | DefaultBatchPutConsumer consumer = new DefaultBatchPutConsumer(buffer, httpclient, secondaryClient, rateLimiter, config); 12 | return consumer; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/BufferQueueFullException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception; 2 | 3 | public class BufferQueueFullException extends RuntimeException { 4 | private static final long serialVersionUID = -6089496689881016447L; 5 | 6 | public BufferQueueFullException(String message, Throwable cause) { 7 | super(message, cause); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/NotImplementedException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception; 2 | 3 | public class NotImplementedException extends RuntimeException { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpClientConnectionRefusedException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | /** 4 | * @author shijiaqi 5 | */ 6 | // 下一个大版本,移动到com.alibaba.hitsdb.client.exception.http.client 7 | public class HttpClientConnectionRefusedException extends HttpClientException { 8 | 9 | private static final long serialVersionUID = -4155566481009479303L; 10 | 11 | public HttpClientConnectionRefusedException(String address,Exception e) { 12 | super(e); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpClientException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | //下一个大版本,移动到com.alibaba.hitsdb.client.exception.http.client 4 | public class HttpClientException extends RuntimeException { 5 | 6 | public HttpClientException() { 7 | } 8 | 9 | public HttpClientException(Exception e) { 10 | super(e); 11 | } 12 | 13 | public HttpClientException(String messsage) { 14 | super(messsage); 15 | } 16 | 17 | public HttpClientException(String messsage,Exception e) { 18 | super(messsage,e); 19 | } 20 | 21 | private static final long serialVersionUID = -2345036415069363458L; 22 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpClientInitException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | //下一个大版本,移动到com.alibaba.hitsdb.client.exception.http.client 4 | public class HttpClientInitException extends HttpClientException { 5 | 6 | private static final long serialVersionUID = 7092596721590181806L; 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpClientSocketTimeoutException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | public class HttpClientSocketTimeoutException extends HttpClientException { 4 | private static final long serialVersionUID = 5407907664312068303L; 5 | 6 | public HttpClientSocketTimeoutException(Exception e) { 7 | super(e); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpServerErrorException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | import com.aliyun.hitsdb.client.http.response.ResultResponse; 4 | 5 | public class HttpServerErrorException extends HttpUnknowStatusException { 6 | 7 | public HttpServerErrorException(int status, String message) { 8 | super(status, message); 9 | } 10 | 11 | public HttpServerErrorException(ResultResponse result) { 12 | super(result); 13 | } 14 | 15 | private static final long serialVersionUID = -7013317353179949073L; 16 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpServerNotSupportException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | import com.aliyun.hitsdb.client.http.response.ResultResponse; 4 | 5 | public class HttpServerNotSupportException extends HttpUnknowStatusException { 6 | 7 | public HttpServerNotSupportException(int status, String message) { 8 | super(status, message); 9 | } 10 | 11 | public HttpServerNotSupportException(ResultResponse result) { 12 | super(result); 13 | } 14 | 15 | private static final long serialVersionUID = 3183070191347274019L; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpServerUnauthorizedException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | import com.aliyun.hitsdb.client.http.response.ResultResponse; 4 | 5 | public class HttpServerUnauthorizedException extends HttpUnknowStatusException { 6 | private static final long serialVersionUID = -8842332621020945130L; 7 | 8 | public HttpServerUnauthorizedException(int status, String message) { super(status, message); } 9 | 10 | public HttpServerUnauthorizedException(ResultResponse result) { super(result); } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/exception/http/HttpUnknowStatusException.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.exception.http; 2 | 3 | import com.aliyun.hitsdb.client.http.response.ResultResponse; 4 | 5 | public class HttpUnknowStatusException extends RuntimeException { 6 | 7 | private static final long serialVersionUID = 3183070191347274019L; 8 | 9 | public HttpUnknowStatusException(int status, String message) { 10 | super(); 11 | this.status = status; 12 | this.message = message; 13 | } 14 | 15 | public HttpUnknowStatusException(ResultResponse result) { 16 | super(); 17 | this.status = result.getStatusCode(); 18 | this.message = result.getContent(); 19 | } 20 | 21 | private int status; 22 | 23 | private String message; 24 | 25 | public int getStatus() { 26 | return status; 27 | } 28 | 29 | public String getMessage() { 30 | return message; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/Host.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http; 2 | 3 | public class Host { 4 | 5 | private String ip; 6 | 7 | private int port; 8 | 9 | public Host(String ip, int port) { 10 | this.ip = ip; 11 | this.port = port; 12 | } 13 | 14 | public Host() { 15 | } 16 | 17 | public String getIp() { 18 | return ip; 19 | } 20 | 21 | public void setIp(String ip) { 22 | this.ip = ip; 23 | } 24 | 25 | public int getPort() { 26 | return port; 27 | } 28 | 29 | public void setPort(int port) { 30 | this.port = port; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/HttpAPI.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http; 2 | 3 | public interface HttpAPI { 4 | String PUT = "/api/put"; 5 | String QUERY = "/api/query"; 6 | String QUERY_LAST = "/api/query/last"; 7 | String TTL = "/api/ttl"; 8 | String DELETE_DATA = "/api/delete_data"; 9 | String DELETE_META = "/api/delete_meta"; 10 | String SUGGEST = "/api/suggest"; 11 | String DUMP_META = "/api/dump_meta"; 12 | String LOOKUP = "/api/search/lookup"; 13 | String VERSION = "/api/version"; 14 | String MAIN_CLUSTER_TIME = "/api/set_cluster?mainClusterTime"; 15 | 16 | String UPDATE_LAST = "/api/updatelast"; 17 | 18 | String TRUNCATE = "/api/truncate"; 19 | 20 | String DELETE_ALL_TABLE = "/api/delete_all_table"; 21 | 22 | String HEALTH = "/api/health"; 23 | 24 | String VIP_HEALTH = "/api/vip_health"; 25 | 26 | /** 27 | * Multi-field data model APIs 28 | */ 29 | String MPUT = "/api/mput"; 30 | String MQUERY = "/api/mquery"; 31 | String QUERY_MLAST = "/api/query/mlast"; 32 | /** 33 | * User authentication API 34 | */ 35 | String USER_AUTH = "/api/users"; 36 | 37 | String SQL = "/api/sqlquery"; 38 | 39 | String TAGS_ADD = "/api/tags/add"; 40 | String TAGS_SHOW = "/api/tags/show"; 41 | String TAGS_REMOVE = "/api/tags/remove"; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/HttpAddressManager.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http; 2 | 3 | import com.aliyun.hitsdb.client.Config; 4 | 5 | public class HttpAddressManager { 6 | private final String host; 7 | private final int port; 8 | private final String address; 9 | 10 | private HttpAddressManager(Config config) { 11 | host = config.getHost(); 12 | port = config.getPort(); 13 | address = host + ":" + port; 14 | } 15 | 16 | public static HttpAddressManager createHttpAddressManager(Config config) { 17 | return new HttpAddressManager(config); 18 | } 19 | 20 | public String getAddress() { 21 | return this.address; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/TSDBHttpAsyncCallbackExecutor.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http; 2 | 3 | import java.io.IOException; 4 | 5 | import org.apache.http.Header; 6 | import org.apache.http.HttpException; 7 | import org.apache.http.HttpRequest; 8 | import org.apache.http.nio.NHttpClientConnection; 9 | import org.apache.http.nio.protocol.HttpAsyncRequestExecutor; 10 | import org.apache.http.protocol.HttpContext; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | public class TSDBHttpAsyncCallbackExecutor extends HttpAsyncRequestExecutor { 15 | private static final Logger LOGGER = LoggerFactory.getLogger(TSDBHttpAsyncCallbackExecutor.class); 16 | private final int liveTime; 17 | 18 | public TSDBHttpAsyncCallbackExecutor(int liveTime) { 19 | super(); 20 | this.liveTime = liveTime; 21 | } 22 | 23 | @Override 24 | public void requestReady(NHttpClientConnection conn) throws IOException, HttpException { 25 | try { 26 | super.requestReady(conn); 27 | } catch (Exception ex) { 28 | LOGGER.error("", ex); 29 | } 30 | 31 | // 需要自动关闭连接 32 | if (this.liveTime > 0) { 33 | HttpRequest httpRequest = conn.getHttpRequest(); 34 | if (httpRequest == null) { 35 | return; 36 | } 37 | 38 | HttpContext context = conn.getContext(); 39 | 40 | long currentTimeMillis = System.currentTimeMillis(); 41 | Object oldTimeMillisObj = context.getAttribute("t"); 42 | if (oldTimeMillisObj == null) { 43 | context.setAttribute("t", currentTimeMillis); 44 | } else { 45 | long oldTimeMillis = (Long) oldTimeMillisObj; 46 | long dt = currentTimeMillis - oldTimeMillis; 47 | if (dt > 1000 * liveTime) { // 超时,重连 48 | tryCloseConnection(httpRequest); 49 | context.setAttribute("t", currentTimeMillis); 50 | } 51 | } 52 | } 53 | } 54 | 55 | private void tryCloseConnection(HttpRequest request) { 56 | Header[] headers = request.getHeaders("Connection"); 57 | if (headers != null && headers.length > 0) { 58 | for (Header h : headers) { 59 | request.removeHeader(h); 60 | } 61 | } 62 | 63 | request.addHeader("Connection", "close"); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/request/HttpDeleteWithEntity.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http.request; 2 | 3 | import java.net.URI; 4 | 5 | import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; 6 | 7 | public class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase { 8 | public final static String METHOD_NAME = "DELETE"; 9 | 10 | public HttpDeleteWithEntity() { 11 | super(); 12 | } 13 | 14 | public HttpDeleteWithEntity(final URI uri) { 15 | super(); 16 | setURI(uri); 17 | } 18 | 19 | public HttpDeleteWithEntity(final String uri) { 20 | super(); 21 | setURI(URI.create(uri)); 22 | } 23 | 24 | @Override 25 | public String getMethod() { 26 | return METHOD_NAME; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/request/HttpGetWithEntity.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http.request; 2 | 3 | import java.net.URI; 4 | 5 | import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; 6 | 7 | public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase { 8 | public final static String METHOD_NAME = "GET"; 9 | 10 | public HttpGetWithEntity() { 11 | super(); 12 | } 13 | 14 | public HttpGetWithEntity(final URI uri) { 15 | super(); 16 | setURI(uri); 17 | } 18 | 19 | public HttpGetWithEntity(final String uri) { 20 | super(); 21 | setURI(URI.create(uri)); 22 | } 23 | 24 | @Override 25 | public String getMethod() { 26 | return METHOD_NAME; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/http/response/HttpStatus.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http.response; 2 | 3 | public enum HttpStatus { 4 | ServerSuccess, 5 | ServerSuccessNoContent, 6 | ServerNotSupport, 7 | ServerError, 8 | ServerUnauthorized, 9 | UnKnow 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/queue/DataQueue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.queue; 2 | 3 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 4 | import com.aliyun.hitsdb.client.value.request.Point; 5 | import com.aliyun.hitsdb.client.value.request.PointsCollection; 6 | 7 | public interface DataQueue { 8 | void send(Point point); 9 | 10 | Point receive() throws InterruptedException; 11 | 12 | Point receive(int timeout) throws InterruptedException; 13 | 14 | 15 | void sendMultiFieldPoint(MultiFieldPoint point); 16 | 17 | MultiFieldPoint receiveMultiFieldPoint() throws InterruptedException; 18 | 19 | MultiFieldPoint receiveMultiFieldPoint(int timeout) throws InterruptedException; 20 | 21 | void sendPoints(PointsCollection points); 22 | 23 | PointsCollection receivePoints() throws InterruptedException; 24 | 25 | PointsCollection receivePoints(int timeout) throws InterruptedException; 26 | 27 | void forbiddenSend(); 28 | 29 | void waitEmpty(); 30 | 31 | boolean isEmpty(); 32 | 33 | Point[] getPoints(); 34 | 35 | MultiFieldPoint[] getMultiFieldPoints(); 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/queue/DataQueueFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.queue; 2 | 3 | public class DataQueueFactory { 4 | 5 | public static DataQueue createDataPointQueue(int batchPutBufferSize, int multiFieldBatchPutBufferSize, int waitTimeLimit, boolean backpressure) { 6 | return new DataPointQueue(batchPutBufferSize, multiFieldBatchPutBufferSize, waitTimeLimit, backpressure); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/FileWatcher.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.io.File; 4 | 5 | public interface FileWatcher { 6 | 7 | 8 | void fileModified(File file); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/HealthWatcher.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | public interface HealthWatcher { 4 | 5 | 6 | void health(String host, boolean health); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/HttpUtil.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | package com.aliyun.hitsdb.client.util; 4 | 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | import java.net.HttpURLConnection; 9 | import java.net.URL; 10 | import java.util.Map; 11 | 12 | public class HttpUtil { 13 | /** 14 | * Send a get request 15 | * @param url 16 | * @return response 17 | * @throws IOException 18 | */ 19 | static public String get(String url) throws IOException { 20 | return fetch("GET", url, null, null); 21 | } 22 | 23 | /** 24 | * Send a request 25 | * @param method HTTP method, for example "GET" or "POST" 26 | * @param url Url as string 27 | * @param body Request body as string 28 | * @param headers Optional map with headers 29 | * @return response Response as string 30 | * @throws IOException 31 | */ 32 | static public String fetch(String method, String url, String body, 33 | Map headers) throws IOException { 34 | // connection 35 | URL u = new URL(url); 36 | HttpURLConnection conn = (HttpURLConnection)u.openConnection(); 37 | conn.setConnectTimeout(10000); 38 | conn.setReadTimeout(10000); 39 | 40 | // method 41 | if (method != null) { 42 | conn.setRequestMethod(method); 43 | } 44 | 45 | // headers 46 | if (headers != null) { 47 | for(String key : headers.keySet()) { 48 | conn.addRequestProperty(key, headers.get(key)); 49 | } 50 | } 51 | 52 | // body 53 | if (body != null) { 54 | conn.setDoOutput(true); 55 | OutputStream os = conn.getOutputStream(); 56 | os.write(body.getBytes()); 57 | os.flush(); 58 | os.close(); 59 | } 60 | 61 | // response 62 | InputStream is = conn.getInputStream(); 63 | String response = streamToString(is); 64 | is.close(); 65 | 66 | // handle redirects 67 | if (conn.getResponseCode() == 301) { 68 | String location = conn.getHeaderField("Location"); 69 | return fetch(method, location, body, headers); 70 | } 71 | 72 | return response; 73 | } 74 | 75 | /** 76 | * Read an input stream into a string 77 | * @param in 78 | * @return 79 | * @throws IOException 80 | */ 81 | static public String streamToString(InputStream in) throws IOException { 82 | StringBuffer out = new StringBuffer(); 83 | byte[] b = new byte[4096]; 84 | for (int n; (n = in.read(b)) != -1;) { 85 | out.append(new String(b, 0, n)); 86 | } 87 | return out.toString(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/LinkedHashMapUtils.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.lang.reflect.Field; 4 | import java.util.LinkedHashMap; 5 | import java.util.Map.Entry; 6 | 7 | import com.aliyun.hitsdb.client.exception.http.HttpClientException; 8 | 9 | public class LinkedHashMapUtils { 10 | 11 | private static final Field tailField; 12 | private static final Field beforeField; 13 | 14 | static { 15 | try { 16 | tailField = LinkedHashMap.class.getDeclaredField("tail"); 17 | tailField.setAccessible(true); 18 | } catch (NoSuchFieldException e) { 19 | throw new LinkedHashMapException(e); 20 | } catch (SecurityException e) { 21 | throw new LinkedHashMapException(e); 22 | } 23 | 24 | try { 25 | Class clazz = Class.forName("java.util.LinkedHashMap$Entry"); 26 | beforeField = clazz.getDeclaredField("before"); 27 | beforeField.setAccessible(true); 28 | } catch (ClassNotFoundException e) { 29 | throw new LinkedHashMapException(e); 30 | } catch (NoSuchFieldException e) { 31 | throw new LinkedHashMapException(e); 32 | } catch (SecurityException e) { 33 | throw new LinkedHashMapException(e); 34 | } 35 | 36 | } 37 | 38 | 39 | @SuppressWarnings("unchecked") 40 | public static Entry getTail(LinkedHashMap map) throws LinkedHashMapException { 41 | try { 42 | Entry entry = (Entry) tailField.get(map); 43 | return entry; 44 | } catch (SecurityException e) { 45 | throw new LinkedHashMapException(e); 46 | } catch (IllegalArgumentException e) { 47 | throw new LinkedHashMapException(e); 48 | } catch (IllegalAccessException e) { 49 | throw new LinkedHashMapException(e); 50 | } 51 | } 52 | 53 | @SuppressWarnings("unchecked") 54 | public static Entry getBefore(Entry entry) throws LinkedHashMapException { 55 | try { 56 | Entry beforeEntry = (Entry) beforeField.get(entry); 57 | return beforeEntry; 58 | } catch (SecurityException e) { 59 | throw new LinkedHashMapException(e); 60 | } catch (IllegalArgumentException e) { 61 | throw new LinkedHashMapException(e); 62 | } catch (IllegalAccessException e) { 63 | throw new LinkedHashMapException(e); 64 | } 65 | } 66 | 67 | } 68 | 69 | class LinkedHashMapException extends HttpClientException { 70 | private static final long serialVersionUID = 1L; 71 | 72 | LinkedHashMapException(Exception e){ 73 | super(e); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/Objects.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | public class Objects { 4 | 5 | public static T requireNonNull(T obj) { 6 | if (obj == null) 7 | throw new NullPointerException(); 8 | return obj; 9 | } 10 | 11 | public static T requireNonNull(T obj, String message) { 12 | if (obj == null) 13 | throw new NullPointerException(message); 14 | return obj; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/Pair.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Pair implements Serializable { 6 | private static final long serialVersionUID = 2289934353998056157L; 7 | 8 | private K key; 9 | 10 | public K getKey() { return key; } 11 | 12 | private V value; 13 | 14 | public V getValue() { return value; } 15 | 16 | public Pair(K key, V value) { 17 | this.key = key; 18 | this.value = value; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return key + "=" + value; 24 | } 25 | 26 | @Override 27 | public int hashCode() { 28 | // name's hashCode is multiplied by an arbitrary prime number (13) 29 | // in order to make sure there is a difference in the hashCode between 30 | return key.hashCode() * 13 + (value == null ? 0 : value.hashCode()); 31 | } 32 | 33 | @Override 34 | public boolean equals(Object o) { 35 | if (this == o) return true; 36 | if (o instanceof Pair) { 37 | Pair pair = (Pair) o; 38 | if (key != null ? !key.equals(pair.key) : pair.key != null) return false; 39 | if (value != null ? !value.equals(pair.value) : pair.value != null) return false; 40 | return true; 41 | } 42 | return false; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/PropKit.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.util.Properties; 4 | 5 | public class PropKit { 6 | 7 | private Properties properties; 8 | 9 | public PropKit(Properties properties){ 10 | this.properties = properties; 11 | } 12 | 13 | public String get(String key){ 14 | return properties.getProperty(key); 15 | } 16 | 17 | public boolean containsKey(String key){ 18 | return properties.containsKey(key); 19 | } 20 | 21 | public Integer getInt(String key){ 22 | return Integer.parseInt(properties.getProperty(key)); 23 | } 24 | 25 | public Boolean getBoolean(String key){ 26 | return Boolean.parseBoolean(properties.getProperty(key)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/WatchManager.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.io.File; 4 | import java.util.Map; 5 | import java.util.concurrent.*; 6 | 7 | public class WatchManager { 8 | 9 | private ConcurrentMap watchers = new ConcurrentHashMap(); 10 | 11 | private ScheduledExecutorService executorService; 12 | 13 | private int intervalSeconds = 2; 14 | 15 | 16 | public WatchManager() { 17 | this.executorService = Executors.newSingleThreadScheduledExecutor(); 18 | } 19 | 20 | 21 | public void start(){ 22 | if(intervalSeconds > 0){ 23 | this.executorService.scheduleWithFixedDelay(new WatchRunnable(), 24 | intervalSeconds,intervalSeconds,TimeUnit.SECONDS); 25 | } 26 | } 27 | 28 | public void stop() { 29 | this.executorService.shutdown(); 30 | } 31 | 32 | public void setIntervalSeconds(int intervalSeconds){ 33 | this.intervalSeconds = intervalSeconds; 34 | } 35 | 36 | public void watchFile(final File file,final FileWatcher watcher){ 37 | final long lastModified = file.lastModified(); 38 | watchers.put(file,new FileMonitor(lastModified,watcher)); 39 | } 40 | 41 | 42 | 43 | private final class WatchRunnable implements Runnable { 44 | 45 | @Override 46 | public void run() { 47 | for(final Map.Entry entry : watchers.entrySet()){ 48 | final File file = entry.getKey(); 49 | final FileMonitor fileMonitor = entry.getValue(); 50 | final long lastModified = file.lastModified(); 51 | if(fileModified(fileMonitor,lastModified)){ 52 | fileMonitor.lastModifiedMillis = lastModified; 53 | fileMonitor.fileWatcher.fileModified(file); 54 | } 55 | } 56 | } 57 | 58 | private boolean fileModified(final FileMonitor fileMonitor, final long lastModifiedMillis){ 59 | return lastModifiedMillis != fileMonitor.lastModifiedMillis; 60 | } 61 | } 62 | 63 | 64 | private final class FileMonitor { 65 | private final FileWatcher fileWatcher; 66 | private volatile long lastModifiedMillis; 67 | 68 | public FileMonitor(final long lastModifiedMillis,final FileWatcher fileWatcher){ 69 | this.fileWatcher = fileWatcher; 70 | this.lastModifiedMillis = lastModifiedMillis; 71 | } 72 | 73 | private void setLastModifiedMillis(final long lastModifiedMillis){ 74 | this.lastModifiedMillis = lastModifiedMillis; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "FileMonitor [fileWatcher=" + fileWatcher + ", lastModifiedMillis=" + lastModifiedMillis + "]"; 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/util/guava/base/Ticker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Guava Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.aliyun.hitsdb.client.util.guava.base; 17 | 18 | /** 19 | * The implementation of Ticker is derived from the Ticker class of Guava 16.0, 20 | * which is licensed under the Apache License 2.0 21 | */ 22 | public abstract class Ticker { 23 | /** 24 | * Constructor for use by subclasses. 25 | */ 26 | protected Ticker() {} 27 | 28 | /** 29 | * Returns the number of nanoseconds elapsed since this ticker's fixed 30 | * point of reference. 31 | */ 32 | public abstract long read(); 33 | 34 | /** 35 | * A ticker that reads the current time using {@link System#nanoTime}. 36 | * 37 | * @since 10.0 38 | */ 39 | public static Ticker systemTicker() { 40 | return SYSTEM_TICKER; 41 | } 42 | 43 | /** 44 | * The original implementation of read() is to return Platform.systemNanoTime() 45 | * However, it is equivalent with System.nanoTime() 46 | */ 47 | private static final Ticker SYSTEM_TICKER = new Ticker() { 48 | @Override 49 | public long read() { 50 | return System.nanoTime(); 51 | } 52 | }; 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/JSONValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | 6 | public class JSONValue { 7 | 8 | public static T parseObject(String json, Class clazz) { 9 | T object = JSON.parseObject(json, clazz); 10 | return object; 11 | } 12 | 13 | /* 14 | public static List parseList(String json, Class clazz) { 15 | List objectList = JSON.parseObject(json, new TypeReference>() {}); 16 | return objectList; 17 | } 18 | */ 19 | 20 | public String toJSON() { 21 | return JSON.toJSONString(this, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.SortField, SerializerFeature.SortField.MapSortField); 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return toJSON(); 27 | } 28 | 29 | public void appendJSON(final StringBuilder sb) { 30 | sb.append(toJSON()); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/Result.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value; 2 | 3 | public class Result extends JSONValue { 4 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/AbstractPoint.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.annotation.JSONType; 4 | import com.aliyun.hitsdb.client.value.JSONValue; 5 | 6 | import java.util.Map; 7 | 8 | @JSONType(ignores = {"pointType"}) 9 | public abstract class AbstractPoint extends JSONValue { 10 | 11 | protected String metric; 12 | protected Map tags; 13 | protected Long timestamp; 14 | 15 | public String getMetric() { 16 | return metric; 17 | } 18 | 19 | public Map getTags() { 20 | return tags; 21 | } 22 | 23 | public Long getTimestamp() { 24 | return timestamp; 25 | } 26 | 27 | public void setMetric(String metric) { 28 | this.metric = metric; 29 | } 30 | 31 | public void setTags(Map tags) { 32 | this.tags = tags; 33 | } 34 | 35 | public void setTimestamp(Long timestamp) { 36 | this.timestamp = timestamp; 37 | } 38 | 39 | public abstract PointType getPointType(); 40 | 41 | /** 42 | * If it is true, it is a legitimate character. 43 | * @param c char 44 | * @return 45 | */ 46 | public static boolean checkChar(char c) { 47 | return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || 48 | c == '-' || c == '_' || 49 | c == '.' || c == ' ' || c == ',' || c == '=' || c == '/' || c == ':' || 50 | c == '(' || c == ')' || c == '[' || c == ']' || c == '\'' || c == '/' || c == '#' || 51 | Character.isLetter(c); 52 | } 53 | 54 | 55 | private static final long MIN_TIME = 4294968L; 56 | 57 | private static final long MAX_TIME = 9999999999999L; 58 | 59 | public static void checkTimestamp(long timestamp) { 60 | if (timestamp < MIN_TIME || timestamp > MAX_TIME) { 61 | throw new IllegalArgumentException("The timestamp must be in range [4294968,9999999999999],but is " + timestamp); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/ByteArrayValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.Base64; 6 | 7 | /** 8 | * @author cuiyuan 9 | * @date 2020/8/4 11:01 上午 10 | */ 11 | 12 | public class ByteArrayValue extends ComplexValue { 13 | static public final String TypeValue = "bytes"; 14 | 15 | public ByteArrayValue(final byte[] content) { 16 | super(TypeValue, Base64.getEncoder().encodeToString(content)); 17 | } 18 | 19 | public byte[] decode(){ 20 | return Base64.getDecoder().decode(content); 21 | } 22 | 23 | public static boolean isJsonObjectTypeMatch(JSONObject jsonObject) { 24 | return ComplexValue.isJsonObjectTypeMatch(jsonObject) && jsonObject.get(TypeKey).equals(TypeValue); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/ComplexValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * @author cuiyuan 9 | * @date 2020/8/10 11:53 上午 10 | */ 11 | 12 | public class ComplexValue { 13 | public static final String TypeKey = "type"; 14 | public static final String ContentKey = "content"; 15 | 16 | String type; 17 | String content; 18 | 19 | public ComplexValue(String type, String content) { 20 | this.type = type; 21 | this.content = content; 22 | } 23 | 24 | public String getType() { 25 | return type; 26 | } 27 | 28 | public void setType(String type) { 29 | this.type = type; 30 | } 31 | 32 | public String getContent() { 33 | return content; 34 | } 35 | 36 | public void setContent(String content) { 37 | this.content = content; 38 | } 39 | 40 | public static boolean isJsonObjectTypeMatch(JSONObject jsonObject) { 41 | return jsonObject.containsKey(TypeKey) && jsonObject.containsKey(ContentKey) && jsonObject.size() == 2; 42 | } 43 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/DeleteMetaRequest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | public class DeleteMetaRequest extends Timeline { 7 | public static class Builder extends Timeline.Builder { 8 | // the data is deleted at the same time by default 9 | private boolean deleteData = true; 10 | private boolean recursive; 11 | 12 | public Builder(String metric) { 13 | super(metric); 14 | this.deleteData = true; // the data is deleted at the same time by default 15 | this.recursive = true; // the timelines are deleted recursively by default 16 | } 17 | 18 | public Builder tag(String tagk, String tagv) { 19 | super.tag(tagk, tagv); 20 | return this; 21 | } 22 | 23 | public Builder tag(Map tags) { 24 | super.tag(tags); 25 | return this; 26 | } 27 | 28 | public Builder fields(List fields) { 29 | super.fields(fields); 30 | return this; 31 | } 32 | 33 | public Builder deleteData(boolean deleteData) { 34 | this.deleteData = deleteData; 35 | return this; 36 | } 37 | 38 | public Builder recursive(boolean recursive) { 39 | this.recursive = recursive; 40 | return this; 41 | } 42 | 43 | public DeleteMetaRequest build() { 44 | DeleteMetaRequest request = new DeleteMetaRequest(); 45 | 46 | Timeline timeline = super.build(); 47 | request.setMetric(timeline.getMetric()); 48 | request.setTags(timeline.getTags()); 49 | if ((timeline.getFields() != null) && (!timeline.getFields().isEmpty())) { 50 | request.setFields(timeline.getFields()); 51 | } 52 | request.setDeleteData(this.deleteData); 53 | request.setRecursive(this.recursive); 54 | 55 | return request; 56 | } 57 | } 58 | 59 | private boolean deleteData; 60 | private boolean recursive; 61 | 62 | public boolean isRecursive() { 63 | return recursive; 64 | } 65 | 66 | public void setRecursive(boolean recursive) { 67 | this.recursive = recursive; 68 | } 69 | 70 | public boolean isDeleteData() { 71 | return deleteData; 72 | } 73 | 74 | public void setDeleteData(boolean deleteData) { 75 | this.deleteData = deleteData; 76 | } 77 | 78 | public DeleteMetaRequest() { 79 | super(); 80 | this.deleteData = true; 81 | this.recursive = true; 82 | } 83 | 84 | public static DeleteMetaRequest.Builder metric(String metric) { 85 | return new DeleteMetaRequest.Builder(metric); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/DeltaOptions.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | /** 4 | * Created By jianhong.hjh 5 | * Date: 2019/08/30 6 | */ 7 | public class DeltaOptions { 8 | 9 | private Boolean counter; 10 | 11 | private Boolean dropResets; 12 | 13 | private Long counterMax; 14 | 15 | public static class Builder { 16 | 17 | private Boolean counter; 18 | 19 | private Boolean dropResets; 20 | 21 | private Long counterMax; 22 | 23 | public Builder() { 24 | 25 | } 26 | 27 | public Builder counter(boolean counter) { 28 | this.counter = counter; 29 | return this; 30 | } 31 | 32 | public Builder dropResets(boolean dropResets) { 33 | this.dropResets = dropResets; 34 | return this; 35 | } 36 | 37 | public Builder counterMax(long counterMax) { 38 | this.counterMax = counterMax; 39 | return this; 40 | } 41 | 42 | public DeltaOptions build() { 43 | DeltaOptions rateOptions = new DeltaOptions(); 44 | if (counter != null) { 45 | rateOptions.counter = counter; 46 | } 47 | if (dropResets != null) { 48 | rateOptions.dropResets = dropResets; 49 | } 50 | if (counterMax != null) { 51 | rateOptions.counterMax = counterMax; 52 | } 53 | return rateOptions; 54 | } 55 | } 56 | 57 | 58 | public static Builder newBuilder() { 59 | return new Builder(); 60 | } 61 | 62 | public Boolean getCounter() { 63 | return counter; 64 | } 65 | 66 | public void setCounter(Boolean counter) { 67 | this.counter = counter; 68 | } 69 | 70 | public Boolean getDropResets() { 71 | return dropResets; 72 | } 73 | 74 | public void setDropResets(Boolean dropResets) { 75 | this.dropResets = dropResets; 76 | } 77 | 78 | public Long getCounterMax() { 79 | return counterMax; 80 | } 81 | 82 | public void setCounterMax(Long counterMax) { 83 | this.counterMax = counterMax; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/DumpMetaValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | public class DumpMetaValue extends JSONValue { 6 | private String metric; 7 | private String tagkey; 8 | private String tagvalueprefix; 9 | private int max; 10 | 11 | private boolean dump_metric; 12 | 13 | public DumpMetaValue() { 14 | super(); 15 | } 16 | 17 | public DumpMetaValue(String tagkey, String tagvalueprefix, int max) { 18 | super(); 19 | this.tagkey = tagkey; 20 | this.tagvalueprefix = tagvalueprefix; 21 | this.max = max; 22 | this.dump_metric = false; 23 | } 24 | 25 | public DumpMetaValue(String tagkey, String tagvalueprefix, int max,boolean dumpMetric) { 26 | super(); 27 | this.tagkey = tagkey; 28 | this.tagvalueprefix = tagvalueprefix; 29 | this.max = max; 30 | this.dump_metric = dumpMetric; 31 | } 32 | 33 | public DumpMetaValue(String metric,String tagkey, String tagvalueprefix, int max) { 34 | super(); 35 | this.metric = metric; 36 | this.tagkey = tagkey; 37 | this.tagvalueprefix = tagvalueprefix; 38 | this.max = max; 39 | this.dump_metric = false; 40 | } 41 | 42 | 43 | public String getMetric() { 44 | return metric; 45 | } 46 | 47 | public void setMetric(String metric) { 48 | this.metric = metric; 49 | } 50 | 51 | public boolean isDump_metric() { 52 | return dump_metric; 53 | } 54 | 55 | public void setDump_metric(boolean dump_metric) { 56 | this.dump_metric = dump_metric; 57 | } 58 | 59 | public String getTagkey() { 60 | return tagkey; 61 | } 62 | 63 | public void setTagkey(String tagkey) { 64 | this.tagkey = tagkey; 65 | } 66 | 67 | public String getTagvalueprefix() { 68 | return tagvalueprefix; 69 | } 70 | 71 | public void setTagvalueprefix(String tagvalueprefix) { 72 | this.tagvalueprefix = tagvalueprefix; 73 | } 74 | 75 | public int getMax() { 76 | return max; 77 | } 78 | 79 | public void setMax(int max) { 80 | this.max = max; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/Filter.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.type.FilterType; 4 | 5 | public class Filter { 6 | private FilterType type; 7 | private String tagk; 8 | private String filter; 9 | private Boolean groupBy; 10 | 11 | public static class Builder { 12 | private FilterType type; 13 | private String tagk; 14 | private String filter; 15 | private boolean groupBy; 16 | 17 | public Builder(FilterType type, String tagk, String filter, boolean groupBy) { 18 | super(); 19 | this.type = type; 20 | this.tagk = tagk; 21 | this.filter = filter; 22 | this.groupBy = groupBy; 23 | } 24 | 25 | public Builder(FilterType type, String tagk, String filter) { 26 | super(); 27 | this.type = type; 28 | this.tagk = tagk; 29 | this.filter = filter; 30 | } 31 | 32 | public Filter build() { 33 | Filter f = new Filter(); 34 | f.type = this.type; 35 | f.tagk = this.tagk; 36 | f.filter = this.filter; 37 | if (this.groupBy) { 38 | f.groupBy = true; 39 | } 40 | 41 | return f; 42 | } 43 | 44 | } 45 | 46 | public static Builder filter(FilterType type, String tagk, String filter) { 47 | return new Builder(type, tagk, filter); 48 | } 49 | 50 | public static Builder filter(FilterType type, String tagk, String filter, boolean groupBy) { 51 | return new Builder(type, tagk, filter, groupBy); 52 | } 53 | 54 | public static Builder filter(FilterType type, String filter) { 55 | return new Builder(type, null, filter); 56 | } 57 | 58 | public FilterType getType() { 59 | return type; 60 | } 61 | 62 | public void setType(FilterType type) { 63 | this.type = type; 64 | } 65 | 66 | public String getTagk() { 67 | return tagk; 68 | } 69 | 70 | public void setTagk(String tagk) { 71 | this.tagk = tagk; 72 | } 73 | 74 | public String getFilter() { 75 | return filter; 76 | } 77 | 78 | public void setFilter(String filter) { 79 | this.filter = filter; 80 | } 81 | 82 | public Boolean getGroupBy() { 83 | return groupBy; 84 | } 85 | 86 | public void setGroupBy(Boolean groupBy) { 87 | this.groupBy = groupBy; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/LastLimit.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | /** 4 | * @author cuiyuan 5 | * @date 2020/7/22 12:05 下午 6 | */ 7 | public class LastLimit { 8 | long from; 9 | int size; 10 | boolean global; 11 | 12 | public LastLimit(long from, int size) { 13 | this(from, size, false) ; 14 | } 15 | 16 | public LastLimit(long from, int size, boolean global) { 17 | this.from = from; 18 | this.size = size; 19 | this.global = global; 20 | } 21 | 22 | public long getFrom() { 23 | return from; 24 | } 25 | 26 | public void setFrom(long from) { 27 | this.from = from; 28 | } 29 | 30 | public int getSize() { 31 | return size; 32 | } 33 | 34 | public void setSize(int size) { 35 | this.size = size; 36 | } 37 | 38 | public boolean isGlobal() { 39 | return global; 40 | } 41 | 42 | public void setGlobal(boolean global) { 43 | this.global = global; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/LookupTagFilter.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | import com.aliyun.hitsdb.client.value.JSONValue; 6 | 7 | public class LookupTagFilter extends JSONValue { 8 | private String key; 9 | private String value; 10 | 11 | public LookupTagFilter() { 12 | super(); 13 | } 14 | 15 | public LookupTagFilter(String key, String value) { 16 | this.value = value; 17 | this.key = key; 18 | } 19 | 20 | public String getKey() { 21 | return key; 22 | } 23 | 24 | public String getValue() { 25 | return value; 26 | } 27 | 28 | public void setKey(String key) { 29 | this.key = key; 30 | } 31 | 32 | public void setValue(String value) { 33 | this.value = value; 34 | } 35 | 36 | public String toJSON() { 37 | return JSON.toJSONString(this, SerializerFeature.DisableCircularReferenceDetect); 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/MetricTimeRange.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class MetricTimeRange extends JSONValue { 9 | private String metric; 10 | /** 11 | * Optional tags input parameters. 12 | * added for TSDB v2.4.1 because newly added optional "tags" in api/delete_data 13 | */ 14 | private Map tags; 15 | /** 16 | * Optional fields input parameters. 17 | * If provided, we only delete data that belong to provided fields. 18 | */ 19 | private List fields; 20 | private long start; 21 | private long end; 22 | 23 | public MetricTimeRange() { 24 | super(); 25 | } 26 | 27 | public MetricTimeRange(String metric, long start, long end) { 28 | this(metric, null, null, start, end); 29 | } 30 | 31 | public MetricTimeRange(String metric, List fields, long start, long end) { 32 | this(metric, null, fields, start, end); 33 | } 34 | 35 | public MetricTimeRange(String metric, Map tags, long start, long end) { 36 | this(metric, tags, null, start, end); 37 | } 38 | 39 | public MetricTimeRange(String metric, Map tags, List fields, long start, long end) { 40 | super(); 41 | this.metric = metric; 42 | this.tags = tags; 43 | this.fields = fields; 44 | this.start = start; 45 | this.end = end; 46 | } 47 | 48 | public String getMetric() { 49 | return metric; 50 | } 51 | 52 | public void setMetric(String metric) { 53 | this.metric = metric; 54 | } 55 | 56 | public Map getTags() { 57 | return tags; 58 | } 59 | 60 | public void setTags(Map tags) { 61 | this.tags = tags; 62 | } 63 | 64 | public List getFields() { 65 | return fields; 66 | } 67 | 68 | public void setFields(List fields) { 69 | this.fields = fields; 70 | } 71 | 72 | public long getStart() { 73 | return start; 74 | } 75 | 76 | public void setStart(long start) { 77 | this.start = start; 78 | } 79 | 80 | public long getEnd() { 81 | return end; 82 | } 83 | 84 | public void setEnd(long end) { 85 | this.end = end; 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/PointType.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | public enum PointType { 4 | SINGLE_VALUE, 5 | MULTI_FIELD 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/PointsCollection.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.annotation.JSONField; 5 | import com.alibaba.fastjson.serializer.SerializerFeature; 6 | import com.aliyun.hitsdb.client.callback.AbstractBatchPutCallback; 7 | import com.aliyun.hitsdb.client.callback.AbstractMultiFieldBatchPutCallback; 8 | import com.aliyun.hitsdb.client.util.Objects; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Collection; 12 | import java.util.List; 13 | 14 | public class PointsCollection extends ArrayList { 15 | @JSONField(serialize = false) 16 | private final AbstractBatchPutCallback simplePointBatchCallbak; 17 | @JSONField(serialize = false) 18 | private final AbstractMultiFieldBatchPutCallback multiFieldBatchPutCallback; 19 | 20 | public PointsCollection(Collection points, AbstractBatchPutCallback callback) { 21 | Objects.requireNonNull(points); 22 | Objects.requireNonNull(callback); 23 | 24 | addAll(points); 25 | this.simplePointBatchCallbak = callback; 26 | this.multiFieldBatchPutCallback = null; 27 | } 28 | 29 | public PointsCollection(Collection multiFieldPoints, AbstractMultiFieldBatchPutCallback callback) { 30 | Objects.requireNonNull(multiFieldPoints); 31 | Objects.requireNonNull(callback); 32 | 33 | addAll(multiFieldPoints); 34 | this.simplePointBatchCallbak = null; 35 | this.multiFieldBatchPutCallback = callback; 36 | } 37 | 38 | public AbstractBatchPutCallback getSimplePointBatchCallbak() { 39 | return simplePointBatchCallbak; 40 | } 41 | 42 | public AbstractMultiFieldBatchPutCallback getMultiFieldBatchPutCallback() { 43 | return multiFieldBatchPutCallback; 44 | } 45 | 46 | public List asSingleFieldPoints() { 47 | List retval = new ArrayList(this.size()); 48 | for (AbstractPoint p : this) { 49 | if (!(p instanceof Point)) { 50 | throw new IllegalStateException("it's not a SingleFieldPoint collection"); 51 | } 52 | retval.add((Point)p); 53 | } 54 | return retval; 55 | } 56 | 57 | public List asMultiFieldPoints() { 58 | List retval = new ArrayList(this.size()); 59 | for (AbstractPoint p : this) { 60 | if (!(p instanceof MultiFieldPoint)) { 61 | throw new IllegalStateException("it's not a MultiFieldPoint collection"); 62 | } 63 | retval.add((MultiFieldPoint)p); 64 | } 65 | return retval; 66 | } 67 | 68 | public String toJSON() { 69 | return JSON.toJSONString(this, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.SortField, SerializerFeature.SortField.MapSortField); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/RateOptions.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | /** 4 | * Created By jianhong.hjh 5 | * Date: 2018/11/5 6 | */ 7 | public class RateOptions { 8 | 9 | private Boolean counter; 10 | 11 | private Boolean dropResets; 12 | 13 | private Long counterMax; 14 | 15 | private Long resetValue; 16 | 17 | public static class Builder { 18 | 19 | private Boolean counter; 20 | 21 | private Boolean dropResets; 22 | 23 | private Long counterMax; 24 | 25 | private Long resetValue; 26 | 27 | public Builder() { 28 | 29 | } 30 | 31 | public Builder counter(boolean counter) { 32 | this.counter = counter; 33 | return this; 34 | } 35 | 36 | public Builder dropResets(boolean dropResets) { 37 | this.dropResets = dropResets; 38 | return this; 39 | } 40 | 41 | public Builder counterMax(long counterMax) { 42 | this.counterMax = counterMax; 43 | return this; 44 | } 45 | 46 | public Builder resetValue(long resetValue) { 47 | this.resetValue = resetValue; 48 | return this; 49 | } 50 | 51 | public RateOptions build() { 52 | RateOptions rateOptions = new RateOptions(); 53 | if (counter != null) { 54 | rateOptions.counter = counter; 55 | } 56 | if (dropResets != null) { 57 | rateOptions.dropResets = dropResets; 58 | } 59 | if (counterMax != null) { 60 | rateOptions.counterMax = counterMax; 61 | } 62 | if (resetValue != null) { 63 | rateOptions.resetValue = resetValue; 64 | } 65 | return rateOptions; 66 | } 67 | } 68 | 69 | 70 | public static Builder newBuilder() { 71 | return new Builder(); 72 | } 73 | 74 | public Boolean getCounter() { 75 | return counter; 76 | } 77 | 78 | public void setCounter(Boolean counter) { 79 | this.counter = counter; 80 | } 81 | 82 | public Boolean getDropResets() { 83 | return dropResets; 84 | } 85 | 86 | public void setDropResets(Boolean dropResets) { 87 | this.dropResets = dropResets; 88 | } 89 | 90 | public Long getCounterMax() { 91 | return counterMax; 92 | } 93 | 94 | public void setCounterMax(Long counterMax) { 95 | this.counterMax = counterMax; 96 | } 97 | 98 | public Long getResetValue() { 99 | return resetValue; 100 | } 101 | 102 | public void setResetValue(Long resetValue) { 103 | this.resetValue = resetValue; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/SQLValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | public class SQLValue extends JSONValue { 6 | private String sql; 7 | 8 | public SQLValue(String sql) { 9 | this.sql = sql; 10 | } 11 | 12 | public String getSql() { 13 | return sql; 14 | } 15 | 16 | public void setSql(String sql) { 17 | this.sql = sql; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/SuggestValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | public class SuggestValue extends JSONValue { 6 | private String type; 7 | /** 8 | * Optional metric input parameter. 9 | * Used for querying tagk, tagv, fields under certain metric. 10 | */ 11 | private String metric; 12 | private String q; 13 | private int max; 14 | 15 | public SuggestValue() { 16 | super(); 17 | } 18 | 19 | public SuggestValue(String type, String q, int max) { 20 | super(); 21 | this.type = type; 22 | this.q = q; 23 | this.max = max; 24 | } 25 | 26 | public SuggestValue(String type, String metric, String q, int max) { 27 | super(); 28 | this.type = type; 29 | this.metric = metric; 30 | this.q = q; 31 | this.max = max; 32 | } 33 | 34 | public String getType() { 35 | return type; 36 | } 37 | 38 | public void setType(String type) { 39 | this.type = type; 40 | } 41 | 42 | public String getMetric() { 43 | return metric; 44 | } 45 | 46 | public void setMetric(String metric) { 47 | this.metric = metric; 48 | } 49 | 50 | public String getQ() { 51 | return q; 52 | } 53 | 54 | public void setQ(String q) { 55 | this.q = q; 56 | } 57 | 58 | public int getMax() { 59 | return max; 60 | } 61 | 62 | public void setMax(int max) { 63 | this.max = max; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/TTLValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | public class TTLValue extends JSONValue { 6 | private int val; 7 | 8 | public TTLValue() { 9 | super(); 10 | } 11 | 12 | public TTLValue(int val) { 13 | super(); 14 | this.val = val; 15 | } 16 | 17 | public int getVal() { 18 | return val; 19 | } 20 | 21 | public void setVal(int val) { 22 | this.val = val; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/TagsAddInfo.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * @author johnnyzou 12 | */ 13 | public class TagsAddInfo extends JSONValue { 14 | private String metric; 15 | private Map tags; 16 | private List _ids; 17 | 18 | public TagsAddInfo(String metric, Map tags, List _ids) { 19 | this.metric = metric; 20 | this.tags = tags; 21 | this._ids = _ids; 22 | } 23 | 24 | public static class Builder { 25 | private String metric; 26 | private Map tags = new HashMap(); 27 | private List _ids = new ArrayList(); 28 | 29 | public Builder(String metric) { 30 | this.metric = metric; 31 | } 32 | 33 | public Builder tag(String tagk, String tagv) { 34 | this.tags.put(tagk, tagv); 35 | return this; 36 | } 37 | 38 | public Builder id(String id) { 39 | this._ids.add(id); 40 | return this; 41 | } 42 | 43 | public Builder ids(List ids) { 44 | this._ids.addAll(ids); 45 | return this; 46 | } 47 | 48 | public TagsAddInfo build() { 49 | if (tags.size() == 0) { 50 | throw new IllegalArgumentException("missing tags."); 51 | } 52 | if (this._ids.size() == 0) { 53 | throw new IllegalArgumentException("missing ids."); 54 | } 55 | return new TagsAddInfo(this.metric, this.tags, this._ids); 56 | } 57 | } 58 | 59 | public String getMetric() { 60 | return metric; 61 | } 62 | 63 | public Map getTags() { 64 | return tags; 65 | } 66 | 67 | public List get_ids() { 68 | return _ids; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/TagsRemoveInfo.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author johnnyzou 10 | */ 11 | public class TagsRemoveInfo extends JSONValue{ 12 | private String metric; 13 | private Map tags; 14 | private String _id; 15 | 16 | public TagsRemoveInfo(String metric, Map tags, String _id) { 17 | this.metric = metric; 18 | this.tags = tags; 19 | this._id = _id; 20 | } 21 | 22 | public static class Builder { 23 | private String metric; 24 | private Map tags = new HashMap(); 25 | private String _id; 26 | 27 | public Builder(String metric, String _id) { 28 | this.metric = metric; 29 | this._id = _id; 30 | } 31 | 32 | public Builder tag(String tagk, String tagv) { 33 | this.tags.put(tagk, tagv); 34 | return this; 35 | } 36 | 37 | public TagsRemoveInfo build() { 38 | return new TagsRemoveInfo(this.metric, this.tags, this._id); 39 | } 40 | } 41 | 42 | public String getMetric() { 43 | return metric; 44 | } 45 | 46 | public Map getTags() { 47 | return tags; 48 | } 49 | 50 | public String get_id() { 51 | return _id; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/TagsShowInfo.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | /** 6 | * @author johnnyzou 7 | */ 8 | public class TagsShowInfo extends JSONValue { 9 | private String metric; 10 | private String _id; 11 | 12 | public TagsShowInfo(String metric, String _id) { 13 | this.metric = metric; 14 | this._id = _id; 15 | } 16 | public String getMetric() { 17 | return metric; 18 | } 19 | 20 | public String get_id() { 21 | return _id; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/Timeline.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import com.aliyun.hitsdb.client.value.JSONValue; 9 | 10 | public class Timeline extends JSONValue { 11 | 12 | public static class Builder { 13 | private String metric; 14 | private Map tags = new HashMap(); 15 | /** 16 | * Optional fields input parameter. 17 | * Used for fields' timelines under specific metric. 18 | */ 19 | private List fields = null; 20 | 21 | public Builder(String metric) { 22 | this.metric = metric; 23 | } 24 | 25 | public Builder tag(String tagk, String tagv) { 26 | this.tags.put(tagk, tagv); 27 | return this; 28 | } 29 | 30 | public Builder tag(Map tags) { 31 | this.tags.putAll(tags); 32 | return this; 33 | } 34 | 35 | public Builder fields(List fields) { 36 | if (fields == null || fields.isEmpty()) { 37 | return this; 38 | } 39 | this.fields = new ArrayList(); 40 | this.fields.addAll(fields); 41 | return this; 42 | } 43 | 44 | public Timeline build() { 45 | Timeline timeline = new Timeline(); 46 | timeline.metric = this.metric; 47 | timeline.tags = this.tags; 48 | if (this.fields != null && !this.fields.isEmpty()) { 49 | timeline.fields = fields; 50 | } 51 | return timeline; 52 | } 53 | } 54 | 55 | public static Builder metric(String metric) { 56 | return new Builder(metric); 57 | } 58 | 59 | private String metric; 60 | private Map tags; 61 | /** 62 | * Optional fields input parameter. 63 | * Used for fields' timelines under specific metric. 64 | */ 65 | private List fields = null; 66 | 67 | public Timeline() { 68 | super(); 69 | } 70 | 71 | public List getFields() { 72 | return fields; 73 | } 74 | 75 | public void setFields(List fields) { 76 | this.fields = fields; 77 | } 78 | 79 | public String getMetric() { 80 | return metric; 81 | } 82 | 83 | public void setMetric(String metric) { 84 | this.metric = metric; 85 | } 86 | 87 | public Map getTags() { 88 | return tags; 89 | } 90 | 91 | public void setTags(Map tags) { 92 | this.tags = tags; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/request/ValueSerializer.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.alibaba.fastjson.parser.DefaultJSONParser; 6 | import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer; 7 | import com.alibaba.fastjson.serializer.JSONSerializer; 8 | import com.alibaba.fastjson.serializer.ObjectSerializer; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import java.io.IOException; 13 | import java.lang.reflect.Type; 14 | 15 | /** 16 | * @author cuiyuan 17 | * @date 2020/8/4 11:30 上午 18 | */ 19 | public class ValueSerializer implements ObjectSerializer, ObjectDeserializer { 20 | 21 | private static final Logger log = LoggerFactory.getLogger(ValueSerializer.class); 22 | 23 | @Override 24 | public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type, int i) throws IOException { 25 | if (o instanceof byte[]) { 26 | ByteArrayValue bv = new ByteArrayValue((byte[]) o); 27 | jsonSerializer.write(bv); 28 | return; 29 | } 30 | jsonSerializer.write(o); 31 | } 32 | 33 | @Override 34 | public Object deserialze(DefaultJSONParser parser, Type type, Object o) { 35 | Object parse = parser.parse(); 36 | if (parse == null) { 37 | return null; 38 | } 39 | if (parse instanceof JSONObject) { 40 | JSONObject jsonObject = (JSONObject) parse; 41 | if (ComplexValue.isJsonObjectTypeMatch(jsonObject)) { 42 | String valueType = jsonObject.getString(ByteArrayValue.TypeKey); 43 | if (ByteArrayValue.TypeValue.equals(valueType)) { 44 | ByteArrayValue bv = JSON.parseObject(jsonObject.toJSONString(), ByteArrayValue.class); 45 | return bv.decode(); 46 | } else if (GeoPointValue.TypeValue.equals(valueType)) { 47 | GeoPointValue gp = JSON.parseObject(jsonObject.toJSONString(), GeoPointValue.class); 48 | return gp; 49 | } else { 50 | log.error("Illegal value type {}", valueType); 51 | throw new IllegalArgumentException("Illegal value type " + valueType); 52 | } 53 | } 54 | } 55 | return parse; 56 | } 57 | 58 | @Override 59 | public int getFastMatchToken() { 60 | return 0; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/LookupDetailedResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | import com.alibaba.fastjson.JSON; 7 | import com.alibaba.fastjson.serializer.SerializerFeature; 8 | import com.aliyun.hitsdb.client.value.JSONValue; 9 | 10 | public class LookupDetailedResult extends JSONValue { 11 | private Map tags = new LinkedHashMap(); 12 | private String metric; 13 | private String tsuid; 14 | 15 | public LookupDetailedResult() { 16 | super(); 17 | } 18 | 19 | public String getTsuid() { 20 | return tsuid; 21 | } 22 | 23 | public String getMetric() { 24 | return metric; 25 | } 26 | 27 | public Map getTags() { 28 | return tags; 29 | } 30 | 31 | public void setTsuid(String tsuid) { 32 | this.tsuid = tsuid; 33 | } 34 | 35 | public void setTags(Map tags) { 36 | this.tags = tags; 37 | } 38 | 39 | public void setMetric(String metric) { 40 | this.metric = metric; 41 | } 42 | 43 | public String toJSON() { 44 | return JSON.toJSONString(this, SerializerFeature.DisableCircularReferenceDetect); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/LookupResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.alibaba.fastjson.JSON; 7 | import com.alibaba.fastjson.serializer.SerializerFeature; 8 | import com.aliyun.hitsdb.client.value.JSONValue; 9 | import com.aliyun.hitsdb.client.value.request.LookupTagFilter; 10 | 11 | public class LookupResult extends JSONValue { 12 | private String type; 13 | private String metric; 14 | private List tags = new ArrayList(); 15 | private Integer limit = null; 16 | private int time; 17 | private List results = new ArrayList(); 18 | private int totalResults; 19 | 20 | public LookupResult() { 21 | super(); 22 | } 23 | 24 | public String getType() { 25 | return type; 26 | } 27 | 28 | public String getMetric() { 29 | return metric; 30 | } 31 | 32 | public List getTags() { 33 | return tags; 34 | } 35 | 36 | public Integer getLimit() { 37 | return limit; 38 | } 39 | 40 | public int getTime() { 41 | return time; 42 | } 43 | 44 | public List getResults() { 45 | return results; 46 | } 47 | 48 | public int getTotalResults() { 49 | return totalResults; 50 | } 51 | 52 | public void setType(String type) { 53 | this.type = type; 54 | } 55 | 56 | public void setMetric(String metric) { 57 | this.metric = metric; 58 | } 59 | 60 | public void setTags(List tags) { 61 | this.tags = tags; 62 | } 63 | 64 | public void setLimit(Integer limit) { 65 | this.limit = limit; 66 | } 67 | 68 | public void setTime(int time) { 69 | this.time = time; 70 | } 71 | 72 | public void setResults(List results) { 73 | this.results = results; 74 | } 75 | 76 | public void setTotalResults(int totalResults) { 77 | this.totalResults = totalResults; 78 | } 79 | 80 | public String toJSON() { 81 | return JSON.toJSONString(this, SerializerFeature.DisableCircularReferenceDetect); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/MultiFieldQueryLastResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.LinkedHashMap; 8 | import java.util.LinkedList; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | public class MultiFieldQueryLastResult { 13 | private String metric; 14 | private Map tags = new HashMap(); 15 | private List columns = new ArrayList(); 16 | @JSONField(serializeUsing = MultiFieldQueryValuesSerializer.class, deserializeUsing = MultiFieldQueryValuesSerializer.class) 17 | private List> values = new ArrayList>(); 18 | 19 | public String getMetric() { 20 | return metric; 21 | } 22 | 23 | public List getColumns() { 24 | return columns; 25 | } 26 | 27 | public List> getValues() { 28 | return values; 29 | } 30 | 31 | public void setTags(Map tags) { 32 | this.tags = tags; 33 | } 34 | 35 | public void setMetric(String metric) { 36 | this.metric = metric; 37 | } 38 | 39 | public void setColumns(List columns) { 40 | this.columns = columns; 41 | } 42 | 43 | public void setValues(List> values) { 44 | this.values = values; 45 | } 46 | 47 | public Map getTags() { 48 | return tags; 49 | } 50 | 51 | public List> asMap() { 52 | final List> columnValueMap = new LinkedList>(); 53 | for (List value : values) { 54 | final LinkedHashMap kv = new LinkedHashMap(); 55 | for (int j = 0; j < columns.size(); j++) { 56 | kv.put(columns.get(j), value.get(j)); 57 | } 58 | columnValueMap.add(kv); 59 | } 60 | return columnValueMap; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/MultiFieldQueryResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.alibaba.fastjson.serializer.ValueFilter; 5 | 6 | import java.util.ArrayList; 7 | import java.util.LinkedHashMap; 8 | import java.util.LinkedList; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | public class MultiFieldQueryResult { 13 | private String metric; 14 | private List aggregatedTags; 15 | private Map tags; 16 | private List columns = new ArrayList(); 17 | @JSONField(serializeUsing = MultiFieldQueryValuesSerializer.class, deserializeUsing = MultiFieldQueryValuesSerializer.class) 18 | private List> values = new ArrayList>(); 19 | private List> types; 20 | 21 | public String getMetric() { 22 | return metric; 23 | } 24 | 25 | public List getAggregatedTags() { 26 | return aggregatedTags; 27 | } 28 | 29 | public List getColumns() { 30 | return columns; 31 | } 32 | 33 | public List> getValues() { 34 | return values; 35 | } 36 | 37 | public void setTags(Map tags) { 38 | this.tags = tags; 39 | } 40 | 41 | public void setMetric(String metric) { 42 | this.metric = metric; 43 | } 44 | 45 | public void setAggregatedTags(List aggregatedTags) { 46 | this.aggregatedTags = aggregatedTags; 47 | } 48 | 49 | public void setColumns(List columns) { 50 | this.columns = columns; 51 | } 52 | 53 | public void setValues(List> values) { 54 | this.values = values; 55 | } 56 | 57 | public Map getTags() { 58 | return tags; 59 | } 60 | 61 | public List> getTypes() { 62 | return types; 63 | } 64 | 65 | public void setTypes(List> types) { 66 | this.types = types; 67 | } 68 | 69 | public List> asMap() { 70 | final List> columnValueMap = new LinkedList>(); 71 | for (List value : values) { 72 | final LinkedHashMap kv = new LinkedHashMap(); 73 | for (int j = 0; j < columns.size(); j++) { 74 | kv.put(columns.get(j), value.get(j)); 75 | } 76 | columnValueMap.add(kv); 77 | } 78 | return columnValueMap; 79 | } 80 | 81 | @Override 82 | public String toString() { 83 | return "MultiFieldQueryResult{" + 84 | "metric='" + metric + '\'' + 85 | ", aggregatedTags=" + aggregatedTags + 86 | ", tags=" + tags + 87 | ", columns=" + columns + 88 | ", values=" + values + 89 | '}'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/MultiValuedQueryLastResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.alibaba.fastjson.annotation.JSONType; 4 | import com.aliyun.hitsdb.client.value.JSONValue; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | @Deprecated 11 | @JSONType(ignores = { "dps" }) 12 | public class MultiValuedQueryLastResult extends JSONValue { 13 | private String name; 14 | private List columns = new ArrayList(); 15 | private List> values = new ArrayList>(); 16 | private List> dps = new ArrayList>(); 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public List getColumns() { 23 | return columns; 24 | } 25 | 26 | public List> getValues() { 27 | return values; 28 | } 29 | 30 | public List> getDps() { 31 | return dps; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | 38 | public void setColumns(List columns) { 39 | this.columns = columns; 40 | } 41 | 42 | public void setValues(List> values) { 43 | this.values = values; 44 | } 45 | 46 | public void setDps(List> dps) { 47 | this.dps = dps; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/MultiValuedQueryResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import java.util.*; 4 | 5 | import com.alibaba.fastjson.annotation.JSONType; 6 | import com.aliyun.hitsdb.client.value.JSONValue; 7 | 8 | @Deprecated 9 | @JSONType(ignores = { "dps", "aggregateTags" }) 10 | public class MultiValuedQueryResult extends JSONValue { 11 | private String name; 12 | private List> aggregateTags; 13 | private List columns = new ArrayList(); 14 | private List> values = new ArrayList>(); 15 | private List> dps = new ArrayList(); 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public List> getAggregateTags() { 22 | return aggregateTags; 23 | } 24 | 25 | public List getColumns() { 26 | return columns; 27 | } 28 | 29 | public List> getValues() { 30 | return values; 31 | } 32 | 33 | public List> getDps() { 34 | return dps; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public void setAggregateTags(List> aggregateTags) { 42 | this.aggregateTags = aggregateTags; 43 | } 44 | 45 | public void setColumns(List columns) { 46 | this.columns = columns; 47 | } 48 | 49 | public void setValues(List> values) { 50 | this.values = values; 51 | } 52 | 53 | public void setDps(List> dps) { 54 | this.dps = dps; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/MultiValuedTupleComparator.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.alibaba.fastjson.annotation.JSONType; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.util.Comparator; 8 | import java.util.List; 9 | 10 | @Deprecated 11 | @JSONType(ignores = { "timestamp" }) 12 | public class MultiValuedTupleComparator implements Comparator> { 13 | 14 | private static final Logger LOGGER = LoggerFactory.getLogger(MultiValuedTupleComparator.class); 15 | 16 | public Boolean reverseOrder = false; 17 | 18 | public MultiValuedTupleComparator() { 19 | 20 | } 21 | 22 | public MultiValuedTupleComparator(Boolean reverseOrder) { 23 | this.reverseOrder = reverseOrder; 24 | } 25 | 26 | @Override 27 | public int compare(List tupleA, List tupleB) { 28 | /** 29 | * Comparison procedure: 30 | * 1. Timestamp --- The first field value of the tuple 31 | * 2. Tuple field values (toString()) 32 | */ 33 | if (tupleA == null || tupleA.isEmpty() || tupleB == null || tupleB.isEmpty()) { 34 | LOGGER.error("Unable to perform these two tuples. {} ||| {}", tupleA == null ? "NULL" : tupleA.toString(), 35 | tupleB == null ? "NULL" : tupleB.toString()); 36 | return 0; 37 | } 38 | int comparingResult = 0; 39 | 40 | Long timestampA = (Long) tupleA.get(0); 41 | Long timestampB = (Long) tupleB.get(0); 42 | 43 | if (!timestampA.equals(timestampB)) { 44 | comparingResult = (int)(timestampA - timestampB); 45 | } else { 46 | if (tupleA.size() != tupleB.size()) { 47 | comparingResult = (tupleA.size() - tupleB.size()); 48 | } else { 49 | comparingResult = (tupleA.toString().compareTo(tupleB.toString())); 50 | } 51 | } 52 | 53 | if (reverseOrder) { 54 | comparingResult = (negateExact(comparingResult)); 55 | } 56 | return comparingResult; 57 | } 58 | 59 | 60 | public static int negateExact(int a) { 61 | if (a == Integer.MIN_VALUE) { 62 | throw new ArithmeticException("integer overflow"); 63 | } 64 | 65 | return -a; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/SQLResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class SQLResult extends JSONValue { 9 | List columns; 10 | List metadata; 11 | List> rows; 12 | 13 | public List getColumns() { 14 | return columns; 15 | } 16 | 17 | public void setColumns(List columns) { 18 | this.columns = columns; 19 | } 20 | 21 | public List getMetadata() { 22 | return metadata; 23 | } 24 | 25 | public void setMetadata(List metadata) { 26 | this.metadata = metadata; 27 | } 28 | 29 | public List> getRows() { 30 | return rows; 31 | } 32 | 33 | public void setRows(List> rows) { 34 | this.rows = rows; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/TTLResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.aliyun.hitsdb.client.value.JSONValue; 4 | 5 | public class TTLResult extends JSONValue { 6 | private int val; 7 | 8 | public TTLResult() { 9 | super(); 10 | } 11 | 12 | public TTLResult(int val) { 13 | super(); 14 | this.val = val; 15 | } 16 | 17 | public int getVal() { 18 | return val; 19 | } 20 | 21 | public void setVal(int val) { 22 | this.val = val; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/TagResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | import java.util.Map.Entry; 7 | import java.util.Set; 8 | 9 | import com.alibaba.fastjson.JSON; 10 | import com.alibaba.fastjson.JSONArray; 11 | import com.alibaba.fastjson.JSONObject; 12 | import com.aliyun.hitsdb.client.value.JSONValue; 13 | 14 | public class TagResult extends JSONValue { 15 | private String tagKey; 16 | private String tagValue; 17 | 18 | public TagResult(String tagKey, String tagValue) { 19 | this.tagKey = tagKey; 20 | this.tagValue = tagValue; 21 | } 22 | 23 | public String getTagKey() { 24 | return tagKey; 25 | } 26 | 27 | public String getTagValue() { 28 | return tagValue; 29 | } 30 | 31 | public static List parseList(String json) { 32 | JSONArray array = JSON.parseArray(json); 33 | List arrayList = new ArrayList(array.size()); 34 | 35 | Iterator iterator = array.iterator(); 36 | while (iterator.hasNext()) { 37 | JSONObject object = (JSONObject) iterator.next(); 38 | Set> entrySet = object.entrySet(); 39 | for (Entry entry : entrySet) { 40 | String key = entry.getKey(); 41 | String value = entry.getValue().toString(); 42 | TagResult tagResult = new TagResult(key, value); 43 | arrayList.add(tagResult); 44 | break; 45 | } 46 | } 47 | 48 | return arrayList; 49 | } 50 | 51 | @Override 52 | public String toJSON() { 53 | JSONObject jsonObject = new JSONObject(); 54 | jsonObject.put(tagKey, tagValue); 55 | return jsonObject.toJSONString(); 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/TagsAddResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.aliyun.hitsdb.client.util.Pair; 5 | import com.aliyun.hitsdb.client.value.Result; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author johnnyzou 11 | */ 12 | public class TagsAddResult extends Result { 13 | private List details; 14 | @JSONField(alternateNames = "failure") 15 | private int failed; 16 | private int success; 17 | 18 | public TagsAddResult() { 19 | super(); 20 | } 21 | 22 | public TagsAddResult(int success, int failed, List details) { 23 | super(); 24 | this.success = success; 25 | this.failed = failed; 26 | this.details = details; 27 | } 28 | 29 | public List getDetails() { 30 | return details; 31 | } 32 | 33 | public int getFailed() { 34 | return failed; 35 | } 36 | 37 | public int getSuccess() { 38 | return success; 39 | } 40 | 41 | public void setDetails(List details) { 42 | this.details = details; 43 | } 44 | 45 | public void setFailed(int failed) { 46 | this.failed = failed; 47 | } 48 | 49 | public void setSuccess(int success) { 50 | this.success = success; 51 | } 52 | 53 | public static class DetailPair { 54 | protected K id; 55 | protected V reason; 56 | 57 | public DetailPair(final K id, final V reason) { 58 | this.id = id; 59 | this.reason = reason; 60 | } 61 | public K getId() { 62 | return id; 63 | } 64 | 65 | public void setId(K id) { 66 | this.id = id; 67 | } 68 | public V getReason() { 69 | return reason; 70 | } 71 | 72 | public void setReason(V reason) { 73 | this.reason = reason; 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/TagsShowResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author johnnyzou 8 | */ 9 | public class TagsShowResult extends Result { 10 | private String metric; 11 | private String _id; 12 | private Map tags; 13 | 14 | public TagsShowResult(String metric, String _id, Map tags) { 15 | super(); 16 | this.metric = metric; 17 | this._id = _id; 18 | this.tags = tags; 19 | } 20 | 21 | public TagsShowResult() { 22 | super(); 23 | } 24 | 25 | public String getMetric() { 26 | return metric; 27 | } 28 | 29 | public void setMetric(String metric) { 30 | this.metric = metric; 31 | } 32 | 33 | public String get_id() { 34 | return _id; 35 | } 36 | 37 | public void set_id(String _id) { 38 | this._id = _id; 39 | } 40 | 41 | public Map getTags() { 42 | return tags; 43 | } 44 | 45 | public void setTags(Map tags) { 46 | this.tags = tags; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/UserResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | import com.aliyun.hitsdb.client.value.JSONValue; 6 | 7 | public class UserResult extends JSONValue { 8 | private String username; 9 | 10 | private String passcode; 11 | 12 | private int privilege; 13 | 14 | public String getUserName() { 15 | return username; 16 | } 17 | 18 | public void setUserName(String userName) { 19 | this.username = userName; 20 | } 21 | 22 | public String getPasscode() { 23 | return passcode; 24 | } 25 | 26 | public void setPasscode(String base64Password) { 27 | this.passcode = base64Password; 28 | } 29 | 30 | public int getPrivilege() { 31 | return privilege; 32 | } 33 | 34 | public void setPrivilege(int privilege) { 35 | this.privilege = privilege; 36 | } 37 | 38 | public UserResult(String userName, String passcode, int privilege) { 39 | this.username = userName; 40 | this.passcode = passcode; 41 | this.privilege = privilege; 42 | } 43 | 44 | @Override 45 | public String toJSON() { 46 | return JSON.toJSONString(this, SerializerFeature.WriteNullStringAsEmpty); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/DetailsResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import java.util.List; 4 | 5 | import com.aliyun.hitsdb.client.value.Result; 6 | import com.aliyun.hitsdb.client.value.request.Point; 7 | 8 | public class DetailsResult extends Result { 9 | private List errors; 10 | private int failed; 11 | private int success; 12 | /** 13 | * fatal is only used for Lindorm TSDB 14 | */ 15 | private List fatal; 16 | 17 | public DetailsResult() { 18 | super(); 19 | } 20 | 21 | public DetailsResult(int success, int failed, List errors, List fatal) { 22 | super(); 23 | this.success = success; 24 | this.failed = failed; 25 | this.errors = errors; 26 | this.fatal = fatal; 27 | } 28 | 29 | public List getFatal() { 30 | return fatal; 31 | } 32 | 33 | public void setFatal(List fatal) { 34 | this.fatal = fatal; 35 | } 36 | 37 | public List getErrors() { 38 | return errors; 39 | } 40 | 41 | public int getFailed() { 42 | return failed; 43 | } 44 | 45 | public int getSuccess() { 46 | return success; 47 | } 48 | 49 | public void setErrors(List errors) { 50 | this.errors = errors; 51 | } 52 | 53 | public void setFailed(int failed) { 54 | this.failed = failed; 55 | } 56 | 57 | public void setSuccess(int success) { 58 | this.success = success; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/ErrorPoint.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import com.aliyun.hitsdb.client.value.request.Point; 4 | 5 | public class ErrorPoint { 6 | private Point datapoint; 7 | private String error; 8 | 9 | public ErrorPoint() { 10 | super(); 11 | } 12 | 13 | public ErrorPoint(Point datapoint, String error) { 14 | super(); 15 | this.datapoint = datapoint; 16 | this.error = error; 17 | } 18 | 19 | public Point getDatapoint() { 20 | return datapoint; 21 | } 22 | 23 | public String getError() { 24 | return error; 25 | } 26 | 27 | public void setDatapoint(Point datapoint) { 28 | this.datapoint = datapoint; 29 | } 30 | 31 | public void setError(String error) { 32 | this.error = error; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/IgnoreErrorsResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Copyright @ 2020 alibaba.com 9 | * All right reserved. 10 | * Function:IgnoreErrors Result 11 | * 12 | * @author Benedict Jin 13 | * @since 2020/09/21 14 | */ 15 | public class IgnoreErrorsResult extends Result { 16 | 17 | private List ignoredErrors; 18 | private int failed; 19 | private int success; 20 | 21 | public IgnoreErrorsResult() { 22 | super(); 23 | } 24 | 25 | public IgnoreErrorsResult(int success, int failed, List ignoredErrors) { 26 | super(); 27 | this.success = success; 28 | this.failed = failed; 29 | this.ignoredErrors = ignoredErrors; 30 | } 31 | 32 | public List getIgnoredErrors() { 33 | return ignoredErrors; 34 | } 35 | 36 | public int getFailed() { 37 | return failed; 38 | } 39 | 40 | public int getSuccess() { 41 | return success; 42 | } 43 | 44 | public void setIgnoredErrors(List ignoredErrors) { 45 | this.ignoredErrors = ignoredErrors; 46 | } 47 | 48 | public void setFailed(int failed) { 49 | this.failed = failed; 50 | } 51 | 52 | public void setSuccess(int success) { 53 | this.success = success; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/MultiFieldDetailsResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 5 | 6 | import java.util.List; 7 | 8 | public class MultiFieldDetailsResult extends Result { 9 | private List errors; 10 | private int failed; 11 | private int success; 12 | /** 13 | * fatal is only used for Lindorm TSDB 14 | */ 15 | private List fatal; 16 | 17 | public MultiFieldDetailsResult() { 18 | super(); 19 | } 20 | 21 | public MultiFieldDetailsResult(int success, int failed, List errors, List fatal) { 22 | super(); 23 | this.success = success; 24 | this.failed = failed; 25 | this.errors = errors; 26 | this.fatal = fatal; 27 | } 28 | 29 | public List getFatal() { 30 | return fatal; 31 | } 32 | 33 | public void setFatal(List fatal) { 34 | this.fatal = fatal; 35 | } 36 | 37 | public List getErrors() { 38 | return errors; 39 | } 40 | 41 | public int getFailed() { 42 | return failed; 43 | } 44 | 45 | public int getSuccess() { 46 | return success; 47 | } 48 | 49 | public void setErrors(List errors) { 50 | this.errors = errors; 51 | } 52 | 53 | public void setFailed(int failed) { 54 | this.failed = failed; 55 | } 56 | 57 | public void setSuccess(int success) { 58 | this.success = success; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/MultiFieldErrorPoint.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 4 | 5 | public class MultiFieldErrorPoint { 6 | 7 | private MultiFieldPoint datapoint; 8 | private String error; 9 | 10 | public MultiFieldErrorPoint() { 11 | super(); 12 | } 13 | 14 | public MultiFieldErrorPoint(MultiFieldPoint datapoint, String error) { 15 | super(); 16 | this.datapoint = datapoint; 17 | this.error = error; 18 | } 19 | 20 | public MultiFieldPoint getDatapoint() { 21 | return datapoint; 22 | } 23 | 24 | public String getError() { 25 | return error; 26 | } 27 | 28 | public void setDatapoint(MultiFieldPoint datapoint) { 29 | this.datapoint = datapoint; 30 | } 31 | 32 | public void setError(String error) { 33 | this.error = error; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/MultiFieldIgnoreErrorsResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Copyright @ 2020 alibaba.com 9 | * All right reserved. 10 | * Function:Multi Field IgnoreErrors Result 11 | * 12 | * @author Benedict Jin 13 | * @since 2020/09/21 14 | */ 15 | public class MultiFieldIgnoreErrorsResult extends Result { 16 | 17 | private List ignoredErrors; 18 | private int failed; 19 | private int success; 20 | 21 | public MultiFieldIgnoreErrorsResult() { 22 | super(); 23 | } 24 | 25 | public MultiFieldIgnoreErrorsResult(int success, int failed, List ignoredErrors) { 26 | super(); 27 | this.success = success; 28 | this.failed = failed; 29 | this.ignoredErrors = ignoredErrors; 30 | } 31 | 32 | public List getIgnoredErrors() { 33 | return ignoredErrors; 34 | } 35 | 36 | public int getFailed() { 37 | return failed; 38 | } 39 | 40 | public int getSuccess() { 41 | return success; 42 | } 43 | 44 | public void setIgnoredErrors(List ignoredErrors) { 45 | this.ignoredErrors = ignoredErrors; 46 | } 47 | 48 | public void setFailed(int failed) { 49 | this.failed = failed; 50 | } 51 | 52 | public void setSuccess(int success) { 53 | this.success = success; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/response/batch/SummaryResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response.batch; 2 | 3 | import com.aliyun.hitsdb.client.value.Result; 4 | 5 | public class SummaryResult extends Result { 6 | private int failed; 7 | private int success; 8 | 9 | public SummaryResult() { 10 | super(); 11 | } 12 | 13 | public SummaryResult(int success, int failed) { 14 | super(); 15 | this.success = success; 16 | this.failed = failed; 17 | } 18 | 19 | public int getFailed() { 20 | return failed; 21 | } 22 | 23 | public int getSuccess() { 24 | return success; 25 | } 26 | 27 | public void setFailed(int failed) { 28 | this.failed = failed; 29 | } 30 | 31 | public void setSuccess(int success) { 32 | this.success = success; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/Aggregator.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public enum Aggregator { 7 | 8 | AVG("avg"), COUNT("count"), DEV("dev"), FIRST("first"), LAST("last"), 9 | MIMMIN("mimmin"), MIMMAX("mimmax"), MIN("min"), MAX("max"), NONE("none"), 10 | P50("p50"), P75("p75"), P90("p90"), P95("p95"), P99("p99"), P999("p999"), 11 | SUM("sum"), ZIMSUM("zimsum"); 12 | 13 | private static final Map CODE_MAP = new HashMap(); 14 | 15 | static { 16 | for (Aggregator typeEnum : Aggregator.values()) { 17 | CODE_MAP.put(typeEnum.getName(), typeEnum); 18 | } 19 | } 20 | 21 | public static Aggregator getEnum(String name) { 22 | return CODE_MAP.get(name); 23 | } 24 | 25 | private String name; 26 | 27 | private Aggregator(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return name; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/DownsampleDataSource.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public enum DownsampleDataSource { 7 | RAW("raw"), 8 | DOWNSAMPLE("downsample"); 9 | 10 | private final String name; 11 | 12 | private DownsampleDataSource(String name) { 13 | this.name = name; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return name; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/FilterType.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import com.aliyun.hitsdb.client.exception.http.HttpClientException; 6 | 7 | public enum FilterType { 8 | LiteralOr("literal_or"), 9 | NotLiteralOr("not_literal_or"), 10 | Wildcard("wildcard"), 11 | Regexp("regexp"), 12 | GeoIntersects("intersects"); 13 | 14 | private String name; 15 | 16 | private FilterType(String name) { 17 | this.name = name; 18 | Class superclass = this.getClass().getSuperclass(); 19 | try { 20 | Field field = superclass.getDeclaredField("name"); 21 | field.setAccessible(true); 22 | field.set(this, name); 23 | } catch (Exception e) { 24 | // 理论上不会触发该异常。 25 | throw new HttpClientException(e); 26 | } 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return name; 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/Granularity.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public enum Granularity { 7 | S1("1s"),S5("5s"), S15("15s"), M1("1m"), M5("5m"), M15("15m"), M60("60m"), H1("1h"), H2("2h"), H6("6h"), H24("24h"); 8 | 9 | private static final Map CODE_MAP = new HashMap(); 10 | 11 | static { 12 | for (Granularity typeEnum : Granularity.values()) { 13 | CODE_MAP.put(typeEnum.getName(), typeEnum); 14 | } 15 | } 16 | 17 | public static Granularity getEnum(String name) { 18 | return CODE_MAP.get(name); 19 | } 20 | 21 | private String name; 22 | 23 | private Granularity(String name) { 24 | this.name = name; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return name; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/HttpResponseLevel.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | public enum HttpResponseLevel { 4 | STATUS, SUMMARY, DETAIL 5 | } -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/QueryType.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public enum QueryType { 7 | ALL("ALL"), 8 | DOUBLE("DOUBLE"), 9 | STRING("STRING"), 10 | LATEST("LATEST"), 11 | UNKNOWN("UNKNOWN"); 12 | 13 | private static final Map CODE_MAP = new HashMap(); 14 | 15 | static { 16 | for (QueryType typeEnum : QueryType.values()) { 17 | CODE_MAP.put(typeEnum.getName(), typeEnum); 18 | } 19 | } 20 | 21 | public static QueryType getEnum(String name) { 22 | return CODE_MAP.get(name); 23 | } 24 | 25 | private String name; 26 | 27 | private QueryType(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return name; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/Suggest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | public enum Suggest { 4 | Metrics("metrics"), Field("field"), Tagk("tagk"), Tagv("tagv"); 5 | 6 | private String name; 7 | 8 | private Suggest(String name) { 9 | this.name = name; 10 | } 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/hitsdb/client/value/type/UserPrivilege.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.type; 2 | 3 | public enum UserPrivilege { 4 | WRITE_ONLY((byte)1), // 001 5 | READ_ONLY((byte)2), // 010 6 | READ_WRITE((byte)3), // 011 7 | SUPER((byte)7); // 111 8 | 9 | private final byte id; 10 | 11 | UserPrivilege(byte id) { this.id = id; } 12 | 13 | public byte id() { return this.id; } 14 | 15 | public static UserPrivilege fromId(int id) { 16 | switch (id) { 17 | case 1: 18 | return WRITE_ONLY; 19 | case 2: 20 | return READ_ONLY; 21 | case 3: 22 | return READ_WRITE; 23 | case 7: 24 | return SUPER; 25 | default: 26 | throw new IllegalArgumentException("No privilege match for id [" + id + "]"); 27 | } 28 | 29 | } 30 | 31 | public static UserPrivilege fromString(String privilege) { 32 | if ("writeonly".equals(privilege)) { 33 | return WRITE_ONLY; 34 | } else if ("readonly".equals(privilege)) { 35 | return READ_ONLY; 36 | } else if ("readwrite".equals(privilege)) { 37 | return READ_WRITE; 38 | } else if ("super".equals(privilege)) { 39 | return SUPER; 40 | } else { 41 | throw new IllegalArgumentException("No privilege match for string [" + privilege + "]"); 42 | } 43 | } 44 | 45 | public boolean canRead() { return ((this.id & READ_ONLY.id) == READ_ONLY.id); } 46 | 47 | public boolean canWrite() { 48 | return ((this.id & WRITE_ONLY.id) == WRITE_ONLY.id); 49 | } 50 | 51 | public boolean isSuper() { return this.id == SUPER.id; } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientDeleteMeta.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.io.IOException; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.aliyun.hitsdb.client.HiTSDB; 12 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 13 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 14 | import com.aliyun.hitsdb.client.value.request.Timeline; 15 | 16 | public class TestHiTSDBClientDeleteMeta { 17 | HiTSDB tsdb; 18 | 19 | @Before 20 | public void init() throws HttpClientInitException { 21 | tsdb = HiTSDBClientFactory.connect("127.0.0.1", 8242); 22 | } 23 | 24 | @After 25 | public void after() { 26 | try { 27 | tsdb.close(); 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | @Test 34 | public void testDeleteMeta1() { 35 | Timeline timeline = Timeline.metric("hello").tag("tagk1", "tagv1").build(); 36 | tsdb.deleteMeta(timeline); 37 | } 38 | 39 | @Test 40 | public void testDeleteMeta2() { 41 | Map tags = new HashMap(); 42 | tags.put("tagk1", "tagv1"); 43 | tsdb.deleteMeta("hello", tags); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientDumpMeta.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.io.IOException; 4 | import java.util.Date; 5 | import java.util.List; 6 | 7 | import com.aliyun.hitsdb.client.value.request.Point; 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import com.aliyun.hitsdb.client.HiTSDB; 13 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 14 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 15 | import com.aliyun.hitsdb.client.value.response.TagResult; 16 | 17 | public class TestHiTSDBClientDumpMeta { 18 | HiTSDB tsdb; 19 | 20 | 21 | String metricPrefix = "nongfu_spring"; 22 | 23 | String tagkey = "address"; 24 | 25 | String tagValuePrefix = "china"; 26 | 27 | @Before 28 | public void init() throws HttpClientInitException { 29 | tsdb = HiTSDBClientFactory.connect("localhost", 3242); 30 | 31 | for(int i = 0;i < 10;i ++){ 32 | for(int j = 0; j < 10;j ++){ 33 | Point point = Point.metric(metricPrefix + i) 34 | .tag(tagkey,tagValuePrefix + i + j) 35 | .value(new Date(System.currentTimeMillis()),Math.random()).build(); 36 | tsdb.put(point); 37 | } 38 | } 39 | 40 | try { 41 | Thread.sleep(10 * 1000); 42 | } catch (InterruptedException e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | 47 | @After 48 | public void after() { 49 | try { 50 | tsdb.close(); 51 | } catch (IOException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | 56 | 57 | 58 | @Test 59 | public void testDumpMeta() { 60 | List dumpMeta = tsdb.dumpMeta(tagkey, tagValuePrefix, 150); 61 | assert dumpMeta.size() == 100; 62 | System.out.println("查询结果:" + dumpMeta.size() + "\t" + dumpMeta); 63 | } 64 | 65 | 66 | @Test 67 | public void testMetricDumpMeta(){ 68 | for(int i = 0;i < 10;i ++){ 69 | List dumpMeta = tsdb.dumpMeta(metricPrefix + i,tagkey, tagValuePrefix, 20); 70 | assert dumpMeta.size() == 10; 71 | System.out.println("查询结果:" + dumpMeta.size() + "\t" + dumpMeta); 72 | } 73 | } 74 | 75 | 76 | @Test 77 | public void testMetricDumpMeta2(){ 78 | for(int i = 0;i < 10;i ++){ 79 | List dumpMeta = tsdb.dumpMeta(metricPrefix + i,tagkey, "", 20); 80 | assert dumpMeta.size() == 10; 81 | System.out.println("查询结果:" + dumpMeta.size() + "\t" + dumpMeta); 82 | } 83 | } 84 | 85 | @Test 86 | public void testDumpMetric() { 87 | List dumpMeta = tsdb.dumpMetric(tagkey, tagValuePrefix, 100); 88 | assert dumpMeta.size() == 10; 89 | System.out.println("查询结果:" + dumpMeta.size() + "\t" + dumpMeta); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientLast.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.io.IOException; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import com.aliyun.hitsdb.client.HiTSDB; 13 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 14 | import com.aliyun.hitsdb.client.HiTSDBConfig; 15 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 16 | import com.aliyun.hitsdb.client.value.request.Query; 17 | import com.aliyun.hitsdb.client.value.request.SubQuery; 18 | import com.aliyun.hitsdb.client.value.response.QueryResult; 19 | import com.aliyun.hitsdb.client.value.type.Aggregator; 20 | 21 | public class TestHiTSDBClientLast { 22 | HiTSDB tsdb; 23 | 24 | @Before 25 | public void init() throws HttpClientInitException { 26 | HiTSDBConfig config = HiTSDBConfig 27 | .address("127.0.0.1", 8242) 28 | .config(); 29 | 30 | tsdb = HiTSDBClientFactory.connect(config); 31 | } 32 | 33 | @After 34 | public void after() { 35 | try { 36 | tsdb.close(); 37 | } catch (IOException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | 42 | @Test 43 | public void testLast() { 44 | Date now = new Date(); 45 | Calendar calendar = Calendar.getInstance(); 46 | calendar.add(Calendar.DATE, -1); 47 | Date startTime = calendar.getTime(); 48 | 49 | Query query = Query.timeRange(startTime, now).sub( 50 | SubQuery.metric("test1") 51 | .aggregator(Aggregator.AVG) 52 | .tag("tagk1", "tagv1").build() 53 | ).build(); 54 | 55 | List result = tsdb.last(query, 10); 56 | System.out.println(result.get(0).getDps().size()); 57 | System.out.println("查询结果:" + result); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientPutOnErrorURL.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | import com.aliyun.hitsdb.client.value.request.Point; 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Ignore; 8 | import org.junit.Test; 9 | 10 | import java.io.IOException; 11 | 12 | @Ignore 13 | public class TestHiTSDBClientPutOnErrorURL { 14 | 15 | HiTSDB tsdb; 16 | 17 | @Before 18 | public void init() throws HttpClientInitException { 19 | // 错误的地址 20 | // hitsdb.com 21 | // www.baidu.com 22 | // HiTSDBConfig config = HiTSDBConfig.address("hitsdb.wwww", 8242).config(); 23 | HiTSDBConfig config = HiTSDBConfig.address("www.baidu.com", 8242) 24 | .httpConnectTimeout(2) 25 | .config(); 26 | tsdb = HiTSDBClientFactory.connect(config); 27 | System.out.println("------------"); 28 | } 29 | 30 | @After 31 | public void after() { 32 | try { 33 | System.out.println("将要关闭"); 34 | tsdb.close(); 35 | } catch (IOException e) { 36 | e.printStackTrace(); 37 | } 38 | } 39 | 40 | @Test 41 | public void testPutData() throws InterruptedException { 42 | for (int i = 0; i < 1; i++) { 43 | Point point = Point.metric("test-test-test").tag("test", "test") 44 | .timestamp(System.currentTimeMillis()) 45 | .value(System.currentTimeMillis()) 46 | .build(); 47 | tsdb.put(point); 48 | } 49 | 50 | } 51 | } -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientSuggest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import com.aliyun.hitsdb.client.value.request.Point; 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.aliyun.hitsdb.client.HiTSDB; 12 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 13 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 14 | import com.aliyun.hitsdb.client.value.type.Suggest; 15 | 16 | public class TestHiTSDBClientSuggest { 17 | HiTSDB tsdb; 18 | 19 | @Before 20 | public void init() throws HttpClientInitException { 21 | tsdb = HiTSDBClientFactory.connect("127.0.0.1", 8242); 22 | tsdb.putSync(Point.metric("hel-metric-test") 23 | .tag("hel-tagk-test","hel-tagv-test") 24 | .value(System.currentTimeMillis(),Math.random()) 25 | .build()); 26 | } 27 | 28 | @After 29 | public void after() { 30 | try { 31 | tsdb.close(); 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | 37 | @Test 38 | public void testSuggestMetric() { 39 | List metrics = tsdb.suggest(Suggest.Metrics, "hel", 10); 40 | System.out.println("查询结果:" + metrics); 41 | } 42 | 43 | @Test 44 | public void testSuggestTagK() { 45 | List metrics = tsdb.suggest(Suggest.Tagk, "hel", 10); 46 | System.out.println("查询结果:" + metrics); 47 | } 48 | 49 | @Test 50 | public void testSuggestTagV() { 51 | List metrics = tsdb.suggest(Suggest.Tagv, "hel", 10); 52 | System.out.println("查询结果:" + metrics); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientSyncPut.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.concurrent.atomic.AtomicInteger; 9 | 10 | import com.aliyun.hitsdb.client.value.response.batch.DetailsResult; 11 | import org.junit.After; 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | 15 | import com.aliyun.hitsdb.client.callback.BatchPutCallback; 16 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 17 | import com.aliyun.hitsdb.client.value.Result; 18 | import com.aliyun.hitsdb.client.value.request.Point; 19 | 20 | public class TestHiTSDBClientSyncPut { 21 | 22 | HiTSDB tsdb; 23 | 24 | @Before 25 | public void init() throws HttpClientInitException { 26 | BatchPutCallback pcb = new BatchPutCallback() { 27 | 28 | final AtomicInteger num = new AtomicInteger(); 29 | 30 | @Override 31 | public void failed(String address, List points, Exception ex) { 32 | System.err.println("业务回调出错!" + points.size() + " error!"); 33 | ex.printStackTrace(); 34 | } 35 | 36 | @Override 37 | public void response(String address, List input, Result output) { 38 | int count = num.addAndGet(input.size()); 39 | System.out.println("已处理" + count + "个点"); 40 | } 41 | 42 | }; 43 | 44 | HiTSDBConfig config = HiTSDBConfig.address("127.0.0.1", 3242) 45 | .listenBatchPut(pcb).httpConnectTimeout(90).config(); 46 | tsdb = HiTSDBClientFactory.connect(config); 47 | } 48 | 49 | @After 50 | public void after() { 51 | try { 52 | System.out.println("将要关闭"); 53 | tsdb.close(); 54 | } catch (IOException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | 59 | @Test 60 | public void testPutData() { 61 | int t = (int) (1508742134297L / 1000); // 1508742134 62 | int t1 = t - 1; 63 | Point point = Point.metric("test-test-test").tag("level", "500").timestamp(t1).value(123.4567).build(); 64 | Result result = tsdb.putSync(point); 65 | System.out.println(result); 66 | } 67 | 68 | @Test 69 | public void testPutPoints() { 70 | int t = (int) (1508742134297L / 1000); // 1508742134 71 | int t1 = t - 10; 72 | List points = new ArrayList(); 73 | Map tags = new HashMap(); 74 | tags.put("host", "server1"); 75 | for(long i=0; i<10; i++) { 76 | Point point = new Point(); 77 | point.setMetric("cpu"); 78 | point.setValue(10); 79 | point.setTimestamp(t1 + i); 80 | point.setTags(tags); 81 | points.add(point); 82 | } 83 | Result result = tsdb.putSync(points, DetailsResult.class); 84 | System.out.println(result); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientTTL.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.io.IOException; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.aliyun.hitsdb.client.HiTSDB; 10 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 11 | import com.aliyun.hitsdb.client.HiTSDBConfig; 12 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 13 | 14 | public class TestHiTSDBClientTTL { 15 | 16 | HiTSDB tsdb; 17 | 18 | @Before 19 | public void init() throws HttpClientInitException { 20 | HiTSDBConfig config = HiTSDBConfig.address("127.0.0.1", 8242).config(); 21 | tsdb = HiTSDBClientFactory.connect(config); 22 | } 23 | 24 | @After 25 | public void after() { 26 | try { 27 | tsdb.close(); 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | @Test 34 | public void testPostTTL() { 35 | // 10年 36 | int time = 2678400; 37 | tsdb.ttl(time); 38 | 39 | System.out.println("结束"); 40 | try { 41 | Thread.sleep(100000000); 42 | } catch (InterruptedException e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | 47 | @Test 48 | public void testGetTTL() { 49 | int ttl = tsdb.ttl(); 50 | System.out.println(ttl); 51 | 52 | try { 53 | Thread.sleep(100000000); 54 | } catch (InterruptedException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientTableOp.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | import com.aliyun.hitsdb.client.value.request.LastPointQuery; 5 | import com.aliyun.hitsdb.client.value.request.LastPointSubQuery; 6 | import com.aliyun.hitsdb.client.value.response.LastDataValue; 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Ignore; 10 | import org.junit.Test; 11 | 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | import java.util.HashMap; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | @Ignore 19 | public class TestHiTSDBClientTableOp { 20 | HiTSDB tsdb; 21 | 22 | @Before 23 | public void init() throws HttpClientInitException { 24 | HiTSDBConfig config = HiTSDBConfig 25 | .address("127.0.0.1", 8242) 26 | .config(); 27 | 28 | tsdb = HiTSDBClientFactory.connect(config); 29 | } 30 | 31 | @After 32 | public void after() { 33 | try { 34 | tsdb.close(); 35 | } catch (IOException e) { 36 | e.printStackTrace(); 37 | } 38 | } 39 | 40 | @Test 41 | public void testTruncate() { 42 | System.out.println(tsdb.truncate()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestHiTSDBClientVersion.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | import com.aliyun.hitsdb.client.value.request.Query; 5 | import com.aliyun.hitsdb.client.value.request.SubQuery; 6 | import com.aliyun.hitsdb.client.value.response.QueryResult; 7 | import com.aliyun.hitsdb.client.value.type.Aggregator; 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import java.io.IOException; 13 | import java.util.Calendar; 14 | import java.util.Date; 15 | import java.util.List; 16 | 17 | public class TestHiTSDBClientVersion { 18 | HiTSDB tsdb; 19 | 20 | @Before 21 | public void init() throws HttpClientInitException { 22 | HiTSDBConfig config = HiTSDBConfig 23 | .address("127.0.0.1", 8242) 24 | .config(); 25 | 26 | tsdb = HiTSDBClientFactory.connect(config); 27 | } 28 | 29 | @After 30 | public void after() { 31 | try { 32 | tsdb.close(); 33 | } catch (IOException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | 38 | @Test 39 | public void testVersion() { 40 | System.out.println(tsdb.version()); 41 | } 42 | 43 | @Test 44 | public void testVersionInfo() { 45 | System.out.println(tsdb.getVersionInfo()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestIotClient.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 4 | import com.aliyun.hitsdb.client.value.request.*; 5 | import com.aliyun.hitsdb.client.value.response.TagsAddResult; 6 | import com.aliyun.hitsdb.client.value.response.TagsShowResult; 7 | import org.junit.After; 8 | import org.junit.Assert; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import java.io.IOException; 13 | import java.util.List; 14 | 15 | /* 16 | * This test can only run in super-tag mode 17 | */ 18 | public class TestIotClient { 19 | TSDB tsdb; 20 | 21 | @Before 22 | public void init() throws HttpClientInitException { 23 | TSDBConfig config = TSDBConfig 24 | .address("localhost", 8242) 25 | .config(); 26 | tsdb = IotClientFactory.connect(config); 27 | } 28 | 29 | @After 30 | public void after() { 31 | try { 32 | tsdb.close(); 33 | } catch (IOException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | 38 | @Test 39 | public void testTagsOperation() { 40 | long startTime = System.currentTimeMillis(); 41 | double value = Math.random(); 42 | MultiFieldPoint point = MultiFieldPoint.metric("test") 43 | .tag("_id","1") 44 | .field("field1",value) 45 | .timestamp(startTime) 46 | .build(); 47 | tsdb.multiFieldPutSync(point); 48 | 49 | TagsAddResult tagsAddResult = ((IotClient)tsdb).tagsAdd(new TagsAddInfo.Builder("test").id("1").tag("tagk1", "tagv1").build()); 50 | Assert.assertEquals(1, tagsAddResult.getSuccess()); 51 | 52 | List tagsShowResult = ((IotClient)tsdb).tagsShow(new TagsShowInfo("test","1")); 53 | Assert.assertEquals(1, tagsShowResult.size()); 54 | 55 | TagsShowResult result = tagsShowResult.get(0); 56 | Assert.assertEquals("tagv1", result.getTags().get("tagk1")); 57 | 58 | ((IotClient)tsdb).tagsRemove(new TagsRemoveInfo.Builder("test","1").tag("tagk1", "tagv1").build()); 59 | tagsShowResult = ((IotClient)tsdb).tagsShow(new TagsShowInfo("test","1")); 60 | Assert.assertEquals(0, tagsShowResult.size()); 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestLastResultTimestampReverse.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.value.response.LastDataValue; 4 | import com.aliyun.hitsdb.client.value.response.MultiFieldQueryLastResult; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.ArrayList; 9 | import java.util.LinkedHashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | * @author cuiyuan 15 | * @date 2021/3/24 12:21 下午 16 | */ 17 | public class TestLastResultTimestampReverse { 18 | 19 | @Test 20 | public void testMultiValueTimestampReverse() { 21 | MultiFieldQueryLastResult multiFieldQueryLastResult = new MultiFieldQueryLastResult(); 22 | 23 | List t1 = new ArrayList(); 24 | List t2 = new ArrayList(); 25 | List t3 = new ArrayList(); 26 | t1.add(1L); 27 | t2.add(2L); 28 | t3.add(3L); 29 | 30 | List> values = new ArrayList>(); 31 | values.add(t1); 32 | values.add(t2); 33 | values.add(t3); 34 | 35 | multiFieldQueryLastResult.setValues(values); 36 | 37 | long timestamp = 1; 38 | for (List value :multiFieldQueryLastResult.getValues()) { 39 | Assert.assertEquals(timestamp, value.get(0)); 40 | timestamp++; 41 | } 42 | 43 | TSDBClient.reverseMultiValueTimestamp(multiFieldQueryLastResult); 44 | 45 | Assert.assertEquals(3, multiFieldQueryLastResult.getValues().size()); 46 | timestamp = 3; 47 | for (List value :multiFieldQueryLastResult.getValues()) { 48 | Assert.assertEquals(timestamp, value.get(0)); 49 | timestamp--; 50 | } 51 | } 52 | 53 | @Test 54 | public void testSingleValueTimestampReverse() { 55 | LastDataValue lastDataValue = new LastDataValue(); 56 | LinkedHashMap dps = new LinkedHashMap(); 57 | dps.put(1L, 1L); 58 | dps.put(2L, 2L); 59 | dps.put(3L, 3L); 60 | lastDataValue.setDps(dps); 61 | long timestamp = 1; 62 | for (Map.Entry longObjectEntry : lastDataValue.getDps().entrySet()) { 63 | Assert.assertEquals(timestamp, longObjectEntry.getKey().longValue()); 64 | Assert.assertEquals(timestamp, longObjectEntry.getValue()); 65 | timestamp++; 66 | } 67 | 68 | TSDBClient.reverseSingleValueTimestamp(lastDataValue); 69 | 70 | //after reverse 71 | Assert.assertEquals(3, lastDataValue.getDps().entrySet().size()); 72 | timestamp = 3; 73 | for (Map.Entry longObjectEntry : lastDataValue.getDps().entrySet()) { 74 | Assert.assertEquals(timestamp, longObjectEntry.getKey().longValue()); 75 | Assert.assertEquals(timestamp, longObjectEntry.getValue()); 76 | timestamp--; 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestLindormTSDBClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.value.request.MultiFieldPoint; 4 | import org.junit.Test; 5 | 6 | public class TestLindormTSDBClientFactory { 7 | 8 | @Test 9 | public void testConnectWithHostAndPort() { 10 | TSDB tsdb = LindormTSDBClientFactory.connect("127.0.0.1", 3002); 11 | MultiFieldPoint multiFieldPoint = generateMultiFieldPoint(); 12 | tsdb.multiFieldPutSync(multiFieldPoint); 13 | } 14 | 15 | @Test 16 | public void testConnectWithConfig() { 17 | TSDBConfig config = TSDBConfig.address("127.0.0.1", 3002).config(); 18 | TSDB tsdb = TSDBClientFactory.connect(config); 19 | 20 | MultiFieldPoint multiFieldPoint = generateMultiFieldPoint(); 21 | tsdb.multiFieldPutSync(multiFieldPoint); 22 | } 23 | 24 | public MultiFieldPoint generateMultiFieldPoint() { 25 | final String metric = "wind"; 26 | final String field = "speed"; 27 | long timestamp = 1537170208; 28 | MultiFieldPoint multiFieldPoint = MultiFieldPoint.metric(metric) 29 | .field(field, 20) 30 | .timestamp(timestamp) 31 | .build(); 32 | return multiFieldPoint; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestLinkedHashMapUtils.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map.Entry; 5 | 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.aliyun.hitsdb.client.util.LinkedHashMapUtils; 10 | 11 | public class TestLinkedHashMapUtils { 12 | 13 | LinkedHashMap linkedHashMap; 14 | 15 | @Before 16 | public void init() { 17 | linkedHashMap = new LinkedHashMap(); 18 | linkedHashMap.put("one", 1); 19 | // linkedHashMap.put("two", 2); 20 | // linkedHashMap.put("three", 3); 21 | // linkedHashMap.put("four", 4); 22 | } 23 | 24 | @Test 25 | public void testTraverse(){ 26 | for(Entry entry:linkedHashMap.entrySet()){ 27 | System.out.println( 28 | entry.getKey() + ":" + entry.getValue() 29 | ); 30 | } 31 | } 32 | 33 | @Test 34 | public void testGetTail() { 35 | Entry entry = LinkedHashMapUtils.getTail(linkedHashMap); 36 | System.out.println( 37 | entry.getKey() + ":" + entry.getValue() 38 | ); 39 | } 40 | 41 | @Test 42 | public void testGetTailBefore() { 43 | Entry tailEntry = LinkedHashMapUtils.getTail(linkedHashMap); 44 | Entry entry = LinkedHashMapUtils.getBefore(tailEntry); 45 | 46 | if(entry != null){ 47 | System.out.println( 48 | entry.getKey() + ":" + entry.getValue() 49 | ); 50 | } else { 51 | System.out.println(entry); 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/TestTSDBSQL.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client; 2 | 3 | import com.aliyun.hitsdb.client.exception.http.HttpUnknowStatusException; 4 | import com.aliyun.hitsdb.client.value.request.Point; 5 | import com.aliyun.hitsdb.client.value.response.SQLResult; 6 | import org.junit.After; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.io.IOException; 11 | 12 | import static org.junit.Assert.assertNotNull; 13 | 14 | public class TestTSDBSQL { 15 | 16 | private TSDB tsdb; 17 | 18 | long startTime; 19 | 20 | @Before 21 | public void setup() { 22 | TSDBConfig config = TSDBConfig.address("127.0.0.1", 8242) 23 | .httpConnectTimeout(90) 24 | // 批次写入时每次提交数据量 25 | .batchPutSize(500) 26 | // 单值异步写入线程数 27 | .batchPutConsumerThreadCount(1) 28 | // 多值异步写入缓存队列长度 29 | .multiFieldBatchPutBufferSize(10000) 30 | // 多值异步写入线程数 31 | .multiFieldBatchPutConsumerThreadCount(1) 32 | .config(); 33 | 34 | // 特别注意,TSDB只需初始化一次即可 35 | tsdb = TSDBClientFactory.connect(config); 36 | 37 | startTime = System.currentTimeMillis() - 2000; 38 | tsdb.putSync(Point.metric("test-test").tag("tk1", "tv1").value(startTime, Math.random()).build()); 39 | } 40 | 41 | @After 42 | public void tearDown() throws IOException { 43 | tsdb.close(); 44 | } 45 | 46 | 47 | @Test 48 | public void test_show_tables() throws IOException { 49 | SQLResult sqlResult = tsdb.queryBySQL("SHOW TABLES FROM TSDB"); 50 | assertNotNull(sqlResult); 51 | } 52 | 53 | @Test(expected = HttpUnknowStatusException.class) 54 | public void test_show_tables_with_exception() throws IOException { 55 | SQLResult sqlResult = tsdb.queryBySQL("SHOW TABLES FROM TSD"); 56 | assertNotNull(sqlResult); 57 | } 58 | 59 | @Test 60 | public void test_describe_table() throws IOException { 61 | SQLResult sqlResult = tsdb.queryBySQL("DESCRIBE TSDB.`test-test`"); 62 | assertNotNull(sqlResult); 63 | } 64 | 65 | @Test(expected = HttpUnknowStatusException.class) 66 | public void test_describe_table_with_exception() throws IOException { 67 | SQLResult sqlResult = tsdb.queryBySQL("DESCRIBE TSDB.`test`"); 68 | assertNotNull(sqlResult); 69 | } 70 | 71 | 72 | @Test 73 | public void test_query_by_sql() throws IOException { 74 | String sql = "SELECT `timestamp`, `value` from tsdb.`test-test` where `timestamp` >= " + (startTime - 8 * 60 * 60 * 1000); 75 | SQLResult sqlResult = tsdb.queryBySQL(sql); 76 | assertNotNull(sqlResult); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/balance/TestBalClient.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.balance; 2 | 3 | import com.aliyun.hitsdb.client.BalHiTSDBClient; 4 | import com.aliyun.hitsdb.client.HiTSDB; 5 | import com.aliyun.hitsdb.client.value.request.Point; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | 10 | /** 11 | * Created By jianhong.hjh 12 | * Date: 2018/10/13 13 | */ 14 | public class TestBalClient { 15 | 16 | public static void main(String[] args) throws IOException { 17 | HiTSDB client = new BalHiTSDBClient(new File("conf/tsdb.conf")); 18 | try { 19 | for(int i = 0;i < 10000;i ++){ 20 | client.putSync(Point.metric("hh.test") 21 | .tag("hh","hh") 22 | .value(System.currentTimeMillis(),1234) 23 | .build()); 24 | Thread.sleep(1000); 25 | } 26 | } catch (Exception e){ 27 | e.printStackTrace(); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/balance/TestFileWatchManager.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.balance; 2 | 3 | import com.aliyun.hitsdb.client.util.FileWatcher; 4 | import com.aliyun.hitsdb.client.util.WatchManager; 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import java.io.File; 10 | 11 | /** 12 | * Created By jianhong.hjh 13 | * Date: 2018/10/13 14 | */ 15 | public class TestFileWatchManager { 16 | 17 | WatchManager watchManager; 18 | 19 | @Before 20 | public void before() { 21 | watchManager = new WatchManager(); 22 | watchManager.setIntervalSeconds(2); 23 | watchManager.start(); 24 | } 25 | 26 | @Test 27 | public void test() { 28 | watchManager.watchFile(new File("conf/tsdb.conf"), new FileWatcher() { 29 | @Override 30 | public void fileModified(File file) { 31 | System.out.println(file.getName()); 32 | } 33 | }); 34 | while (true) { 35 | 36 | } 37 | } 38 | 39 | 40 | @After 41 | public void after() { 42 | watchManager.stop(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/balance/TestHealthManager.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.balance; 2 | 3 | import com.aliyun.hitsdb.client.util.HealthManager; 4 | import com.aliyun.hitsdb.client.util.HealthWatcher; 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | /** 10 | * Created By jianhong.hjh 11 | * Date: 2018/10/13 12 | */ 13 | public class TestHealthManager { 14 | 15 | HealthManager healthManager; 16 | 17 | 18 | 19 | @Before 20 | public void before() { 21 | healthManager = new HealthManager(); 22 | healthManager.setIntervalSeconds(2); 23 | healthManager.start(); 24 | } 25 | 26 | 27 | @Test 28 | public void test() { 29 | String[] address = "127.0.0.1:8242".split(","); 30 | for (String host : address) { 31 | healthManager.watch(host, new HealthWatcher() { 32 | @Override 33 | public void health(String host, boolean health) { 34 | System.out.println(host + "\t" + health); 35 | } 36 | }); 37 | } 38 | while (true){ 39 | 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | 47 | @After 48 | public void after(){ 49 | healthManager.stop(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/balance/TestPoint.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.balance; 2 | 3 | import com.aliyun.hitsdb.client.value.request.Point; 4 | import org.junit.Test; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created By jianhong.hjh 11 | * Date: 2018/10/27 12 | */ 13 | public class TestPoint { 14 | 15 | @Test 16 | public void testEmpty() { 17 | Map tags = new HashMap(); 18 | tags.put("hh",""); 19 | tags.put("test",""); 20 | Point point = new Point.MetricBuilder("test") 21 | .tag(tags) 22 | .value(System.currentTimeMillis(),Math.random()) 23 | .build(); 24 | } 25 | 26 | @Test 27 | public void testNull() { 28 | Map tags = new HashMap(); 29 | tags.put("kk",null); 30 | tags.put("test",""); 31 | Point point = new Point.MetricBuilder("test") 32 | .tag(tags) 33 | .value(System.currentTimeMillis(),Math.random()) 34 | .build(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/callback/TestBatchPutSummaryCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.aliyun.hitsdb.client.HiTSDB; 10 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 11 | import com.aliyun.hitsdb.client.HiTSDBConfig; 12 | import com.aliyun.hitsdb.client.callback.BatchPutSummaryCallback; 13 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 14 | import com.aliyun.hitsdb.client.value.request.Point; 15 | import com.aliyun.hitsdb.client.value.response.batch.SummaryResult; 16 | 17 | public class TestBatchPutSummaryCallback { 18 | HiTSDB tsdb; 19 | 20 | @Before 21 | public void init() throws HttpClientInitException, IOException { 22 | System.out.println("按下任意键,开始运行..."); 23 | while (true) { 24 | int read = System.in.read(); 25 | if (read != 0) { 26 | break; 27 | } 28 | } 29 | System.out.println("开始运行"); 30 | 31 | BatchPutSummaryCallback pcb = new BatchPutSummaryCallback() { 32 | 33 | @Override 34 | public void response(String address,List input, SummaryResult result) { 35 | int success = result.getSuccess(); 36 | int failed = result.getFailed(); 37 | System.out.println(success + "," + failed); 38 | } 39 | 40 | }; 41 | 42 | HiTSDBConfig config = HiTSDBConfig 43 | .address("test.host", 8242) 44 | .httpConnectionPool(100) 45 | .listenBatchPut(pcb) 46 | .config(); 47 | 48 | tsdb = HiTSDBClientFactory.connect(config); 49 | } 50 | 51 | @Test 52 | public void test() throws InterruptedException { 53 | for(int i = 0;i<1000;i++) { 54 | Point point = createPoint(i%4,1.123); 55 | tsdb.put(point); 56 | Thread.sleep(1000); 57 | } 58 | } 59 | 60 | public Point createPoint(int tag, double value) { 61 | int t = (int) (System.currentTimeMillis() / 1000); 62 | return Point.metric("test-performance").tag("tag", String.valueOf(tag)).value(t, value).build(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/callback/TestNoLogicCallback.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.callback; 2 | 3 | import java.io.IOException; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.aliyun.hitsdb.client.HiTSDB; 10 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 11 | import com.aliyun.hitsdb.client.HiTSDBConfig; 12 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 13 | import com.aliyun.hitsdb.client.util.UI; 14 | import com.aliyun.hitsdb.client.value.request.Point; 15 | 16 | public class TestNoLogicCallback { 17 | HiTSDB tsdb; 18 | 19 | @Before 20 | public void init() throws HttpClientInitException { 21 | UI.pauseStart(); 22 | HiTSDBConfig config = HiTSDBConfig 23 | .address("test.host", 3242) 24 | .httpConnectionPool(64) 25 | .httpConnectTimeout(90) 26 | // .batchPutRetryCount() 27 | .config(); 28 | 29 | tsdb = HiTSDBClientFactory.connect(config); 30 | } 31 | 32 | @Test 33 | public void test() throws InterruptedException { 34 | for (int i = 0; i < 1; i++) { 35 | Point point = createPoint(i % 4, 1.123); 36 | tsdb.put(point); 37 | Thread.sleep(1000*2); 38 | } 39 | } 40 | 41 | @After 42 | public void end() throws IOException { 43 | tsdb.close(); 44 | } 45 | 46 | public Point createPoint(int tag, double value) { 47 | int t = (int) (System.currentTimeMillis() / 1000); 48 | return Point.metric("test-performance").tag("tag", String.valueOf(tag)).value(t, value).build(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/compress/TestHiTSDBClientQuery.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.compress; 2 | 3 | import java.io.IOException; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import com.aliyun.hitsdb.client.HiTSDB; 13 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 14 | import com.aliyun.hitsdb.client.HiTSDBConfig; 15 | import com.aliyun.hitsdb.client.callback.QueryCallback; 16 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 17 | import com.aliyun.hitsdb.client.value.request.Query; 18 | import com.aliyun.hitsdb.client.value.request.SubQuery; 19 | import com.aliyun.hitsdb.client.value.response.QueryResult; 20 | import com.aliyun.hitsdb.client.value.type.Aggregator; 21 | 22 | public class TestHiTSDBClientQuery { 23 | 24 | HiTSDB tsdb; 25 | 26 | @Before 27 | public void init() throws HttpClientInitException { 28 | HiTSDBConfig config = HiTSDBConfig 29 | .address("127.0.0.1", 8242) 30 | .httpCompress(true) 31 | .config(); 32 | 33 | tsdb = HiTSDBClientFactory.connect(config); 34 | } 35 | 36 | @After 37 | public void after() { 38 | try { 39 | tsdb.close(); 40 | } catch (IOException e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | 45 | @Test 46 | public void testQuery() { 47 | Date now = new Date(); 48 | Calendar calendar = Calendar.getInstance(); 49 | calendar.add(Calendar.DATE, -1); 50 | Date startTime = calendar.getTime(); 51 | 52 | Query query = Query.timeRange(startTime, now).sub( 53 | SubQuery.metric("test1") 54 | .aggregator(Aggregator.AVG) 55 | .tag("tagk1", "tagv1").build() 56 | ).build(); 57 | 58 | List result = tsdb.query(query); 59 | System.out.println("查询结果:" + result); 60 | } 61 | 62 | @Test 63 | public void testQueryCallback() { 64 | 65 | Query query = Query.timeRange(1501655667, 1501742067) 66 | .sub(SubQuery.metric("mem.usage.GB").aggregator(Aggregator.AVG).tag("site", "et2").tag("appname", "hitsdb").build()) 67 | .build(); 68 | 69 | QueryCallback cb = new QueryCallback() { 70 | 71 | @Override 72 | public void response(String address, Query input, List result) { 73 | System.out.println("查询参数:" + input); 74 | System.out.println("返回结果:" + result); 75 | } 76 | 77 | // 在需要处理异常的时候,重写failed方法 78 | @Override 79 | public void failed(String address, Query request, Exception ex) { 80 | super.failed(address, request, ex); 81 | } 82 | 83 | }; 84 | 85 | tsdb.query(query, cb); 86 | 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/configuration/TestConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.configuration; 2 | 3 | import com.aliyun.hitsdb.client.TSDBConfig; 4 | import com.aliyun.hitsdb.client.callback.BatchPutCallback; 5 | import com.aliyun.hitsdb.client.value.Result; 6 | import com.aliyun.hitsdb.client.value.request.Point; 7 | import org.junit.Test; 8 | 9 | import java.util.List; 10 | 11 | public class TestConfiguration { 12 | 13 | @Test 14 | public void test() { 15 | TSDBConfig 16 | .address("127.0.0.1", 8242) // 地址,第一个参数可以是域名,IP,或者VIPServer的域名。 17 | .asyncPut(true) 18 | .readonly(false) 19 | .batchPutBufferSize(20000) // 客户端缓冲队列长度,默认为10000。 20 | .batchPutConsumerThreadCount(2) // 缓冲队列消费线程数,默认为1。 21 | .batchPutSize(800) // 每次批次提交给客户端点的个数,默认为500。 22 | .batchPutTimeLimit(100) // 每次等待最大时间限制,单位为ms,默认为200。 23 | .httpConnectionPool(256) // 网络连接池大小,默认为10。 24 | .httpConnectTimeout(10) // HTTP等待时间,单位为s,默认为90。 25 | .httpSocketTimeout(10) // Socket 超时时间,单位为s,默认为90。 26 | .httpConnectionRequestTimeout(10) // 获取 HTTP 连接超时时间,单位为s,默认为90。 27 | .ioThreadCount(2) // IO 线程数,默认为1。 28 | .batchPutRetryCount(2) // 异常重试次数。 29 | .putRequestLimit(100) // IO请求队列数,默认等于连接池数。不建议使用。 30 | .closePutRequestLimit() // 不限制请求队列数,若关闭可能导致OOM,不建议使用。 31 | .listenBatchPut(new BatchPutCallback() { // 批量Put回调接口 32 | @Override 33 | public void response(String address, List input, Result output) { 34 | } 35 | }).config(); // 构造对象 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/connection/TestConnectionLiveTime.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.connection; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.concurrent.atomic.AtomicLong; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.aliyun.hitsdb.client.HiTSDB; 12 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 13 | import com.aliyun.hitsdb.client.HiTSDBConfig; 14 | import com.aliyun.hitsdb.client.callback.BatchPutCallback; 15 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 16 | import com.aliyun.hitsdb.client.util.UI; 17 | import com.aliyun.hitsdb.client.value.Result; 18 | import com.aliyun.hitsdb.client.value.request.Point; 19 | 20 | public class TestConnectionLiveTime { 21 | 22 | HiTSDB tsdb; 23 | 24 | @Before 25 | public void init() throws HttpClientInitException { 26 | UI.pauseStart(); 27 | HiTSDBConfig config = HiTSDBConfig 28 | .address("127.0.0.1", 8242) 29 | .httpConnectionPool(1) 30 | .batchPutSize(100) 31 | .listenBatchPut(new BatchPutCallback() { 32 | final AtomicLong num = new AtomicLong(); 33 | 34 | @Override 35 | public void response(String address, List input, Result output) { 36 | long afterNum = num.addAndGet(input.size()); 37 | System.out.println("成功处理" + input.size() + ",已处理" + afterNum); 38 | } 39 | 40 | @Override 41 | public void failed(String address, List input, Exception ex) { 42 | ex.printStackTrace(); 43 | long afterNum = num.addAndGet(input.size()); 44 | System.out.println("失败处理" + input.size() + ",已处理" + afterNum); 45 | } 46 | }) 47 | .config(); 48 | 49 | tsdb = HiTSDBClientFactory.connect(config); 50 | } 51 | 52 | @Test 53 | public void test() throws InterruptedException { 54 | for (int i = 0; i < 1000000; i++) { 55 | Point point = createPoint(i % 4); 56 | tsdb.put(point); 57 | Thread.sleep(50); 58 | } 59 | } 60 | 61 | @After 62 | public void end() throws IOException { 63 | tsdb.close(); 64 | } 65 | 66 | public Point createPoint(int tag) { 67 | int t = (int) (System.currentTimeMillis() / 1000); 68 | double random = Math.random(); 69 | double value = Math.round(random*1000)/1000.0; 70 | return Point.metric("test-performance").tag("tag", String.valueOf(tag)).value(t, value).build(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/connection/TestKeepaliveConnection.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.connection; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.concurrent.atomic.AtomicLong; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.aliyun.hitsdb.client.HiTSDB; 12 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 13 | import com.aliyun.hitsdb.client.HiTSDBConfig; 14 | import com.aliyun.hitsdb.client.callback.BatchPutCallback; 15 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 16 | import com.aliyun.hitsdb.client.util.UI; 17 | import com.aliyun.hitsdb.client.value.Result; 18 | import com.aliyun.hitsdb.client.value.request.Point; 19 | 20 | public class TestKeepaliveConnection { 21 | HiTSDB tsdb; 22 | 23 | 24 | @Before 25 | public void init() throws HttpClientInitException { 26 | UI.pauseStart(); 27 | 28 | HiTSDBConfig config = HiTSDBConfig 29 | .address(".address(", 8242) 30 | .httpConnectionPool(1) 31 | .httpConnectTimeout(200) 32 | .batchPutSize(100).listenBatchPut(new BatchPutCallback() { 33 | final AtomicLong num = new AtomicLong(); 34 | 35 | @Override 36 | public void response(String address, List input, Result output) { 37 | long afterNum = num.addAndGet(input.size()); 38 | System.out.println("成功处理" + input.size() + ",已处理" + afterNum); 39 | } 40 | 41 | @Override 42 | public void failed(String address, List input, Exception ex) { 43 | ex.printStackTrace(); 44 | long afterNum = num.addAndGet(input.size()); 45 | System.out.println("失败处理" + input.size() + ",已处理" + afterNum); 46 | } 47 | }).config(); 48 | 49 | tsdb = HiTSDBClientFactory.connect(config); 50 | } 51 | 52 | @Test 53 | public void test() throws InterruptedException { 54 | for (int i = 0; i < 1000000; i++) { 55 | Point point = createPoint(i % 4); 56 | tsdb.put(point); 57 | } 58 | } 59 | 60 | @After 61 | public void end() throws IOException { 62 | tsdb.close(); 63 | } 64 | 65 | public Point createPoint(int tag) { 66 | int t = (int) (System.currentTimeMillis() / 1000); 67 | double random = Math.random(); 68 | double value = Math.round(random * 1000) / 1000.0; 69 | return Point.metric("test-performance").tag("tag", String.valueOf(tag)).value(t, value).build(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/example/TestRead.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.example; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import com.aliyun.hitsdb.client.HiTSDB; 7 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 8 | import com.aliyun.hitsdb.client.HiTSDBConfig; 9 | import com.aliyun.hitsdb.client.value.request.Query; 10 | import com.aliyun.hitsdb.client.value.request.SubQuery; 11 | import com.aliyun.hitsdb.client.value.response.QueryResult; 12 | import com.aliyun.hitsdb.client.value.type.Aggregator; 13 | 14 | public class TestRead { 15 | public static void main(String[] args) throws IOException { 16 | HiTSDBConfig config = HiTSDBConfig.address("example.hitsdb.com", 8242).config(); 17 | HiTSDB tsdb = HiTSDBClientFactory.connect(config); 18 | 19 | // 构造查询条件并查询数据。 20 | long now = System.currentTimeMillis(); 21 | 22 | // 查询一小时的数据 23 | Query query = Query.timeRange(now - 3600 * 1000, now) 24 | .sub(SubQuery.metric("test").aggregator(Aggregator.NONE).tag("V", "1.0").build()).build(); 25 | 26 | // 查询数据 27 | List result = tsdb.query(query); 28 | 29 | // 打印输出 30 | System.out.println(result); 31 | 32 | // 安全关闭客户端,以防数据丢失。 33 | tsdb.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/example/TestWrite.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.example; 2 | 3 | import java.io.IOException; 4 | 5 | import com.aliyun.hitsdb.client.HiTSDB; 6 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 7 | import com.aliyun.hitsdb.client.HiTSDBConfig; 8 | import com.aliyun.hitsdb.client.value.request.Point; 9 | 10 | public class TestWrite { 11 | public static void main(String[] args) throws InterruptedException, IOException { 12 | // example.hitsdb.com 13 | HiTSDBConfig config = HiTSDBConfig.address("example.hitsdb.com", 8242).config(); 14 | 15 | HiTSDB tsdb = HiTSDBClientFactory.connect(config); 16 | 17 | // 构造数据并写入HiTSDB 18 | for (int i = 0; i < 1; i++) { 19 | Point point = Point.metric("test").tag("V", "1.0").value(System.currentTimeMillis(), 123.4567).build(); 20 | Thread.sleep(1000); // 1秒提交1次 21 | tsdb.put(point); 22 | } 23 | 24 | // 安全关闭客户端,以防数据丢失。 25 | System.out.println("关闭"); 26 | tsdb.close(); 27 | } 28 | } -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/http/TestHiTSDBRequestTime.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.http; 2 | 3 | import java.io.IOException; 4 | import java.text.ParseException; 5 | import java.text.SimpleDateFormat; 6 | import java.util.List; 7 | import java.util.Random; 8 | import java.util.concurrent.atomic.AtomicInteger; 9 | 10 | import org.junit.After; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | 14 | import com.aliyun.hitsdb.client.HiTSDB; 15 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 16 | import com.aliyun.hitsdb.client.HiTSDBConfig; 17 | import com.aliyun.hitsdb.client.callback.BatchPutCallback; 18 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 19 | import com.aliyun.hitsdb.client.value.Result; 20 | import com.aliyun.hitsdb.client.value.request.Point; 21 | 22 | public class TestHiTSDBRequestTime { 23 | HiTSDB tsdb; 24 | 25 | @Before 26 | public void init() throws HttpClientInitException { 27 | BatchPutCallback pcb = new BatchPutCallback() { 28 | 29 | final AtomicInteger num = new AtomicInteger(); 30 | 31 | @Override 32 | public void failed(String address, List points, Exception ex) { 33 | System.err.println("业务回调出错!" + points.size() + " error!"); 34 | ex.printStackTrace(); 35 | } 36 | 37 | @Override 38 | public void response(String address, List input, Result output) { 39 | int count = num.addAndGet(input.size()); 40 | System.out.println("已处理" + count + "个点"); 41 | } 42 | 43 | }; 44 | 45 | HiTSDBConfig config = HiTSDBConfig.address("127.0.0.1", 8242) 46 | .listenBatchPut(pcb) 47 | .httpConnectTimeout(5000) 48 | .config(); 49 | tsdb = HiTSDBClientFactory.connect(config); 50 | } 51 | 52 | @Test 53 | public void testLitterDateBatchPutDataCallback() { 54 | Random random = new Random(); 55 | int time = getTime(); 56 | for (int i = 0; i < 4000; i++) { 57 | double nextDouble = random.nextDouble() * 100; 58 | Point point = Point.metric("test1").tag("tagk1", "tagv1").tag("tagk2", "tagv2").tag("tagk3", "tagv3") 59 | .timestamp(time + i).value(nextDouble).build(); 60 | tsdb.put(point); 61 | } 62 | } 63 | 64 | @After 65 | public void after() { 66 | try { 67 | tsdb.close(); 68 | } catch (IOException e) { 69 | e.printStackTrace(); 70 | } 71 | } 72 | 73 | private static int getTime() { 74 | int time; 75 | try { 76 | String strDate = "2017-08-16 13:14:15"; 77 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 78 | time = (int) (sdf.parse(strDate).getTime() / 1000); 79 | } catch (ParseException e) { 80 | e.printStackTrace(); 81 | time = 0; 82 | } 83 | return time; 84 | 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/keepalive/TestKeepalive.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.keepalive; 2 | 3 | import java.io.IOException; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.aliyun.hitsdb.client.HiTSDB; 10 | import com.aliyun.hitsdb.client.HiTSDBClientFactory; 11 | import com.aliyun.hitsdb.client.HiTSDBConfig; 12 | import com.aliyun.hitsdb.client.exception.http.HttpClientInitException; 13 | import com.aliyun.hitsdb.client.util.UI; 14 | import com.aliyun.hitsdb.client.value.request.Point; 15 | 16 | public class TestKeepalive { 17 | 18 | HiTSDB tsdb; 19 | 20 | @Before 21 | public void init() throws HttpClientInitException { 22 | UI.pauseStart(); 23 | HiTSDBConfig config = HiTSDBConfig 24 | .address("127.0.0.1", 8242) 25 | .httpConnectionPool(1) 26 | .config(); 27 | 28 | tsdb = HiTSDBClientFactory.connect(config); 29 | } 30 | 31 | @Test 32 | public void keepalive() throws InterruptedException { 33 | for (int i = 0; i < 1000000; i++) { 34 | Point point = createPoint(i % 4, 1.123); 35 | tsdb.put(point); 36 | UI.pauseStart(); 37 | } 38 | } 39 | 40 | @After 41 | public void end() throws IOException { 42 | tsdb.close(); 43 | } 44 | 45 | public Point createPoint(int tag, double value) { 46 | int t = (int) (System.currentTimeMillis() / 1000); 47 | return Point.metric("test-performance").tag("tag", String.valueOf(tag)).value(t, value).build(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/log/TestLog.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.log; 2 | 3 | import org.junit.Test; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | public class TestLog { 8 | private Logger L = LoggerFactory.getLogger(TestLog.class); 9 | 10 | @Test 11 | public void testLogLevel(){ 12 | L.trace("hello {}","world"); 13 | L.debug("hello {}","world"); 14 | L.info("hello {}","world"); 15 | L.warn("hello {}","world"); 16 | L.error("hello {}","world"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/performance/TestFastJSON.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.performance; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.concurrent.CountDownLatch; 7 | import java.util.concurrent.atomic.AtomicLong; 8 | 9 | import org.junit.After; 10 | import org.junit.Ignore; 11 | import org.junit.Test; 12 | 13 | import com.alibaba.fastjson.JSON; 14 | import com.aliyun.hitsdb.client.value.request.Point; 15 | 16 | @Ignore 17 | public class TestFastJSON { 18 | int BatchTimes = 50000; 19 | int BatchSize = 1000; 20 | int P_NUM = 1; 21 | final CountDownLatch countDownLatch = new CountDownLatch(P_NUM); 22 | final AtomicLong T0 = new AtomicLong(); 23 | final AtomicLong T1 = new AtomicLong(); 24 | 25 | @Test 26 | public void test() throws InterruptedException, IOException { 27 | System.out.println("按下任意键,开始运行..."); 28 | while (true) { 29 | int read = System.in.read(); 30 | if (read != 0) { 31 | break; 32 | } 33 | } 34 | 35 | for (int thread_index = 0; thread_index < P_NUM; thread_index++) { 36 | new Thread(new Runnable() { 37 | 38 | @Override 39 | public void run() { 40 | T0.compareAndSet(0, System.currentTimeMillis()); 41 | long t0 = System.currentTimeMillis(); 42 | for (int n = 0; n < BatchTimes; n++) { 43 | List ps = new ArrayList(BatchSize); 44 | for (int i = 0; i < BatchSize; i++) { 45 | Point p = createPoint(1, 1.12345678910); 46 | ps.add(p); 47 | } 48 | JSON.toJSONString(ps); 49 | } 50 | long t1 = System.currentTimeMillis(); 51 | 52 | double dt = t1 - t0; 53 | System.out.println(dt + "ms \n" + BatchTimes * BatchSize / dt + "K/s \n-----"); 54 | System.out.println(); 55 | 56 | countDownLatch.countDown(); 57 | } 58 | }).start(); 59 | } 60 | 61 | // 发送线程发送完毕 62 | countDownLatch.await(); 63 | System.out.println("主线程将要结束,尝试优雅关闭"); 64 | } 65 | 66 | @After 67 | public void end() throws IOException { 68 | // 优雅关闭 69 | T1.compareAndSet(0, System.currentTimeMillis()); 70 | 71 | double dt = T1.get() - T0.get(); 72 | System.out.println("时间:" + (dt)); 73 | System.out.println("Fast解析速率" + BatchTimes * BatchSize * P_NUM / dt + "K/s"); 74 | System.out.println("结束"); 75 | } 76 | 77 | public Point createPoint(int tag, double value) { 78 | int t = (int) (System.currentTimeMillis() / 1000); 79 | return Point.metric("test-performance").tag("tag", String.valueOf(tag)).value(t, value).build(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/util/DateUtils.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Calendar; 6 | import java.util.Date; 7 | 8 | public class DateUtils { 9 | public static Date toDate(String strDate){ 10 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 11 | Date date = null; 12 | try { 13 | date = sdf.parse(strDate); 14 | } catch (ParseException e) { 15 | e.printStackTrace(); 16 | } 17 | return date; 18 | } 19 | 20 | public static Date now() { 21 | return new Date(); 22 | } 23 | 24 | public static Date add(Date date,long timestamp){ 25 | Calendar instance = Calendar.getInstance(); 26 | instance.setTimeInMillis(date.getTime() + timestamp); 27 | Date time = instance.getTime(); 28 | return time; 29 | } 30 | 31 | public static int toTimestampSecond(Date date){ 32 | int second = (int)(date.getTime()/1000); 33 | return second; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/util/UI.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.util; 2 | 3 | import java.io.IOException; 4 | 5 | public class UI { 6 | public static void pauseStart() { 7 | System.out.println("按下任意键,开始运行..."); 8 | while (true) { 9 | int read; 10 | try { 11 | read = System.in.read(); 12 | if (read != 0) { 13 | break; 14 | } 15 | } catch (IOException e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | System.out.println("开始运行"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/FlushTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.aliyun.hitsdb.client.value.request.Point; 5 | import org.junit.Test; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collections; 9 | import java.util.List; 10 | import java.util.concurrent.ArrayBlockingQueue; 11 | import java.util.concurrent.BlockingQueue; 12 | 13 | /** 14 | * Copyright @ 2020 alibaba.com 15 | * All right reserved. 16 | * Function:Flush Test 17 | * 18 | * @author Benedict Jin 19 | * @since 2020/8/27 20 | */ 21 | public class FlushTest { 22 | 23 | /** 24 | * {"timestamp":1} 25 | * {"timestamp":2} 26 | * {"timestamp":3} 27 | * {"timestamp":4} 28 | * {"timestamp":5} 29 | * [{"timestamp":1},{"timestamp":2}] 30 | * [{"timestamp":3},{"timestamp":4}] 31 | * [{"timestamp":5}] 32 | */ 33 | @Test 34 | public void testFlush() throws Exception { 35 | int batchPutBufferSize = 1024; 36 | int batchPutSize = 2; 37 | BlockingQueue pointQueue = new ArrayBlockingQueue(batchPutBufferSize); 38 | final Point point1 = new Point(); 39 | point1.setTimestamp(1L); 40 | pointQueue.put(point1); 41 | final Point point2 = new Point(); 42 | point2.setTimestamp(2L); 43 | pointQueue.put(point2); 44 | final Point point3 = new Point(); 45 | point3.setTimestamp(3L); 46 | pointQueue.put(point3); 47 | final Point point4 = new Point(); 48 | point4.setTimestamp(4L); 49 | pointQueue.put(point4); 50 | final Point point5 = new Point(); 51 | point5.setTimestamp(5L); 52 | pointQueue.put(point5); 53 | final Point[] points = pointQueue.toArray(new Point[0]); 54 | for (Point point : points) { 55 | System.out.println(point); 56 | } 57 | final ArrayList pointList = new ArrayList(points.length); 58 | Collections.addAll(pointList, points); 59 | for (int i = 0; i <= points.length - 1; i += batchPutSize) { 60 | final int endBound = Math.min(points.length, i + batchPutSize); 61 | final List sub = pointList.subList(i, endBound); 62 | System.out.println(JSON.toJSONString(sub)); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/TestMetricPointStringValue.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.TimeZone; 6 | 7 | import org.junit.*; 8 | 9 | import com.aliyun.hitsdb.client.value.request.Point; 10 | 11 | public class TestMetricPointStringValue { 12 | 13 | private String metric; 14 | private long time; 15 | 16 | private static TimeZone defaultTz; 17 | 18 | @BeforeClass 19 | public static void setup() { 20 | defaultTz = TimeZone.getDefault(); 21 | TimeZone.setDefault(TimeZone.getTimeZone("GMT+8:00")); 22 | } 23 | 24 | @AfterClass 25 | public static void finish() { 26 | // reset 27 | TimeZone.setDefault(defaultTz); 28 | } 29 | 30 | @Before 31 | public void init() throws ParseException { 32 | metric = "test"; 33 | String strDate = "2017-08-01 13:14:15"; 34 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 35 | time = sdf.parse(strDate).getTime(); 36 | } 37 | 38 | @Test 39 | public void testPoint0() { 40 | int timestamp = (int) (time / 1000); 41 | Point point = Point 42 | .metric(metric) 43 | .tag("tagk1", "tagv1").tag("tagk2", "tagv2").tag("tagk3", "tagv3") 44 | .timestamp(timestamp) 45 | .value(12.3) 46 | .build(); 47 | 48 | String json = point.toJSON(); 49 | Assert.assertEquals("{\"metric\":\"test\",\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\",\"tagk3\":\"tagv3\"},\"timestamp\":1501564455,\"value\":12.3}", 50 | json); 51 | } 52 | 53 | @Test(expected = IllegalArgumentException.class) 54 | public void testPoint1() { 55 | int timestamp = (int) (time / 1000); 56 | Point point = Point 57 | .metric(metric) 58 | .tag("tagk1", "tagv1").tag("tagk2", "tagv2").tag("tagk3", "tagv3") 59 | .timestamp(timestamp) 60 | .build(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/TestTagResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value; 2 | 3 | import java.util.*; 4 | import java.util.Map.Entry; 5 | 6 | import org.junit.AfterClass; 7 | import org.junit.Assert; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | import com.alibaba.fastjson.JSON; 12 | import com.alibaba.fastjson.JSONArray; 13 | import com.alibaba.fastjson.JSONObject; 14 | import com.aliyun.hitsdb.client.value.response.TagResult; 15 | 16 | public class TestTagResult { 17 | private static TimeZone defaultTz; 18 | 19 | @BeforeClass 20 | public static void setup() { 21 | defaultTz = TimeZone.getDefault(); 22 | TimeZone.setDefault(TimeZone.getTimeZone("GMT+8:00")); 23 | } 24 | 25 | @AfterClass 26 | public static void finish() { 27 | // reset 28 | TimeZone.setDefault(defaultTz); 29 | } 30 | 31 | @Test 32 | public void testJSONToResult() { 33 | String json = "[{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1.012\"}]"; 34 | 35 | try { 36 | JSONArray array = JSON.parseArray(json); 37 | // System.out.println(array); 38 | Assert.assertEquals(array.toString(), 39 | "[{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1\"},{\"host\":\"127.0.0.1.012\"}]"); 40 | List arrayList = new ArrayList(array.size()); 41 | 42 | Iterator iterator = array.iterator(); 43 | while (iterator.hasNext()) { 44 | JSONObject object = (JSONObject) iterator.next(); 45 | Set> entrySet = object.entrySet(); 46 | for (Entry entry : entrySet) { 47 | String key = entry.getKey(); 48 | Assert.assertNotEquals(key, null); 49 | Object valueObject = entry.getValue(); 50 | Assert.assertNotEquals(key, null); 51 | String value = valueObject.toString(); 52 | TagResult tagResult = new TagResult(key, value); 53 | arrayList.add(tagResult); 54 | break; 55 | } 56 | } 57 | Assert.assertEquals(arrayList.size(), 3); 58 | } catch (Exception ex) { 59 | ex.printStackTrace(); 60 | } 61 | 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/DeleteMetaRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | public class DeleteMetaRequestTest { 9 | 10 | @Test 11 | public void testDeleteMetaRequest() { 12 | DeleteMetaRequest deleteMetaRequest = DeleteMetaRequest.metric("testMetric") 13 | .tag("tagk1", "tagv1").tag("tagk2", "tagv2") 14 | .deleteData(false).recursive(true) 15 | .build(); 16 | Assert.assertNotNull(deleteMetaRequest); 17 | 18 | String json = deleteMetaRequest.toJSON(); 19 | 20 | JSONObject object = JSON.parseObject(json); 21 | 22 | Assert.assertTrue("testMetric".equals(object.getString("metric"))); 23 | Assert.assertFalse(object.getBoolean("deleteData")); 24 | Assert.assertTrue(object.getBoolean("recursive")); 25 | Assert.assertNull(object.getJSONObject("fields")); 26 | 27 | System.out.println(json); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/FilterTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.type.FilterType; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertNotNull; 7 | 8 | public class FilterTest { 9 | 10 | 11 | @Test 12 | public void testFilterBuilderWithoutGroupBy() { 13 | Filter filter = Filter.filter(FilterType.Wildcard,"*").build(); 14 | assertNotNull(filter); 15 | } 16 | 17 | 18 | @Test 19 | public void testFilterBuilderWithGroupBy() { 20 | Filter filter = Filter.filter(FilterType.Wildcard,"tagk","null",false).build(); 21 | assertNotNull(filter); 22 | } 23 | 24 | @Test 25 | public void testGeoFilterBuilder() { 26 | Filter filter = Filter.filter(FilterType.GeoIntersects,"tagk","POLYGON ((111 22, 111 23, 111 24, 111 22))",false).build(); 27 | assertNotNull(filter); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/LastPointSubQueryTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | public class LastPointSubQueryTest { 11 | @Test 12 | public void testLastPointSubQuerySerialization() { 13 | { 14 | LastPointSubQuery subQuery = LastPointSubQuery 15 | .builder("fake.metric") 16 | .hint(new HashMap>(){{ 17 | put("tagk", new HashMap(){{ 18 | put("iotid", 1); 19 | }}); 20 | }}) 21 | .build(); 22 | String serializedString = JSON.toJSONString(subQuery); 23 | System.out.println(serializedString); 24 | 25 | Assert.assertTrue(serializedString.equals("{\"metric\":\"fake.metric\",\"hint\":{\"tagk\":{\"iotid\":1}}}")); 26 | 27 | LastPointSubQuery newSubQuery = JSON.parseObject(serializedString, LastPointSubQuery.class); 28 | 29 | Assert.assertTrue(newSubQuery.getMetric().equals(subQuery.getMetric())); 30 | Assert.assertNotNull(newSubQuery.getHint()); 31 | Assert.assertNotNull(newSubQuery.getHint().get("tagk")); 32 | 33 | Assert.assertEquals(subQuery.getHint().get("tagk").get("iotid"), newSubQuery.getHint().get("tagk").get("iotid")); 34 | } 35 | 36 | { 37 | LastPointSubQuery subQuery = LastPointSubQuery 38 | .builder("fake.metric") 39 | .hint(null) 40 | .build(); 41 | String serializedString = JSON.toJSONString(subQuery); 42 | System.out.println(serializedString); 43 | 44 | Assert.assertTrue(serializedString.equals("{\"metric\":\"fake.metric\"}")); 45 | 46 | LastPointSubQuery newSubQuery = JSON.parseObject(serializedString, LastPointSubQuery.class); 47 | 48 | Assert.assertTrue(newSubQuery.getMetric().equals(subQuery.getMetric())); 49 | Assert.assertNull(newSubQuery.getHint()); 50 | } 51 | 52 | { 53 | try { 54 | LastPointSubQuery subQuery = LastPointSubQuery 55 | .builder("fake.metric") 56 | .hint(new HashMap>()) 57 | .build(); 58 | Assert.fail("the LastPointSubQuery object should not be created successfully"); 59 | } catch (IllegalArgumentException iaex) { 60 | System.err.println(iaex.getMessage()); 61 | } catch (Exception ex) { 62 | Assert.fail("unexpected exception: " + ex.getClass().getName()); 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/MultiFieldQueryRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.aliyun.hitsdb.client.value.type.Aggregator; 6 | import com.aliyun.hitsdb.client.value.type.DownsampleDataSource; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | public class MultiFieldQueryRequestTest { 11 | 12 | @Test 13 | public void testMultiFieldSubQueryRequest() { 14 | final String metric = "wind"; 15 | final String field = "speed"; 16 | long startTimestamp = System.currentTimeMillis(); 17 | long endTimestamp = startTimestamp + 1; 18 | 19 | MultiFieldSubQueryDetails fieldSubQueryDetails = MultiFieldSubQueryDetails.field(field).aggregator(Aggregator.NONE) 20 | .downsample("5s-sum") 21 | .build(); 22 | MultiFieldSubQuery subQuery = MultiFieldSubQuery.metric(metric) 23 | .fieldsInfo(fieldSubQueryDetails) 24 | .downsampleDataSource(DownsampleDataSource.DOWNSAMPLE) 25 | .build(); 26 | MultiFieldQuery query = MultiFieldQuery.start(startTimestamp).end(endTimestamp).sub(subQuery).build(); 27 | 28 | Assert.assertNotNull(query); 29 | String json = query.toJSON(); 30 | JSONObject object = JSON.parseObject(json); 31 | 32 | Assert.assertEquals(startTimestamp, object.getLongValue("start")); 33 | Assert.assertEquals(endTimestamp, object.getLongValue("end")); 34 | 35 | Assert.assertNotNull(object.getJSONArray("queries")); 36 | Assert.assertEquals(1, object.getJSONArray("queries").size()); 37 | System.out.println(json); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/MultiFieldSubQueryRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.aliyun.hitsdb.client.value.type.Aggregator; 6 | import com.aliyun.hitsdb.client.value.type.DownsampleDataSource; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | public class MultiFieldSubQueryRequestTest { 11 | 12 | @Test 13 | public void testMultiFieldSubQueryRequest() { 14 | final String metric = "wind"; 15 | final String field = "speed"; 16 | 17 | MultiFieldSubQueryDetails fieldSubQueryDetails = MultiFieldSubQueryDetails.field(field).aggregator(Aggregator.NONE) 18 | .downsample("5s-sum") 19 | .build(); 20 | MultiFieldSubQuery subQuery = MultiFieldSubQuery.metric(metric) 21 | .fieldsInfo(fieldSubQueryDetails) 22 | .downsampleDataSource(DownsampleDataSource.DOWNSAMPLE) 23 | .build(); 24 | 25 | Assert.assertNotNull(subQuery); 26 | 27 | String json = subQuery.toJSON(); 28 | JSONObject object = JSON.parseObject(json); 29 | 30 | Assert.assertEquals(metric, object.getString("metric")); 31 | Assert.assertEquals("DOWNSAMPLE", object.getString("downsampleDataSource")); 32 | 33 | Assert.assertNotNull(object.getJSONArray("fields")); 34 | Assert.assertEquals(1, object.getJSONArray("fields").size()); 35 | 36 | System.out.println(json); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/PointTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | public class PointTest { 8 | 9 | 10 | @Test 11 | public void test_can_set_empty_string_tag_value() { 12 | Point point = Point.metric("hh").tag("tag", "").value(System.currentTimeMillis(), Math.random()).build(); 13 | Assert.assertNotNull(point); 14 | Assert.assertEquals("", point.getTags().get("tag")); 15 | } 16 | 17 | @Test 18 | public void testPointValueSerialize() { 19 | String metric = "test"; 20 | 21 | final byte[] byteValue = new byte[]{0x1a,0x0a,0x0f}; 22 | Point point = Point.metric(metric).tag("tagk1", "tagv1").tag("tagk2", "tagv2").tag("tagk3", "tagv3") 23 | .timestamp(System.currentTimeMillis()).value(byteValue).build(); 24 | 25 | String jsonString = JSON.toJSONString(point); 26 | Assert.assertTrue(jsonString.contains("type")); 27 | Assert.assertTrue(jsonString.contains("content")); 28 | 29 | Point p = JSON.parseObject(jsonString,Point.class); 30 | Assert.assertArrayEquals((byte[]) p.getValue(), byteValue); 31 | 32 | point.setValue(2); 33 | jsonString = JSON.toJSONString(point); 34 | Assert.assertFalse(jsonString.contains("type")); 35 | Assert.assertFalse(jsonString.contains("content")); 36 | p =JSON.parseObject(jsonString,Point.class); 37 | Assert.assertSame(p.getValue(), 2); 38 | } 39 | 40 | @Test 41 | public void testGeoPointValueSerialize() { 42 | String metric = "test"; 43 | 44 | final String wktString = "POINT (110 23)"; 45 | final GeoPointValue gp = new GeoPointValue(wktString); 46 | Point point = Point.metric(metric).tag("tagk1", "tagv1").tag("tagk2", "tagv2").tag("tagk3", "tagv3") 47 | .timestamp(System.currentTimeMillis()).value(gp).build(); 48 | 49 | String jsonString = JSON.toJSONString(point); 50 | Assert.assertTrue(jsonString.contains("type")); 51 | Assert.assertTrue(jsonString.contains("geopoint")); 52 | Assert.assertTrue(jsonString.contains("content")); 53 | Assert.assertTrue(jsonString.contains(wktString)); 54 | 55 | 56 | Point p = JSON.parseObject(jsonString, Point.class); 57 | Assert.assertEquals(p.getValue(), gp); 58 | 59 | point.setValue(2); 60 | jsonString = JSON.toJSONString(point); 61 | Assert.assertFalse(jsonString.contains("type")); 62 | Assert.assertFalse(jsonString.contains("content")); 63 | p =JSON.parseObject(jsonString, Point.class); 64 | Assert.assertSame(p.getValue(), 2); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/SubQueryTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.aliyun.hitsdb.client.value.type.Aggregator; 4 | import com.aliyun.hitsdb.client.value.type.FilterType; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | public class SubQueryTest { 9 | 10 | 11 | 12 | 13 | @Test 14 | public void testBuildFilter() { 15 | SubQuery subQuery = SubQuery.metric("mm").aggregator(Aggregator.NONE).tag("a","b").filter(FilterType.Wildcard,"tagk","*").build(); 16 | Assert.assertNotNull(subQuery); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/request/TagsTest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | public class TagsTest { 8 | @Test 9 | public void testTagsOperationSerialization() { 10 | { 11 | TagsAddInfo tagsAddInfo = new TagsAddInfo.Builder("test").id("2").tag("tagk1", "tagv1").build(); 12 | String serializedString = JSON.toJSONString(tagsAddInfo); 13 | Assert.assertEquals(serializedString, "{\"_ids\":[\"2\"],\"metric\":\"test\",\"tags\":{\"tagk1\":\"tagv1\"}}"); 14 | } 15 | 16 | { 17 | TagsAddInfo tagsAddInfo = new TagsAddInfo.Builder("test").id("2").id("3") 18 | .tag("tagk1", "tagv1").tag("tagk2", "tagv2").build(); 19 | String serializedString = JSON.toJSONString(tagsAddInfo); 20 | Assert.assertEquals(serializedString, "{\"_ids\":[\"2\",\"3\"],\"metric\":\"test\",\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\"}}"); 21 | } 22 | 23 | { 24 | TagsShowInfo tagsShowInfo = new TagsShowInfo("test","2"); 25 | String serializedString = JSON.toJSONString(tagsShowInfo); 26 | Assert.assertEquals(serializedString, "{\"_id\":\"2\",\"metric\":\"test\"}"); 27 | } 28 | 29 | { 30 | TagsRemoveInfo tagsRemoveInfo = new TagsRemoveInfo.Builder("test","2").tag("tagk1", "tagv1").build(); 31 | String serializedString = JSON.toJSONString(tagsRemoveInfo); 32 | Assert.assertEquals(serializedString, "{\"_id\":\"2\",\"metric\":\"test\",\"tags\":{\"tagk1\":\"tagv1\"}}"); 33 | } 34 | 35 | { 36 | TagsRemoveInfo tagsRemoveInfo = new TagsRemoveInfo.Builder("test","2") 37 | .tag("tagk1", "tagv1").tag("tagk2", "tagv2").build(); 38 | String serializedString = JSON.toJSONString(tagsRemoveInfo); 39 | Assert.assertEquals(serializedString, "{\"_id\":\"2\",\"metric\":\"test\",\"tags\":{\"tagk1\":\"tagv1\",\"tagk2\":\"tagv2\"}}"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/com/aliyun/hitsdb/client/value/response/TestSummaryResult.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.hitsdb.client.value.response; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import com.alibaba.fastjson.JSON; 7 | import com.aliyun.hitsdb.client.value.response.batch.SummaryResult; 8 | 9 | 10 | public class TestSummaryResult { 11 | 12 | @Test 13 | public void testDeserialize() { 14 | String jsonStr = "{\"failed\": 13, \"success\": 12}"; 15 | SummaryResult summaryResult = JSON.parseObject(jsonStr,SummaryResult.class); 16 | int failed = summaryResult.getFailed(); 17 | int success = summaryResult.getSuccess(); 18 | Assert.assertEquals(success, 12); 19 | Assert.assertEquals(failed, 13); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,stdout 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 4 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t][%p][%c] %m%n 5 | 6 | log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 7 | log4j.appender.file.File=logs/hitsdb-client 8 | log4j.appender.file.DatePattern='_'yyyy-MM-dd'.log' 9 | log4j.appender.file.layout=org.apache.log4j.PatternLayout 10 | log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t][%p][%c] %m%n --------------------------------------------------------------------------------