123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- package com.gyee.wisdom.samplekudu;
- import com.gyee.wisdom.samplekudu.domain.data.LcingTagNameData;
- import com.gyee.wisdom.samplekudu.domain.input.InputLcing;
- import com.gyee.wisdom.samplekudu.entity.TsPointEntity;
- import com.gyee.wisdom.samplekudu.timeseries.DoubleTsData;
- import com.gyee.wisdom.samplekudu.timeseries.TsData;
- import com.gyee.wisdom.samplekudu.util.StringUtil;
- import org.apache.kudu.ColumnSchema;
- import org.apache.kudu.Schema;
- import org.apache.kudu.Type;
- import org.apache.kudu.client.*;
- import org.junit.jupiter.api.BeforeAll;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.sql.SQLOutput;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- * @description:
- * @auther: Wanghs
- * @date: 2022-11-21
- */
- @SpringBootTest
- public class MyTest {
- @Test
- public void test123() {
- System.out.printf("123");
- }
- private KuduClient kuduClient;
- private String kuduMaster;
- private String tableName;
- public void init() {
- kuduMaster = "manager1.prd.nxd1.com,21050";
- KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(kuduMaster);
- kuduClientBuilder.defaultSocketReadTimeoutMs(10000);
- kuduClient = kuduClientBuilder.build();
- }
- @Test
- public void testCreateTable() throws KuduException {
- //port 21050
- //manager1.prd.nxd1.com
- tableName = "impala::gyee_sample_kudu.wanghs_test";
- init();
- DeleteTableResponse deleteTableResponse = kuduClient.deleteTable(tableName);
- if (!kuduClient.tableExists(tableName)) {
- ArrayList<ColumnSchema> columnSchemas = new ArrayList<>();
- columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build());
- columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).build());
- columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("date", Type.UNIXTIME_MICROS).build());//日期待定
- columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("money", Type.DOUBLE).build());//小数待定
- Schema schema = new Schema(columnSchemas);
- CreateTableOptions options = new CreateTableOptions();
- List<String> partitionList = new ArrayList<>();
- //kudu表的分区字段是什么 TODO
- partitionList.add("id");
- //按照id.hashcode % 分区数 = 分区号
- options.addHashPartitions(partitionList, 6);
- // kuduClient.createTable(tableName, schema, options);
- System.out.println("123");
- }
- }
- @Test
- public void insert() throws KuduException {
- init();
- KuduSession kuduSession = kuduClient.newSession();
- kuduSession.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
- KuduTable userTable = kuduClient.openTable("impala::gyee_sample_kudu.wanghs_test");
- for (int i = 201; i <= 300; i++) {
- Insert insert = userTable.newInsert();
- PartialRow row = insert.getRow();
- row.addInt("id", i);
- row.addString("name", "wang" + i);
- OperationResponse apply = kuduSession.apply(insert);
- System.out.println(apply.getWriteTimestampRaw());
- }
- }
- @Test
- public void query() throws KuduException {
- KuduScanner.KuduScannerBuilder kuduScannerBuilder = kuduClient
- .newScannerBuilder(kuduClient.openTable(tableName));
- List<String> columns = Arrays.asList("id", "name", "moeny");
- kuduScannerBuilder.setProjectedColumnNames(columns);
- KuduScanner kuduScanner = kuduScannerBuilder.build();
- while (kuduScanner.hasMoreRows()) {
- RowResultIterator rowResults = kuduScanner.nextRows();
- while (rowResults.hasNext()) {
- RowResult row = rowResults.next();
- // new StringBuffer("id={},name={},money={}",
- // row.getInt("id"),
- // row.getString("name"),
- // row.getDouble("money"));
- String outStr = "id=" + row.getInt("id") + "name=" + row.getString("name") + "money=" + row.getDouble("money");
- System.out.println(outStr);
- }
- }
- }
- @Test
- public void update() throws KuduException {
- KuduSession kuduSession = kuduClient.newSession();
- kuduSession.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
- KuduTable kuduTable = kuduClient.openTable(tableName);
- // Update update = kuduTable.newUpdate();
- //id存在就修改,不存在就新增
- Upsert upsert = kuduTable.newUpsert();
- PartialRow row = upsert.getRow();
- row.addInt("id", 100000);
- row.addString("name", "yangege");
- row.addDouble("money", 100.222);
- kuduSession.apply(upsert);
- }
- @Test
- public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
- LcingTagNameData data = new LcingTagNameData();
- InputLcing lcing = new InputLcing();
- lcing.setWindturbineStatusUniformCode("FJZT8");
- lcing.setWindSpeedUniformCode("AI007");
- Field[] lcingFieldArrays = lcing.getClass().getDeclaredFields();
- for (Field field :
- lcingFieldArrays) {
- String name = field.getName();
- String getMethodStr = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
- Method getMethod = lcing.getClass().getMethod(getMethodStr);
- String fieldValue = (String) getMethod.invoke(lcing);
- String tagName = "";
- if (StringUtil.isNotBlank(fieldValue)) {
- if (fieldValue.equals("FJZT8"))
- tagName = "NX_FD_FJ_AI001";
- else
- tagName = "NX_FD_FJ_AI007";
- }
- String setMethodStr = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
- String replace = setMethodStr.replace("UniformCode", "TagName");
- Method setMethod = data.getClass().getMethod(replace, String.class);
- setMethod.invoke(data, tagName);
- }
- return;
- }
- @Test
- public void test2() {
- Double d1 = new Double(2.001);
- List<Long> list2 = new ArrayList<>();
- list2.add(1111L);
- list2.add(2222L);
- list2.add(3333L);
- list2.add(4444L);
- list2.add(5555L);
- list2.add(6666L);
- List<DoubleTsData> tsDataList = new ArrayList<>();
- DoubleTsData doubleTsData1 = new DoubleTsData(1111L, (short) 1, 3.1);
- DoubleTsData doubleTsData2 = new DoubleTsData(2222L, (short) 1, 3.2);
- DoubleTsData doubleTsData3 = new DoubleTsData(3333L, (short) 1, 3.3);
- DoubleTsData doubleTsData4 = new DoubleTsData(4444L, (short) 1, 3.4);
- DoubleTsData doubleTsData5 = new DoubleTsData(5555L, (short) 1, 3.5);
- DoubleTsData doubleTsData6 = new DoubleTsData(6666L, (short) 1, 3.6);
- DoubleTsData doubleTsData7 = new DoubleTsData(7777L, (short) 1, 3.7);
- DoubleTsData doubleTsData8 = new DoubleTsData(8888L, (short) 1, 3.8);
- tsDataList.add(doubleTsData1);
- tsDataList.add(doubleTsData2);
- tsDataList.add(doubleTsData3);
- tsDataList.add(doubleTsData4);
- tsDataList.add(doubleTsData5);
- tsDataList.add(doubleTsData6);
- tsDataList.add(doubleTsData7);
- tsDataList.add(doubleTsData8);
- List<DoubleTsData> tsDataList2 = new ArrayList<>();
- DoubleTsData doubleTsData11 = new DoubleTsData(1111L, (short) 1, 3.1);
- DoubleTsData doubleTsData21 = new DoubleTsData(2222L, (short) 1, 3.2);
- DoubleTsData doubleTsData31 = new DoubleTsData(3333L, (short) 1, 3.3);
- DoubleTsData doubleTsData41 = new DoubleTsData(4444L, (short) 1, 3.4);
- DoubleTsData doubleTsData51 = new DoubleTsData(5555L, (short) 1, 3.5);
- DoubleTsData doubleTsData61 = new DoubleTsData(6666L, (short) 1, 3.6);
- DoubleTsData doubleTsData71 = new DoubleTsData(7777L, (short) 1, 3.7);
- DoubleTsData doubleTsData81 = new DoubleTsData(8888L, (short) 1, 3.8);
- tsDataList2.add(doubleTsData11);
- tsDataList2.add(doubleTsData21);
- tsDataList2.add(doubleTsData31);
- tsDataList2.add(doubleTsData41);
- tsDataList2.add(doubleTsData51);
- tsDataList2.add(doubleTsData61);
- tsDataList2.add(doubleTsData71);
- tsDataList2.add(doubleTsData81);
- Map<String, List<DoubleTsData>> mp = new HashMap<>();
- mp.put("a", tsDataList);
- mp.put("b", tsDataList2);
- Map<String, List<DoubleTsData>> resultMap = new HashMap<>();
- for (Map.Entry<String, List<DoubleTsData>> entry :
- mp.entrySet()) {
- List<DoubleTsData> collect = tsDataList.stream().filter(s -> !list2.contains(s.getTs())).collect(Collectors.toList());
- resultMap.put(entry.getKey(), collect);
- }
- List<DoubleTsData> collect2 = tsDataList.stream().filter(s -> !list2.contains(s.getTs())).collect(Collectors.toList());
- System.out.println("---");
- }
- public void test3() throws NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
- LcingTagNameData data = new LcingTagNameData();
- Field field = data.getClass().getField("windturbineId");
- Class<?> type = field.getType();
- Class aClass = Class.forName(type.getName());
- Object o = aClass.newInstance();
- }
- private static ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();
- static void print(String str) {
- System.out.println(str + ":" + stringThreadLocal.get());
- stringThreadLocal.remove();
- }
- public static void main(String[] args) throws InterruptedException {
- new Thread(new Runnable() {
- @Override
- public void run() {
- MyTest.stringThreadLocal.set("local_A");
- print("A");
- System.out.println("remove后:"+stringThreadLocal.get());
- }
- },"A").start();
- Thread.sleep(1000);
- new Thread(new Runnable() {
- @Override
- public void run() {
- MyTest.stringThreadLocal.set("local_B");
- print("B");
- System.out.println("remove后:"+stringThreadLocal.get());
- }
- },"B").start();
- Thread.sleep(1000);
- String str1 = "通话";
- String str2 = "重地";
- System.out.println(str1.hashCode() + "====" + str2.hashCode());
- LcingTagNameData lc1 = new LcingTagNameData();
- LcingTagNameData lc2 = new LcingTagNameData();
- lc1.setModel("UP82");
- lc2.setModel("UP82");
- System.out.println(lc1.hashCode() + "====" + lc2.hashCode());
- System.out.println("equals===" + lc1.equals(lc2));
- System.out.println("=" + (lc1 == lc2));
- }
- }
|