MyTest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.gyee.wisdom.samplekudu;
  2. import com.gyee.wisdom.samplekudu.domain.data.LcingTagNameData;
  3. import com.gyee.wisdom.samplekudu.domain.input.InputLcing;
  4. import com.gyee.wisdom.samplekudu.entity.TsPointEntity;
  5. import com.gyee.wisdom.samplekudu.timeseries.DoubleTsData;
  6. import com.gyee.wisdom.samplekudu.timeseries.TsData;
  7. import com.gyee.wisdom.samplekudu.util.StringUtil;
  8. import org.apache.kudu.ColumnSchema;
  9. import org.apache.kudu.Schema;
  10. import org.apache.kudu.Type;
  11. import org.apache.kudu.client.*;
  12. import org.junit.jupiter.api.BeforeAll;
  13. import org.junit.jupiter.api.Tag;
  14. import org.junit.jupiter.api.Test;
  15. import org.springframework.boot.test.context.SpringBootTest;
  16. import java.lang.reflect.Field;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19. import java.sql.SQLOutput;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.Stream;
  23. /**
  24. * @description:
  25. * @auther: Wanghs
  26. * @date: 2022-11-21
  27. */
  28. @SpringBootTest
  29. public class MyTest {
  30. @Test
  31. public void test123() {
  32. System.out.printf("123");
  33. }
  34. private KuduClient kuduClient;
  35. private String kuduMaster;
  36. private String tableName;
  37. public void init() {
  38. kuduMaster = "manager1.prd.nxd1.com,21050";
  39. KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(kuduMaster);
  40. kuduClientBuilder.defaultSocketReadTimeoutMs(10000);
  41. kuduClient = kuduClientBuilder.build();
  42. }
  43. @Test
  44. public void testCreateTable() throws KuduException {
  45. //port 21050
  46. //manager1.prd.nxd1.com
  47. tableName = "impala::gyee_sample_kudu.wanghs_test";
  48. init();
  49. DeleteTableResponse deleteTableResponse = kuduClient.deleteTable(tableName);
  50. if (!kuduClient.tableExists(tableName)) {
  51. ArrayList<ColumnSchema> columnSchemas = new ArrayList<>();
  52. columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build());
  53. columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).build());
  54. columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("date", Type.UNIXTIME_MICROS).build());//日期待定
  55. columnSchemas.add(new ColumnSchema.ColumnSchemaBuilder("money", Type.DOUBLE).build());//小数待定
  56. Schema schema = new Schema(columnSchemas);
  57. CreateTableOptions options = new CreateTableOptions();
  58. List<String> partitionList = new ArrayList<>();
  59. //kudu表的分区字段是什么 TODO
  60. partitionList.add("id");
  61. //按照id.hashcode % 分区数 = 分区号
  62. options.addHashPartitions(partitionList, 6);
  63. // kuduClient.createTable(tableName, schema, options);
  64. System.out.println("123");
  65. }
  66. }
  67. @Test
  68. public void insert() throws KuduException {
  69. init();
  70. KuduSession kuduSession = kuduClient.newSession();
  71. kuduSession.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
  72. KuduTable userTable = kuduClient.openTable("impala::gyee_sample_kudu.wanghs_test");
  73. for (int i = 201; i <= 300; i++) {
  74. Insert insert = userTable.newInsert();
  75. PartialRow row = insert.getRow();
  76. row.addInt("id", i);
  77. row.addString("name", "wang" + i);
  78. OperationResponse apply = kuduSession.apply(insert);
  79. System.out.println(apply.getWriteTimestampRaw());
  80. }
  81. }
  82. @Test
  83. public void query() throws KuduException {
  84. KuduScanner.KuduScannerBuilder kuduScannerBuilder = kuduClient
  85. .newScannerBuilder(kuduClient.openTable(tableName));
  86. List<String> columns = Arrays.asList("id", "name", "moeny");
  87. kuduScannerBuilder.setProjectedColumnNames(columns);
  88. KuduScanner kuduScanner = kuduScannerBuilder.build();
  89. while (kuduScanner.hasMoreRows()) {
  90. RowResultIterator rowResults = kuduScanner.nextRows();
  91. while (rowResults.hasNext()) {
  92. RowResult row = rowResults.next();
  93. // new StringBuffer("id={},name={},money={}",
  94. // row.getInt("id"),
  95. // row.getString("name"),
  96. // row.getDouble("money"));
  97. String outStr = "id=" + row.getInt("id") + "name=" + row.getString("name") + "money=" + row.getDouble("money");
  98. System.out.println(outStr);
  99. }
  100. }
  101. }
  102. @Test
  103. public void update() throws KuduException {
  104. KuduSession kuduSession = kuduClient.newSession();
  105. kuduSession.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC);
  106. KuduTable kuduTable = kuduClient.openTable(tableName);
  107. // Update update = kuduTable.newUpdate();
  108. //id存在就修改,不存在就新增
  109. Upsert upsert = kuduTable.newUpsert();
  110. PartialRow row = upsert.getRow();
  111. row.addInt("id", 100000);
  112. row.addString("name", "yangege");
  113. row.addDouble("money", 100.222);
  114. kuduSession.apply(upsert);
  115. }
  116. @Test
  117. public void test() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  118. LcingTagNameData data = new LcingTagNameData();
  119. InputLcing lcing = new InputLcing();
  120. lcing.setWindturbineStatusUniformCode("FJZT8");
  121. lcing.setWindSpeedUniformCode("AI007");
  122. Field[] lcingFieldArrays = lcing.getClass().getDeclaredFields();
  123. for (Field field :
  124. lcingFieldArrays) {
  125. String name = field.getName();
  126. String getMethodStr = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
  127. Method getMethod = lcing.getClass().getMethod(getMethodStr);
  128. String fieldValue = (String) getMethod.invoke(lcing);
  129. String tagName = "";
  130. if (StringUtil.isNotBlank(fieldValue)) {
  131. if (fieldValue.equals("FJZT8"))
  132. tagName = "NX_FD_FJ_AI001";
  133. else
  134. tagName = "NX_FD_FJ_AI007";
  135. }
  136. String setMethodStr = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
  137. String replace = setMethodStr.replace("UniformCode", "TagName");
  138. Method setMethod = data.getClass().getMethod(replace, String.class);
  139. setMethod.invoke(data, tagName);
  140. }
  141. return;
  142. }
  143. @Test
  144. public void test2() {
  145. Double d1 = new Double(2.001);
  146. List<Long> list2 = new ArrayList<>();
  147. list2.add(1111L);
  148. list2.add(2222L);
  149. list2.add(3333L);
  150. list2.add(4444L);
  151. list2.add(5555L);
  152. list2.add(6666L);
  153. List<DoubleTsData> tsDataList = new ArrayList<>();
  154. DoubleTsData doubleTsData1 = new DoubleTsData(1111L, (short) 1, 3.1);
  155. DoubleTsData doubleTsData2 = new DoubleTsData(2222L, (short) 1, 3.2);
  156. DoubleTsData doubleTsData3 = new DoubleTsData(3333L, (short) 1, 3.3);
  157. DoubleTsData doubleTsData4 = new DoubleTsData(4444L, (short) 1, 3.4);
  158. DoubleTsData doubleTsData5 = new DoubleTsData(5555L, (short) 1, 3.5);
  159. DoubleTsData doubleTsData6 = new DoubleTsData(6666L, (short) 1, 3.6);
  160. DoubleTsData doubleTsData7 = new DoubleTsData(7777L, (short) 1, 3.7);
  161. DoubleTsData doubleTsData8 = new DoubleTsData(8888L, (short) 1, 3.8);
  162. tsDataList.add(doubleTsData1);
  163. tsDataList.add(doubleTsData2);
  164. tsDataList.add(doubleTsData3);
  165. tsDataList.add(doubleTsData4);
  166. tsDataList.add(doubleTsData5);
  167. tsDataList.add(doubleTsData6);
  168. tsDataList.add(doubleTsData7);
  169. tsDataList.add(doubleTsData8);
  170. List<DoubleTsData> tsDataList2 = new ArrayList<>();
  171. DoubleTsData doubleTsData11 = new DoubleTsData(1111L, (short) 1, 3.1);
  172. DoubleTsData doubleTsData21 = new DoubleTsData(2222L, (short) 1, 3.2);
  173. DoubleTsData doubleTsData31 = new DoubleTsData(3333L, (short) 1, 3.3);
  174. DoubleTsData doubleTsData41 = new DoubleTsData(4444L, (short) 1, 3.4);
  175. DoubleTsData doubleTsData51 = new DoubleTsData(5555L, (short) 1, 3.5);
  176. DoubleTsData doubleTsData61 = new DoubleTsData(6666L, (short) 1, 3.6);
  177. DoubleTsData doubleTsData71 = new DoubleTsData(7777L, (short) 1, 3.7);
  178. DoubleTsData doubleTsData81 = new DoubleTsData(8888L, (short) 1, 3.8);
  179. tsDataList2.add(doubleTsData11);
  180. tsDataList2.add(doubleTsData21);
  181. tsDataList2.add(doubleTsData31);
  182. tsDataList2.add(doubleTsData41);
  183. tsDataList2.add(doubleTsData51);
  184. tsDataList2.add(doubleTsData61);
  185. tsDataList2.add(doubleTsData71);
  186. tsDataList2.add(doubleTsData81);
  187. Map<String, List<DoubleTsData>> mp = new HashMap<>();
  188. mp.put("a", tsDataList);
  189. mp.put("b", tsDataList2);
  190. Map<String, List<DoubleTsData>> resultMap = new HashMap<>();
  191. for (Map.Entry<String, List<DoubleTsData>> entry :
  192. mp.entrySet()) {
  193. List<DoubleTsData> collect = tsDataList.stream().filter(s -> !list2.contains(s.getTs())).collect(Collectors.toList());
  194. resultMap.put(entry.getKey(), collect);
  195. }
  196. List<DoubleTsData> collect2 = tsDataList.stream().filter(s -> !list2.contains(s.getTs())).collect(Collectors.toList());
  197. System.out.println("---");
  198. }
  199. public void test3() throws NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException {
  200. LcingTagNameData data = new LcingTagNameData();
  201. Field field = data.getClass().getField("windturbineId");
  202. Class<?> type = field.getType();
  203. Class aClass = Class.forName(type.getName());
  204. Object o = aClass.newInstance();
  205. }
  206. private static ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();
  207. static void print(String str) {
  208. System.out.println(str + ":" + stringThreadLocal.get());
  209. stringThreadLocal.remove();
  210. }
  211. public static void main(String[] args) throws InterruptedException {
  212. new Thread(new Runnable() {
  213. @Override
  214. public void run() {
  215. MyTest.stringThreadLocal.set("local_A");
  216. print("A");
  217. System.out.println("remove后:"+stringThreadLocal.get());
  218. }
  219. },"A").start();
  220. Thread.sleep(1000);
  221. new Thread(new Runnable() {
  222. @Override
  223. public void run() {
  224. MyTest.stringThreadLocal.set("local_B");
  225. print("B");
  226. System.out.println("remove后:"+stringThreadLocal.get());
  227. }
  228. },"B").start();
  229. Thread.sleep(1000);
  230. String str1 = "通话";
  231. String str2 = "重地";
  232. System.out.println(str1.hashCode() + "====" + str2.hashCode());
  233. LcingTagNameData lc1 = new LcingTagNameData();
  234. LcingTagNameData lc2 = new LcingTagNameData();
  235. lc1.setModel("UP82");
  236. lc2.setModel("UP82");
  237. System.out.println(lc1.hashCode() + "====" + lc2.hashCode());
  238. System.out.println("equals===" + lc1.equals(lc2));
  239. System.out.println("=" + (lc1 == lc2));
  240. }
  241. }