我正在使用 jackson mapper 将一个 json 字符串映射到一个类。这个类看起来像这样:
class MyClass{
@JsonProperty("my_boolean")
private boolean myBoolean;
@JsonProperty("my_int")
private int myInt;
//Getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我想检查字段 myBoolean 和 myInt 是否实际设置或包含它们的默认值(false 和 0)。我尝试使用反射并检查该字段是否为空,但我想这不适用于原始类型。这就是我现在所拥有的:
Field[] fields = myClass.getClass().getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
Object myObject = field.get(myClass);
if(myObject != null) {
//Thought this would work, but it doesn't
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还能怎么检查这个?
谢谢。
我正在尝试使用Spring使用其他API调用,如下所示:
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<String>(headers);
RestTemplate restTemplate = new RestTemplate();
Item item = restTemplate.exchange(url, HttpMethod.GET, request, Item.class).getBody();
Run Code Online (Sandbox Code Playgroud)
我从API获得的响应采用以下形式:
{
"item":[{
"itemname": "abc",
"qty":...
}]
}
Run Code Online (Sandbox Code Playgroud)
Item类包含以下字段:
Class Item{
@JsonProperty("itemname")
String name;
@JsonProperty("qty")
int quantity;
// Getter / setter methods
}
Run Code Online (Sandbox Code Playgroud)
我已经在字段中添加了JsonProperty注释,因为它们的名称与我从API获得的json不同.有了这个,我就能成功地反序列化api响应.
但是,当我尝试将Item类再次序列化为json时,字段名称为"itemname"和"qty".有没有办法将这些保留为"名称"和"数量",并且能够映射到API响应?
提前致谢.
我开始尝试使用Scala和Play来解析Json数据,并按照https://www.playframework.com/documentation/2.3.9/ScalaJson上的教程进行操作.现在,当我尝试运行那里给出的示例代码时:
val json: JsValue = Json.parse("""{
"name" : "Watership Down",
"location" : {
"lat" : 51.235685,
"long" : -1.309197
},
"residents" : [ {
"name" : "Fiver",
"age" : 4,
"role" : null
}, {
"name" : "Bigwig",
"age" : 6,
"role" : "Owsla"
} ]
}
""")
val lat = json \ "location" \ "lat"
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
java.lang.NoSuchMethodError: play.api.libs.json.JsValue.$bslash(Ljava/lang/String;)Lplay/api/libs/json/JsValue;
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我正在使用Scala 2.10和Play 2.3.9.
谢谢.
我正在编写一个代码来迭代所有可用的实例并为它们创建一个AMI,如下所示:
for reservation in reservations:
......
ami_id = ec2_conn.create_image(instance.id, ami_name, description=ami_desc, no_reboot=True)
Run Code Online (Sandbox Code Playgroud)
但是,在继续创建下一个图像之前,我该如何等待图像创建?因为我需要跟踪每个创建的ami的状态.
我知道我可以使用以下方法检索状态:
image_status = get_image(ami_id).state
Run Code Online (Sandbox Code Playgroud)
那么,我是否遍历创建的ami_ids列表,然后为每个列表获取状态?如果是这样,那么当我读取图像的状态时,如果图像仍处于未决状态怎么办?如何确定图像创建最终是否失败?
谢谢.
我试图将BufferedImage变灰(不将其转换为灰度,只需在顶部添加灰色).现在,我通过使用另一个图像,使其半透明,然后将其覆盖在我的原始图像上来做到这一点.这是我现在的代码:
package com.mypkg;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr;
public class Overlay {
public static void main(String args[]){
URL url = null;
try{
//The gray image used for overlay
url = new URL("https://hoursofidleness.files.wordpress.com/2012/06/gray-card.jpg");
BufferedImage img1 = ImageIO.read(url);
//The original image which I want to gray out
url = new URL("http://www.staywallpaper.com/wp-content/uploads/2016/01/Colorful-Wallpaper-HD-pictures-STAY015.jpg");
BufferedImage img2 = ImageIO.read(url);
BufferedImage reImg2 = Scalr.resize(img2, Scalr.Method.BALANCED, Scalr.Mode.FIT_EXACT, 150, 150);
//Make the gray image, which is used as the overlay, …
Run Code Online (Sandbox Code Playgroud) 我有一个长度为 n 的单行字符串,我想将其分成最多 3 行。每行最多可以有 45 个字符,之后我想添加一个换行符(“\n”)。第 3 行最多可以有 42 个字符,如果字符串超出这个范围,我需要包含 3 个点 (...),从而使第 3 行的总字符数也达到 45 个。
条件是换行符不应添加在单词中间。我如何有效地做到这一点?这个操作只是整个程序的一小部分,但是会被重复调用。所以我不确定我是否应该真正担心效率。
我现在正在做的是,我首先找出单词之间的空格在哪里,然后将其添加到列表中。然后,我迭代列表并找到 3 个索引,每个索引代表每行的结束词。因此,第一个索引将是最接近 45 的空格,第二个最接近 90,第三个最接近 135。然后,我使用这些索引来分割实际字符串,并分别添加“\n”和“...”。这是我的代码:
//maxCharsPerLine will be 45
public String splitString(String input, int maxCharsPerLine){
String output = "";
ArrayList<Integer> spaces = new ArrayList<Integer>();
// Logic to figure out after which word the sentence should be split so that we don't split in middle of a word
for(int index = 0; index < input.length(); index++){
if(input.charAt(index)==' '){
spaces.add(index);
} …
Run Code Online (Sandbox Code Playgroud) 我有一个非常大的csv文件,我想将其拆分成较小的文件,以便大文件中具有相同ID值(csv中的第二列)的所有条目最终都在同一个文件中.但是,我还需要在每个较小的文件中有50个不同的ID.
我有这样做的代码,但对于1 gig文件,它需要大约15 - 20分钟.有没有一种有效的方法呢?
这就是我现在所拥有的:
awk -F, '{if(NR > 1) {print >> $2"_backfill_tmp.csv"; close($2"_backfill_tmp.csv")}}' $input_file
counter=0
for file in *"_backfill_tmp.csv"
do
file_name=${input_file%.*}"_backfill2_part_"$PART_NUMBER".csv"
cat "$file" >> "$file_name"
rm "$file"
(( counter++ ))
if (( $counter % 50 == 0 )) ; then
(( PART_NUMBER++ ))
fi
done
Run Code Online (Sandbox Code Playgroud)
awk命令根据第2列的值将每一行写入单独的文件中(忽略第一行是标题),以便具有相同ID值的每一行最终都在同一文件中.我每次关闭文件因为我遇到了一个Too many files open error
我无法ulimit
在机器上设置的文件.但是,这个过程只需要15秒左右,所以不用担心.
然后我遍历每个临时创建的文件,并将它们写入单独的文件,直到$counter
达到50(即将50个文件组合在一起).现在这是花费大量时间的地方.我猜是因为有很多个人ID文件,逐个打开它们并合并它们需要很长时间.
我是一个awk初学者,所以我很确定我的代码效率不高.无论如何我可以更快地完成整个过程吗?
我有一个对象如下:
class License{
private field1;
private field2;
private boolean active;
private String activeMessage;
private boolean processed = false;
//Getter and setter methods
}
Run Code Online (Sandbox Code Playgroud)
我想做的是,根据 field1 和 field2 的值,我需要设置 isActive 标志和相应的消息。但是,如果 field1 或 field2 的规则被触发,我需要停止规则处理。也就是说,我只需要执行 1 条成功的规则。
我在一篇文章中读到,执行 ksession.fireAllRules(1) 将解决此问题。但是 fireAllRules() 方法在 Drools 6 中不可用。我也尝试输入一个返回;每条规则末尾的声明。那对我也没有帮助。
最后,我最终向我的对象添加了一个名为“processed”的附加字段。因此,每当我执行任何规则时,我都会将已处理标志设置为 true。如果该标志已经设置,那么我不会执行任何规则。这是我的规则文件:
rule "Check field1"
when
$obj : License(getField1() == "abc" && isProcessed() == false)
then
System.out.println("isProcessed >>>>>> "+$obj.isProcessed());
$obj.setActive(true);
$order.setActiveMessage("...");
$order.setProcessed(true);
end
rule "Check field2"
when
$obj : License(getField2() == "def" && isProcessed() == false) …
Run Code Online (Sandbox Code Playgroud) java ×3
algorithm ×1
amazon-ec2 ×1
awk ×1
bash ×1
boto ×1
drools ×1
graphics2d ×1
json ×1
python ×1
reflection ×1
resttemplate ×1
scala ×1
spring ×1