示例#1
0
  @Override
  public DDF loadTextFile(DataSourceDescriptor dataSource) throws DDFException {
    String hiveTableName = UUID.randomUUID().toString().replace("-", "_");
    StringBuilder stringBuilder = new StringBuilder();
    List<String> columnNames = new ArrayList<>();
    List<Schema.Column> columnList = dataSource.getDataSourceSchema().getColumns();
    for (int i = 0; i < columnList.size(); ++i) {
      columnNames.add(columnList.get(i).getName());
      if (i == 0) {
        stringBuilder.append(columnList.get(i).getName() + " " + columnList.get(i).getType());
      } else {
        stringBuilder.append(
            ", " + columnList.get(i).getName() + " " + columnList.get(i).getType());
      }
    }
    String schemaStr = stringBuilder.toString();

    TextFileFormat textFileFormat = (TextFileFormat) (dataSource.getFileFormat());
    String quote = textFileFormat.getQuote();
    String delimiter = textFileFormat.getDelimiter();

    String serdesString =
        "ROW FORMAT SERDE 'com.bizo.hive.serde.csv.CSVSerde' "
            + "WITH serdeproperties ('separatorChar' = '"
            + delimiter
            + "', 'quoteChar' = '"
            + quote
            + "')";

    URI uri = dataSource.getDataSourceUri().getUri();
    String sqlCmd =
        "create external table "
            + hiveTableName
            + " ("
            + schemaStr
            + ") "
            + serdesString
            + " STORED AS TEXTFILE LOCATION '"
            + uri.toString()
            + "'";

    this.mDDFManager.sql(sqlCmd, false);
    DDF ddf = this.mDDFManager.sql2ddf(String.format("select * from %s", hiveTableName), false);

    ddf.setColumnNames(columnNames);
    return ddf;
  }