/** * Create the table definition to export to, removing any prior table. By specifying * ColumnGenerator arguments, you can add extra columns to the table of arbitrary type. */ public void createTable(ColumnGenerator... extraColumns) throws SQLException { Connection conn = getConnection(); PreparedStatement statement = conn.prepareStatement( getDropTableStatement(getTableName()), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); try { statement.executeUpdate(); conn.commit(); } finally { statement.close(); } StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE "); sb.append(getTableName()); sb.append(" (id INT NOT NULL PRIMARY KEY, msg VARCHAR(64)"); int colNum = 0; for (ColumnGenerator gen : extraColumns) { sb.append(", " + forIdx(colNum++) + " " + gen.getType()); } sb.append(")"); statement = conn.prepareStatement( sb.toString(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); try { statement.executeUpdate(); conn.commit(); } finally { statement.close(); } }
private Record getOdpsRecord( TableTunnel.UploadSession uploadSession, int idx, ColumnGenerator... extraCols) throws ParseException { Record record = uploadSession.newRecord(); record.setBigint("id", (long) idx); record.setString("msg", getMsgPrefix() + idx); int colNum = 0; for (ColumnGenerator generator : extraCols) { String field = forIdx(colNum++); String fieldValue = generator.getExportText(idx); switch (generator.getOdpsType()) { case STRING: record.setString(field, fieldValue); break; case BIGINT: record.setBigint(field, Long.parseLong(fieldValue)); break; case DATETIME: String dateFormat = generator.getDateFormat(); record.setDatetime(field, new SimpleDateFormat(dateFormat).parse(fieldValue)); break; case DOUBLE: record.setDouble(field, Double.parseDouble(fieldValue)); break; case DECIMAL: record.setDecimal(field, new BigDecimal(fieldValue)); break; default: throw new RuntimeException("Unknown column type: " + generator.getOdpsType()); } } return record; }
protected void createOdpsTable( String tableName, String[] partitionCols, ColumnGenerator... extraCols) throws OdpsException { TableSchema schema = new TableSchema(); schema.addColumn(new Column("id", OdpsType.BIGINT)); schema.addColumn(new Column("msg", OdpsType.STRING)); int colNum = 0; for (ColumnGenerator generator : extraCols) { schema.addColumn(new Column(forIdx(colNum++), generator.getOdpsType())); } if (partitionCols != null) { for (String partition : partitionCols) { schema.addPartitionColumn(new Column(partition, OdpsType.STRING)); } } odps.tables().create(tableName, schema); }