MapReduce执行的结果输出到mysql中

colie 发布于 2015/06/03 10:19
阅读 580
收藏 0

先有一个需求式吧MapReduce的结果输出到Mysql中,现在可以直接插入一条,但是在更新的时候不知道该怎么做,求解答,我继承了DBOutputFormat,重写了constructQuery

public String constructQuery(String table, String[] fieldNames) {
		if (fieldNames == null) {
			throw new IllegalArgumentException("Field names may not be null");
		}
		if (fieldNames.length > 0) {
			System.out.println("fieldNames.length:" + fieldNames.length);
			StringBuilder query = new StringBuilder();
			query.append("INSERT INTO ").append(table).append('(');
			for(String fieldName : fieldNames) {
				query.append(fieldName + ",");
			}
			query.deleteCharAt(query.length() - 1).append(')');
			query.append(" values (");
			for(int i = 0;i<fieldNames.length;i++) {
				query.append("?,");
			}
			query.deleteCharAt(query.length() - 1).append(')');
			query.append(" ON DUPLICATE KEY UPDATE ");
			for(String fieldName : fieldNames) {
				query.append(fieldName + "= ?,");
			}
			query.deleteCharAt(query.length() - 1);
			System.out.println(query.toString());
			return query.toString();
		} else {
			return null;
		}
	}



求解答啊

加载中
返回顶部
顶部