@Test(testName = "测试写入op") public void testWriteOp() throws IOException { ArrayList<ColumnMetaData> columnMetaDatas = new ArrayList<ColumnMetaData>(); ColumnMetaData columnMetaData = new ColumnMetaData("c1", 1); columnMetaDatas.add(columnMetaData); columnMetaData = new ColumnMetaData("c2", 2); columnMetaDatas.add(columnMetaData); columnMetaData = new ColumnMetaData("c3", 3); columnMetaDatas.add(columnMetaData); columnMetaData = new ColumnMetaData("c4", 4); columnMetaDatas.add(columnMetaData); columnMetaData = new ColumnMetaData("c5", 5); columnMetaDatas.add(columnMetaData); TableName tableName = new TableName("ogg_test.t_person"); TableMetaData tableMetaData = new TableMetaData(tableName, columnMetaDatas); DsMetaData dsMetaData = new DsMetaData(); dsMetaData.setTableMetaData(tableMetaData); DsColumn[] columns = new DsColumn[5]; columns[0] = new DsColumnAfterValue("testNormal"); columns[1] = new DsColumnAfterValue("2"); columns[2] = new DsColumnAfterValue("3"); columns[3] = new DsColumnAfterValue("2016-05-20 09:00:00"); columns[4] = new DsColumnAfterValue("6"); DsRecord dsRecord = new DsRecord(columns); DsOperation dsOperation = new DsOperation(tableName, DsOperation.OpType.DO_INSERT, "2016-05-13 19:15:15.010", 0L, 0L, dsRecord); Op op = new Op(dsOperation, tableMetaData, null); BadOperateWriter.write(op, "ogg_test.t_person", "t_person", fileName, 10000, "op error"); String jsonStr = FileUtils.readFileToString(new File(fileName)); JSONObject object = JSON.parseObject(jsonStr); Assert.assertEquals(object.getString("oracleTable"), "ogg_test.t_person"); Assert.assertEquals(object.getString("topicName"), "t_person"); Assert.assertNull(object.getString("shardId")); Assert.assertEquals(object.getString("errorMessage"), "op error"); JSONObject record = object.getJSONObject("record"); Assert.assertEquals(record.getString("c1"), "testNormal"); Assert.assertEquals(record.getString("c2"), "2"); Assert.assertEquals(record.getString("c3"), "3"); Assert.assertEquals(record.getString("c4"), "2016-05-20 09:00:00"); Assert.assertEquals(record.getString("c5"), "6"); }
private static void write(Map<String, String> record, String oracleFullTableName, String topicName, String fileName, int maxFileSize, String msg) { checkFileSize(fileName, maxFileSize); DirtyRecordInfo dirtyRecordInfo = new DirtyRecordInfo(); dirtyRecordInfo.setOracleTable(oracleFullTableName); dirtyRecordInfo.setTopicName(topicName); dirtyRecordInfo.setShardId(null); dirtyRecordInfo.setErrorMessage(msg); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dirtyRecordInfo.setErrorTime(simpleDateFormat.format(new Date())); dirtyRecordInfo.setRecord(record); try { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true)); bw.write(JsonHelper.beanToJson(dirtyRecordInfo) + "\n"); bw.close(); } catch (IOException e) { logger.error("logBadOperation() failed. ", e); throw new RuntimeException("logBadOperation() failed. ", e); } }
BadOperateWriter { private static void write(Map<String, String> record, String oracleFullTableName, String topicName, String fileName, int maxFileSize, String msg) { checkFileSize(fileName, maxFileSize); DirtyRecordInfo dirtyRecordInfo = new DirtyRecordInfo(); dirtyRecordInfo.setOracleTable(oracleFullTableName); dirtyRecordInfo.setTopicName(topicName); dirtyRecordInfo.setShardId(null); dirtyRecordInfo.setErrorMessage(msg); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dirtyRecordInfo.setErrorTime(simpleDateFormat.format(new Date())); dirtyRecordInfo.setRecord(record); try { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true)); bw.write(JsonHelper.beanToJson(dirtyRecordInfo) + "\n"); bw.close(); } catch (IOException e) { logger.error("logBadOperation() failed. ", e); throw new RuntimeException("logBadOperation() failed. ", e); } } }
BadOperateWriter { private static void write(Map<String, String> record, String oracleFullTableName, String topicName, String fileName, int maxFileSize, String msg) { checkFileSize(fileName, maxFileSize); DirtyRecordInfo dirtyRecordInfo = new DirtyRecordInfo(); dirtyRecordInfo.setOracleTable(oracleFullTableName); dirtyRecordInfo.setTopicName(topicName); dirtyRecordInfo.setShardId(null); dirtyRecordInfo.setErrorMessage(msg); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dirtyRecordInfo.setErrorTime(simpleDateFormat.format(new Date())); dirtyRecordInfo.setRecord(record); try { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true)); bw.write(JsonHelper.beanToJson(dirtyRecordInfo) + "\n"); bw.close(); } catch (IOException e) { logger.error("logBadOperation() failed. ", e); throw new RuntimeException("logBadOperation() failed. ", e); } }  }
BadOperateWriter { private static void write(Map<String, String> record, String oracleFullTableName, String topicName, String fileName, int maxFileSize, String msg) { checkFileSize(fileName, maxFileSize); DirtyRecordInfo dirtyRecordInfo = new DirtyRecordInfo(); dirtyRecordInfo.setOracleTable(oracleFullTableName); dirtyRecordInfo.setTopicName(topicName); dirtyRecordInfo.setShardId(null); dirtyRecordInfo.setErrorMessage(msg); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dirtyRecordInfo.setErrorTime(simpleDateFormat.format(new Date())); dirtyRecordInfo.setRecord(record); try { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true)); bw.write(JsonHelper.beanToJson(dirtyRecordInfo) + "\n"); bw.close(); } catch (IOException e) { logger.error("logBadOperation() failed. ", e); throw new RuntimeException("logBadOperation() failed. ", e); } }  static void checkFileSize(String fileName, int maxFileSize); static void write(Op op, String oracleFullTableName, String topicName, String fileName,
                             int maxFileSize, String msg); static void write(RecordEntry recordEntry, String oracleFullTableName, String topicName,
                             String fileName, int maxFileSize, String msg); }
BadOperateWriter { private static void write(Map<String, String> record, String oracleFullTableName, String topicName, String fileName, int maxFileSize, String msg) { checkFileSize(fileName, maxFileSize); DirtyRecordInfo dirtyRecordInfo = new DirtyRecordInfo(); dirtyRecordInfo.setOracleTable(oracleFullTableName); dirtyRecordInfo.setTopicName(topicName); dirtyRecordInfo.setShardId(null); dirtyRecordInfo.setErrorMessage(msg); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dirtyRecordInfo.setErrorTime(simpleDateFormat.format(new Date())); dirtyRecordInfo.setRecord(record); try { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true)); bw.write(JsonHelper.beanToJson(dirtyRecordInfo) + "\n"); bw.close(); } catch (IOException e) { logger.error("logBadOperation() failed. ", e); throw new RuntimeException("logBadOperation() failed. ", e); } }  static void checkFileSize(String fileName, int maxFileSize); static void write(Op op, String oracleFullTableName, String topicName, String fileName,
                             int maxFileSize, String msg); static void write(RecordEntry recordEntry, String oracleFullTableName, String topicName,
                             String fileName, int maxFileSize, String msg);  }