我试图在java中运行map/reducer.以下是我的文件
WordCount.java
package counter;
public class WordCount extends Configured implements Tool {
public int run(String[] arg0) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "wordcount");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("counterinput"));
// Erase previous run output (if any)
FileSystem.get(conf).delete(new Path("counteroutput"), true);
FileOutputFormat.setOutputPath(job, new Path("counteroutput"));
job.waitForCompletion(true);
return 0;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new WordCount(), args);
System.exit(res);
}
}
Run Code Online (Sandbox Code Playgroud)
WordCountMapper.java
public class WordCountMapper extends …
Run Code Online (Sandbox Code Playgroud) 下面是我的类,我必须使用它们@Configuration
,@Controller
因为Thymeleaf
在整个应用程序中应该只有一个实例,否则我会得到例外.我的其他类都带有注释,@RequestScope
所以我不能使用单例作用域bean.所以我有一个配置和控制器的混合来获得结果,但我觉得这是一个不好的做法.我将不胜感激任何帮助重构代码并删除不良做法.
UPDATE
我在用spring-boot 1.5.14
.我使用以下方法处理模板并将处理后的模板保持为字符串.
@Controller
@Configuration
@EnableWebMvc
@ApplicationScope
public class MyThymeleafConfig {
@GetMapping("/view-template")
@ResponseBody
public void viewTemplates() {
Context context = new Context();
context.setVariable("mydata", "this is it");
String html = templateEngine().process("templates/view-to-process.html", context);
System.out.println(html);
}
/*
configuration for thymeleaf and template processing
*/
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:");
templateResolver.setSuffix(".html");
templateResolver.setCacheable(false);
templateResolver.setTemplateMode(TemplateMode.HTML); …
Run Code Online (Sandbox Code Playgroud) 如何在 spring data jpa 中执行本机查询,同时获取子实体?如果我在子实体对象上有 Eager FetchType,则 spring 数据正在执行 2 个查询。1 为父实体,1 为子实体。
有没有办法只执行 1 个本地查询来获取父实体和子实体?
家长:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Temporal(TemporalType.TIMESTAMP)
private Date ts;
@ManyToOne(fetch=FetchType.LAZY)
private Child child;
}
Run Code Online (Sandbox Code Playgroud)
孩子:
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(fetch=FetchType.LAZY, mappedBy="parent")
private Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
询问:
public interface ParentRepository extends Repository<Parent, Integer> {
@Query(value = "SELECT * from parents p inner join children c on c.id=p.childId where TIMESTAMPDIFF(SECOND, …
Run Code Online (Sandbox Code Playgroud) 我需要添加 HTTP \xe2\x80\x9cFeature-Policy\xe2\x80\x9d 响应标头,但我没有找到任何方法在 spring 中实现此标头,例如 -
\n\n@EnableWebSecurity\npublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {\n\n @Override\n protected void configure(HttpSecurity http) throws Exception {\n http\n // ...\n .headers()\n .contentSecurityPolicy("script-src \'self\' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/");\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n我可以在这里看到规范草案,但没有太多关于在 Spring 中使用它的信息。任何建议将不胜感激。
\n我需要将Spring Boot Zuul网关中的maxKeepAliveRequests值修改为大于默认值100的值。注意到此值未在Spring Boot的公共属性列表中公开,所以我尝试通过@Configuration类设置该属性:
@Configuration
public class DefaultConfig {
@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.addConnectorCustomizers(connector ->
((AbstractHttp11Protocol) connector.getProtocolHandler()).setMaxKeepAliveRequests(1000));
return factory;
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有达到预期的效果。我是否有适当的方法来更改未通过Spring通用属性公开的Tomcat属性?
我正在尝试使用thymeleaf呈现HTML文件并将结果HTML内容保存在String变量中,web-based scopes of Spring
以便稍后可以使用它来发送电子邮件或将内容转换为pdf.我已经完成了本网站给出的实现,但它给了我一些我无法弄清楚的错误.我正在使用春季靴子1.5.12.RELEASE与thymeleaf 3.0.9 n thymeleaf方言2.2.2.以下是我的实施.请帮忙.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Controller
public class jataController {
@GetMapping(value = "/manual-thym")
@ResponseBody
public void justSample() {
Context context = new Context();
String filename = "templates/view/failure";
String html = renderHtml(filename, context);
System.out.println("template\n" + html);
}
private String renderHtml(String filename, Context context) {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
templateResolver.setOrder(1);
templateResolver.setCharacterEncoding("UTF-8");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
String …
Run Code Online (Sandbox Code Playgroud) I have two entities: Comment
, and SubComment
. A Comment
can have multiple SubComment
s. I'm trying to establish a one to many/many to one bidirectional relationship with Hibernate.
I do not know what is wrong. Both of the tables seem to have been created correctly in PSQL.
Comment.java
import javax.persistence.*;
import java.util.Set;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private String text;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "comment")
private Set<SubComment> subComment;
public …
Run Code Online (Sandbox Code Playgroud) 我们知道,java String是不可变的,并且我们可以使用StringBuffer
或创建可变的String StringBuilder
.现在,如果我们在StringBuilder对象中有一个String并且我们使用toString()
它的方法,那么它将在String池中创建另一个不可变的String.如果是这样,那么如何避免这种情况.
我想在使用 thymeleaf 渲染我的 html 时有一个整数类型的计数器变量,但计数器变量意外增加。下面是我的代码。请帮忙。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:with="mycounter = 0">
<div th:with="mycounter=${mycounter + 1}">
<span th:text="${mycounter}"></span> <!-- result: 1 -->
</div>
<div th:with="mycounter=${mycounter + 1}">
<span th:text="${mycounter}"></span> <!-- result: 1 , expected: 2-->
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我有一个有3个int
变量的类:day,month和year.我也有一个方法叫做toString()
三个字段并以"dd/mm/yyyy"格式返回(如果日期或月份只有1个数字,则无需输入0).
做这个的最好方式是什么?
public String toString(){
String dateString = this.day + "/" + this.month + "/" + this.year;
return dateString;
}
Run Code Online (Sandbox Code Playgroud)
要么
public String toString(){
String dateString = Integer.toString(this.day) + "/" + Integer.toString(this.month) + "/" + Integer.toString(this.year);
return dateString;
}
Run Code Online (Sandbox Code Playgroud)