小编Sam*_*Sam的帖子

键入map中的键不匹配:期望org.apache.hadoop.io.Text,收到org.apache.hadoop.io.LongWritable

我试图在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)

java hadoop mapreduce

21
推荐指数
3
解决办法
3万
查看次数

使用@Configuration和@Controller注释一个类.需要帮助重构

下面是我的类,我必须使用它们@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)

java spring thymeleaf spring-boot

10
推荐指数
4
解决办法
470
查看次数

spring data jpa本机查询与连接

如何在 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)

mysql hibernate jpa spring-data spring-data-jpa

7
推荐指数
1
解决办法
3万
查看次数

如何在 Spring 中添加 HTTP“Feature-Policy”标头

我需要添加 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-security content-security-policy spring-boot

6
推荐指数
1
解决办法
4205
查看次数

在Spring Boot嵌入式Tomcat中配置maxKeepAliveRequests

我需要将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属性?

tomcat spring-boot

5
推荐指数
1
解决办法
5264
查看次数

在基于Web的Scopes of Spring中使用Thymeleaf处理HTML文件,并将处理后的模板存储为String

我正在尝试使用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)

java spring thymeleaf spring-boot

5
推荐指数
1
解决办法
1556
查看次数

Spring Boot &amp; Postgres: relation "sub_comment" does not exist

I have two entities: Comment, and SubComment. A Comment can have multiple SubComments. 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 postgresql hibernate spring-data-jpa spring-boot

5
推荐指数
1
解决办法
1万
查看次数

StringBuilder或StringBuffer的toString()方法是否会创建一个新的不可变String?

我们知道,java String是不可变的,并且我们可以使用StringBuffer或创建可变的String StringBuilder.现在,如果我们在StringBuilder对象中有一个String并且我们使用toString()它的方法,那么它将在String池中创建另一个不可变的String.如果是这样,那么如何避免这种情况.

java

4
推荐指数
2
解决办法
414
查看次数

不带 ActionBar 的汉堡图标不带透明操作栏

我想在没有操作栏的情况下在我的活动中使用汉堡图标。我已经看到使用透明操作栏执行此操作的示例,但我不希望它是透明的。我实际上确实想添加操作栏,如果我单击汉堡包图标,它应该显示导航抽屉。布局类似于 Uber Android 应用程序。作为参考,我还发布了一张带有红色标记的图像。请帮我实现相同的。在此处输入图片说明

android android-layout android-fragments

2
推荐指数
1
解决办法
2576
查看次数

Thymeleaf 计数器变量不递增

我想在使用 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)

spring counter increment thymeleaf spring-boot

1
推荐指数
1
解决办法
4038
查看次数

Java 1.6中字符串连接的提示,包含多个整数

我有一个有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)

java integer concatenation tostring java-6

0
推荐指数
1
解决办法
65
查看次数