Kafka制作人发送无效字符

使用以下代码,我发送Elasticsearch文档以进行索引。 我尝试将基本对象转换为JSON并通过制作人发送。 但是,每条消息(从控制台检查)都附加了像 – t {“productId”:2455这样的乱码字符 public boolean sendMessage() { PageRequest page = new PageRequest(0, 1); Product p = product.findByName(“Cream”, page).getContent().get(0); String json = “”; ObjectMapper mapper = new ObjectMapper(); try { json = mapper.writeValueAsString(p); } catch (JsonProcessingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } logger.info(“JSON = ” + json); boolean status = […]

C struct写在文件中,用Java打开

例如在CI中有结构: typedef struct { int number; double x1; double y1; double x2; double y2; double x3; double y3; } CTRstruct;` 然后我把它写到文件fwrite(&tr, 1, sizeof(tr), fp); (tr – 它的CTRstruct var,fp – 文件指针); 然后我需要用Java阅读它! 我真的不知道如何从文件中读取struct …我试图用ObjectInputStream()读取它,最后的想法是用RandomAccessFile()读取但我也不知道如何…( readLong(), readDouble()也不起作用,它工作原理但不读取正确的数据)。 那么,任何想法如何用Java从二进制文件中读取C结构? 如果它很有意思,我的版本读取整数(但它很丑,我不知道如何处理double ): public class MyDataInputStream扩展DataInputStream { public MyDataInputStream(InputStream AIs) { super(AIs); } public int readInt1() throws IOException{ int ch1 […]

没有内存屏障的无序写入:数据竞争的唯一可能原因?

在Brian Goetz的实践中通过Java Concurrency时,我遇到了以下行: 当一个变量被多个线程读取并由至少一个线程写入时,会发生数据争用,但读取和写入不是由before-before排序的 。 正确同步的程序是没有数据竞争的程序; 正确同步的程序表现出顺序一致性,这意味着程序中的所有操作似乎都以固定的全局顺序发生。 我的问题是,乱序是写java或其他编程语言中数据竞争条件的唯一原因吗? UPDATE 好的,我做了一些关于数据竞争的调查,并从oracle官方网站上找到了以下内容: 线程分析器检测在执行multithreading进程期间发生的数据争用。 数据竞争发生在: 单个进程中的两个或多个线程同时访问同一个内存位置,并且 至少有一个访问是用于写入,和 线程没有使用任何独占锁来控制它们对该内存的访问。 当这三个条件成立时, 访问顺序是非确定性的 ,并且计算可以根据该顺序从运行到运行给出不同的结果。 某些数据争用可能是良性的(例如,当内存访问用于忙等待时),但许多数据争用是程序中的错误。 在这一部分中,提到: 访问顺序是非确定性的 它是在讨论线程访问内存位置的顺序吗? 如果是,那么同步永远不会保证线程将访问代码块的顺序。 那么,同步如何解决数据竞争问题呢?

在字符串数组上实现二进制搜索

我对此有点麻烦。 输入数组基于文件输入,数组的大小由文件中的第一行指定。 binarySearch方法似乎看起来没问题,但似乎没有工作。 有人能帮忙吗? 谢谢。 public static int binarySearch(String[] a, String x) { int low = 0; int high = a.length – 1; int mid; while (low <= high) { mid = (low + high) / 2; if (a[mid].compareTo(x) 0) { high = mid – 1; } else { return mid; } } return -1; […]

MigLayout错误:“绝对链接值中的不稳定循环依赖!”

为什么这个SSCCE(使用MigLayout库)…… public static void main(String[] args) { try { UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } JFrame frame = new JFrame(); frame.setLayout(new MigLayout(new LC().fill().insetsAll(“0”))); JTabbedPane jtp = new JTabbedPane(); jtp.add(new JPanel(), “Tab 1”); jtp.add(new JPanel(), “Tab 2”); JLabel label = new JLabel(“label”); JPanel panel = new JPanel(new MigLayout(new […]

带SQL的JAVA:insert命令的返回值?

我有一个作者表:authorID,authorName。 authorID是一个带自动增量的pk。 我想在java中编写一个从用户获取名称并将其添加到表中的方法。 但是我需要返回作者的id。 有没有办法用1 sql语句做到这一点? 例如,如果我的代码有命令: stmt.executeUpdate(“INSERT INTO authors ” + “VALUES (, ‘”+ string.get(1) +”‘)”); 其中string.get(1)是作者姓名。 现在,如果我写: ResultSet rs =stmt.executeUpdate(“INSERT INTO authors ” + “VALUES (, ‘”+ string.get(1) +”‘)”); 它表示错误,因为rs是结果集,但返回的值是int。 这是我插入的行的pk吗?

如何删除不平衡/未共享的双引号(在Java中)

我想与大家分享这个相对聪明的问题。 我试图从字符串中删除不平衡/不成对的双引号。 我的工作正在进行中,我可能接近解决方案。 但是,我还没有得到一个有效的解决方案。 我无法从字符串中删除未配对/未配对的双引号。 示例输入 string1=injunct! alter ego.” string2=successor “alter ego” single employer” “proceeding “citation assets” 输出应该是 string1=injunct! alter ego. string2=successor “alter ego” single employer proceeding “citation assets” 此问题听起来类似于使用Java删除不平衡/未共享的括号 这是我到目前为止的代码(它不会删除所有非空双引号) private String removeUnattachedDoubleQuotes(String stringWithDoubleQuotes) { String firstPass = “”; String openingQuotePattern = “\\\”[a-z0-9\\p{Punct}]”; String closingQuotePattern = “[a-z0-9\\p{Punct}]\\\””; int doubleQuoteLevel = 0; for (int i […]

如何比较两个MultiMaps?

我有两个Multimaps,它们是从两个巨大的CSV文件创建的。 Multimap mapOne = ArrayListMultimap.create(); Multimap mapTwo = ArrayListMultimap.create(); 我假设一个CSV列作为密钥,每个密钥都有数千个与之关联的值。 这些Multimap包含的数据应该相同。 现在我想比较这些Multimap的数据,并查找是否有任何值不同。 以下是我想到的两种方法: 方法一: 从Multimap一个大列表。 这个大清单将包含一些单独的清单。 每个较小的列表都包含一个唯一值,该值是从Multimap读取的“关键”及其相关值,它将构成该单个列表的其余部分。 ArrayList<Collection> bigList = new ArrayList<Collection>(); 在bigList中将是单个小列表A,B,C等。 我计划在检查来自第二个Multimap单个列表包含该“key”元素的基础上从两个文件的每个bigList中选择单个列表。 如果是,则比较这两个列表并找到任何无法匹配的内容。 方法二: 比较Multimap但我不确定如何做到这一点。 哪种方法应该有更短的执行时间? 我需要在最短的时间内完成操作。

java字符串到datetime转换问题

我似乎无法看到下面的示例代码的问题。 出于某种原因,似乎忽略了年份,并且说日期是相同的,如下面的输出中所示。 我一定很遗憾。 2006年1月28日 2007年1月16日 2008年4月1日星期二00:00:00 2008年4月1日星期二00:00:00 DONE import java.util.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; class ExampleProgram { public static void main(String[] args){ DateFormat df = new SimpleDateFormat(“dd/MM/yyyy”); String d1String = “01/28/2006”; String d2String = “01/16/2007”; System.out.println(d1String); System.out.println(d2String); Date d1=null; try { d1 = df.parse(d1String); } catch (ParseException e) { System.out.println(e.getMessage()); } Date d2=null; […]

通过java中的POST方法发送Xml字符串

我想通过POST方法将xml字符串传递给URL。 我试过下面的代码片段,但它没有返回任何内容 disableCertificateValidation(); String url = “https://..url”; //https Properties sysProps = System.getProperties(); sysProps.put(“proxySet”, “true”); sysProps.put(“proxyHost”, “1.2.3.4”); sysProps.put(“proxyPort”, “80”); Authenticator authenticator = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return (new PasswordAuthentication(“userid”, “password”.toCharArray())); } }; Authenticator.setDefault(authenticator); String xml = —xml string; URL urll; HttpURLConnection connection = null; try { // Create connection urll = new URL(url); […]