Example usage for org.apache.hadoop.fs FileSystem isFile

List of usage examples for org.apache.hadoop.fs FileSystem isFile

Introduction

In this page you can find the example usage for org.apache.hadoop.fs FileSystem isFile.

Prototype

@Deprecated
public boolean isFile(Path f) throws IOException 

Source Link

Document

True iff the named path is a regular file.

Usage

From source file:org.apache.pig.piggybank.test.storage.TestMultiStorage.java

License:Apache License

/**
 * Test if records are split into directories corresponding to split field
 * values/* w  w  w  . j av  a 2s. com*/
 * 
 * @param mode
 * @throws IOException
 */
private void verifyResults(Mode mode, String outPath) throws IOException {
    FileSystem fs = (Mode.local == mode ? FileSystem.getLocal(new Configuration()) : cluster.getFileSystem());
    Path output = new Path(outPath);
    Assert.assertTrue("Output dir does not exists!", fs.exists(output) && fs.getFileStatus(output).isDir());

    Path[] paths = FileUtil.stat2Paths(fs.listStatus(output, hiddenPathFilter));
    Assert.assertTrue("Split field dirs not found!", paths != null);

    for (Path path : paths) {
        String splitField = path.getName();
        Path[] files = FileUtil.stat2Paths(fs.listStatus(path, hiddenPathFilter));
        Assert.assertTrue("No files found for path: " + path.toUri().getPath(), files != null);
        for (Path filePath : files) {
            Assert.assertTrue("This shouldn't be a directory", fs.isFile(filePath));

            BufferedReader reader = new BufferedReader(new InputStreamReader(fs.open(filePath)));
            String line = "";
            int count = 0;
            while ((line = reader.readLine()) != null) {
                String[] fields = line.split("\\t");
                Assert.assertEquals(fields.length, 3);
                Assert.assertEquals("Unexpected field value in the output record", splitField, fields[1]);
                count++;
                System.out.println("field: " + fields[1]);
            }
            reader.close();
            Assert.assertEquals(count, 3);
        }
    }
}

From source file:org.apache.rya.reasoning.mr.MRReasoningUtils.java

License:Apache License

/**
 * Delete an HDFS directory if it exists
 *///from w w  w . j  a v a 2  s.co  m
static void deleteIfExists(Configuration conf, String rel) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path path = getOutputPath(conf, rel);
    if (fs.isDirectory(path) || fs.isFile(path)) {
        fs.delete(path, true);
    }
}

From source file:org.apache.sqoop.connector.hdfs.HdfsToInitializer.java

License:Apache License

/**
 * {@inheritDoc}//from   w  w w.  j a v a  2  s . c  o m
 */
@Override
public void initialize(InitializerContext context, LinkConfiguration linkConfig, ToJobConfiguration jobConfig) {
    assert jobConfig != null;
    assert linkConfig != null;
    assert jobConfig.toJobConfig != null;
    assert jobConfig.toJobConfig.outputDirectory != null;

    Configuration configuration = HdfsUtils.createConfiguration(linkConfig);
    HdfsUtils.configurationToContext(configuration, context.getContext());
    boolean appendMode = Boolean.TRUE.equals(jobConfig.toJobConfig.appendMode);

    // Verification that given HDFS directory either don't exists or is empty
    try {
        FileSystem fs = FileSystem.get(configuration);
        Path path = new Path(jobConfig.toJobConfig.outputDirectory);

        if (fs.exists(path)) {
            if (fs.isFile(path)) {
                throw new SqoopException(HdfsConnectorError.GENERIC_HDFS_CONNECTOR_0007,
                        "Output directory already exists and is a file");
            }

            if (fs.isDirectory(path) && !appendMode) {
                FileStatus[] fileStatuses = fs.listStatus(path);
                if (fileStatuses.length != 0) {
                    throw new SqoopException(HdfsConnectorError.GENERIC_HDFS_CONNECTOR_0007,
                            "Output directory is not empty");
                }
            }
        }
    } catch (IOException e) {
        throw new SqoopException(HdfsConnectorError.GENERIC_HDFS_CONNECTOR_0007, "Unexpected exception", e);
    }

    // Building working directory
    String workingDirectory = jobConfig.toJobConfig.outputDirectory + "/." + UUID.randomUUID();
    LOG.info("Using working directory: " + workingDirectory);
    context.getContext().setString(HdfsConstants.WORK_DIRECTORY, workingDirectory);
}

From source file:org.apache.sqoop.util.password.FilePasswordLoader.java

License:Apache License

/**
 * Verify that given path leads to a file that we can read.
 *
 * @param fs Associated FileSystem//from w ww  . ja  v a 2  s  .c om
 * @param path Path
 * @throws IOException
 */
protected void verifyPath(FileSystem fs, Path path) throws IOException {
    if (!fs.exists(path)) {
        throw new IOException("The password file does not exist! " + path);
    }

    if (!fs.isFile(path)) {
        throw new IOException("The password file cannot be a directory! " + path);
    }
}

From source file:org.apache.sysml.runtime.io.WriterMatrixMarket.java

License:Apache License

public static void mergeTextcellToMatrixMarket(String srcFileName, String fileName, long rlen, long clen,
        long nnz) throws IOException {
    Configuration conf = new Configuration(ConfigurationManager.getCachedJobConf());

    Path src = new Path(srcFileName);
    Path merge = new Path(fileName);
    FileSystem fs = IOUtilFunctions.getFileSystem(src, conf);

    if (fs.exists(merge)) {
        fs.delete(merge, true);/* w  w w .j a v a  2s . com*/
    }

    OutputStream out = fs.create(merge, true);

    // write out the header first 
    StringBuilder sb = new StringBuilder();
    sb.append("%%MatrixMarket matrix coordinate real general\n");

    // output number of rows, number of columns and number of nnz
    sb.append(rlen + " " + clen + " " + nnz + "\n");
    out.write(sb.toString().getBytes());

    // if the source is a directory
    if (fs.getFileStatus(src).isDirectory()) {
        try {
            FileStatus[] contents = fs.listStatus(src);
            for (int i = 0; i < contents.length; i++) {
                if (!contents[i].isDirectory()) {
                    InputStream in = fs.open(contents[i].getPath());
                    try {
                        IOUtils.copyBytes(in, out, conf, false);
                    } finally {
                        IOUtilFunctions.closeSilently(in);
                    }
                }
            }
        } finally {
            IOUtilFunctions.closeSilently(out);
        }
    } else if (fs.isFile(src)) {
        InputStream in = null;
        try {
            in = fs.open(src);
            IOUtils.copyBytes(in, out, conf, true);
        } finally {
            IOUtilFunctions.closeSilently(in);
            IOUtilFunctions.closeSilently(out);
        }
    } else {
        throw new IOException(src.toString() + ": No such file or directory");
    }
}

From source file:org.apache.sysml.runtime.io.WriterTextCSV.java

License:Apache License

@SuppressWarnings("unchecked")
public final void addHeaderToCSV(String srcFileName, String destFileName, long rlen, long clen)
        throws IOException {
    Configuration conf = new Configuration(ConfigurationManager.getCachedJobConf());

    Path srcFilePath = new Path(srcFileName);
    Path destFilePath = new Path(destFileName);
    FileSystem fs = IOUtilFunctions.getFileSystem(srcFilePath, conf);

    if (!_props.hasHeader()) {
        // simply move srcFile to destFile

        /*//  w w w .  ja v a  2s . c  o m
         * TODO: Remove this roundabout way! 
         * For example: destFilePath = /user/biadmin/csv/temp/out/file.csv 
         *              & the only path that exists already on HDFS is /user/biadmin/csv/.
         * In this case: the directory structure /user/biadmin/csv/temp/out must be created. 
         * Simple hdfs.rename() does not seem to create this directory structure.
         */

        // delete the destination file, if exists already
        fs.delete(destFilePath, true);

        // Create /user/biadmin/csv/temp/out/file.csv so that ..../temp/out/ is created.
        fs.createNewFile(destFilePath);

        // delete the file "file.csv" but preserve the directory structure /user/biadmin/csv/temp/out/
        fs.delete(destFilePath, true);

        // finally, move the data to destFilePath = /user/biadmin/csv/temp/out/file.csv
        fs.rename(srcFilePath, destFilePath);

        return;
    }

    // construct the header line
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < clen; i++) {
        sb.append("C" + (i + 1));
        if (i < clen - 1)
            sb.append(_props.getDelim());
    }
    sb.append('\n');

    if (fs.isDirectory(srcFilePath)) {

        // compute sorted order among part files
        ArrayList<Path> files = new ArrayList<>();
        for (FileStatus stat : fs.listStatus(srcFilePath, CSVReblockMR.hiddenFileFilter))
            files.add(stat.getPath());
        Collections.sort(files);

        // first part file path
        Path firstpart = files.get(0);

        // create a temp file, and add header and contents of first part
        Path tmp = new Path(firstpart.toString() + ".tmp");
        OutputStream out = fs.create(tmp, true);
        out.write(sb.toString().getBytes());
        sb.setLength(0);

        // copy rest of the data from firstpart
        InputStream in = null;
        try {
            in = fs.open(firstpart);
            IOUtils.copyBytes(in, out, conf, true);
        } finally {
            IOUtilFunctions.closeSilently(in);
            IOUtilFunctions.closeSilently(out);
        }

        // rename tmp to firstpart
        fs.delete(firstpart, true);
        fs.rename(tmp, firstpart);

        // rename srcfile to destFile
        fs.delete(destFilePath, true);
        fs.createNewFile(destFilePath); // force the creation of directory structure
        fs.delete(destFilePath, true); // delete the file, but preserve the directory structure
        fs.rename(srcFilePath, destFilePath); // move the data 

    } else if (fs.isFile(srcFilePath)) {
        // create destination file
        OutputStream out = fs.create(destFilePath, true);

        // write header
        out.write(sb.toString().getBytes());
        sb.setLength(0);

        // copy the data from srcFile
        InputStream in = null;
        try {
            in = fs.open(srcFilePath);
            IOUtils.copyBytes(in, out, conf, true);
        } finally {
            IOUtilFunctions.closeSilently(in);
            IOUtilFunctions.closeSilently(out);
        }
    } else {
        throw new IOException(srcFilePath.toString() + ": No such file or directory");
    }
}

From source file:org.apache.tajo.catalog.store.HCatalogStore.java

License:Apache License

@Override
public final void createTable(final CatalogProtos.TableDescProto tableDescProto) throws CatalogException {
    HCatalogStoreClientPool.HCatalogStoreClient client = null;

    TableDesc tableDesc = new TableDesc(tableDescProto);
    String[] splitted = CatalogUtil.splitFQTableName(tableDesc.getName());
    String databaseName = splitted[0];
    String tableName = splitted[1];

    try {//  w  w  w. j  a  v a 2  s .  c  om
        client = clientPool.getClient();

        org.apache.hadoop.hive.metastore.api.Table table = new org.apache.hadoop.hive.metastore.api.Table();
        table.setDbName(databaseName);
        table.setTableName(tableName);
        table.setParameters(new HashMap<String, String>(tableDesc.getMeta().getOptions().getAllKeyValus()));
        // TODO: set owner
        //table.setOwner();

        StorageDescriptor sd = new StorageDescriptor();
        sd.setSerdeInfo(new SerDeInfo());
        sd.getSerdeInfo().setParameters(new HashMap<String, String>());
        sd.getSerdeInfo().setName(table.getTableName());

        // if tajo set location method, thrift client make exception as follows:
        // Caused by: MetaException(message:java.lang.NullPointerException)
        // If you want to modify table path, you have to modify on Hive cli.
        if (tableDesc.isExternal()) {
            table.setTableType(TableType.EXTERNAL_TABLE.name());
            table.putToParameters("EXTERNAL", "TRUE");

            Path tablePath = new Path(tableDesc.getPath());
            FileSystem fs = tablePath.getFileSystem(conf);
            if (fs.isFile(tablePath)) {
                LOG.warn("A table path is a file, but HCatalog does not allow a file path.");
                sd.setLocation(tablePath.getParent().toString());
            } else {
                sd.setLocation(tablePath.toString());
            }
        }

        // set column information
        List<Column> columns = tableDesc.getSchema().getColumns();
        ArrayList<FieldSchema> cols = new ArrayList<FieldSchema>(columns.size());

        for (Column eachField : columns) {
            cols.add(new FieldSchema(eachField.getSimpleName(),
                    HCatalogUtil.getHiveFieldType(eachField.getDataType()), ""));
        }
        sd.setCols(cols);

        // set partition keys
        if (tableDesc.hasPartition()
                && tableDesc.getPartitionMethod().getPartitionType().equals(PartitionType.COLUMN)) {
            List<FieldSchema> partitionKeys = new ArrayList<FieldSchema>();
            for (Column eachPartitionKey : tableDesc.getPartitionMethod().getExpressionSchema().getColumns()) {
                partitionKeys.add(new FieldSchema(eachPartitionKey.getSimpleName(),
                        HCatalogUtil.getHiveFieldType(eachPartitionKey.getDataType()), ""));
            }
            table.setPartitionKeys(partitionKeys);
        }

        if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.RCFILE)) {
            String serde = tableDesc.getMeta().getOption(StorageConstants.RCFILE_SERDE);
            sd.setInputFormat(org.apache.hadoop.hive.ql.io.RCFileInputFormat.class.getName());
            sd.setOutputFormat(org.apache.hadoop.hive.ql.io.RCFileOutputFormat.class.getName());
            if (StorageConstants.DEFAULT_TEXT_SERDE.equals(serde)) {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe.class.getName());
            } else {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe.class.getName());
            }

            if (tableDesc.getMeta().getOptions().containsKey(StorageConstants.RCFILE_NULL)) {
                table.putToParameters(serdeConstants.SERIALIZATION_NULL_FORMAT, StringEscapeUtils
                        .unescapeJava(tableDesc.getMeta().getOption(StorageConstants.RCFILE_NULL)));
            }
        } else if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.CSV)
                || tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.TEXTFILE)) {
            sd.getSerdeInfo()
                    .setSerializationLib(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.class.getName());
            sd.setInputFormat(org.apache.hadoop.mapred.TextInputFormat.class.getName());
            sd.setOutputFormat(org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat.class.getName());

            String fieldDelimiter = tableDesc.getMeta().getOption(StorageConstants.TEXT_DELIMITER,
                    StorageConstants.DEFAULT_FIELD_DELIMITER);

            // User can use an unicode for filed delimiter such as \u0001, \001.
            // In this case, java console will convert this value into "\\u001".
            // And hive will un-espace this value again.
            // As a result, user can use right field delimiter.
            // So, we have to un-escape this value.
            sd.getSerdeInfo().putToParameters(serdeConstants.SERIALIZATION_FORMAT,
                    StringEscapeUtils.unescapeJava(fieldDelimiter));
            sd.getSerdeInfo().putToParameters(serdeConstants.FIELD_DELIM,
                    StringEscapeUtils.unescapeJava(fieldDelimiter));
            table.getParameters().remove(StorageConstants.TEXT_DELIMITER);

            if (tableDesc.getMeta().containsOption(StorageConstants.TEXT_NULL)) {
                table.putToParameters(serdeConstants.SERIALIZATION_NULL_FORMAT, StringEscapeUtils
                        .unescapeJava(tableDesc.getMeta().getOption(StorageConstants.TEXT_NULL)));
                table.getParameters().remove(StorageConstants.TEXT_NULL);
            }
        } else if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.SEQUENCEFILE)) {
            String serde = tableDesc.getMeta().getOption(StorageConstants.SEQUENCEFILE_SERDE);
            sd.setInputFormat(org.apache.hadoop.mapred.SequenceFileInputFormat.class.getName());
            sd.setOutputFormat(org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat.class.getName());

            if (StorageConstants.DEFAULT_TEXT_SERDE.equals(serde)) {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.class.getName());

                String fieldDelimiter = tableDesc.getMeta().getOption(StorageConstants.SEQUENCEFILE_DELIMITER,
                        StorageConstants.DEFAULT_FIELD_DELIMITER);

                // User can use an unicode for filed delimiter such as \u0001, \001.
                // In this case, java console will convert this value into "\\u001".
                // And hive will un-espace this value again.
                // As a result, user can use right field delimiter.
                // So, we have to un-escape this value.
                sd.getSerdeInfo().putToParameters(serdeConstants.SERIALIZATION_FORMAT,
                        StringEscapeUtils.unescapeJava(fieldDelimiter));
                sd.getSerdeInfo().putToParameters(serdeConstants.FIELD_DELIM,
                        StringEscapeUtils.unescapeJava(fieldDelimiter));
                table.getParameters().remove(StorageConstants.SEQUENCEFILE_DELIMITER);
            } else {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.lazybinary.LazyBinarySerDe.class.getName());
            }

            if (tableDesc.getMeta().containsOption(StorageConstants.SEQUENCEFILE_NULL)) {
                table.putToParameters(serdeConstants.SERIALIZATION_NULL_FORMAT, StringEscapeUtils
                        .unescapeJava(tableDesc.getMeta().getOption(StorageConstants.SEQUENCEFILE_NULL)));
                table.getParameters().remove(StorageConstants.SEQUENCEFILE_NULL);
            }
        } else {
            if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.PARQUET)) {
                sd.setInputFormat(parquet.hive.DeprecatedParquetInputFormat.class.getName());
                sd.setOutputFormat(parquet.hive.DeprecatedParquetOutputFormat.class.getName());
                sd.getSerdeInfo().setSerializationLib(parquet.hive.serde.ParquetHiveSerDe.class.getName());
            } else {
                throw new CatalogException(
                        new NotImplementedException(tableDesc.getMeta().getStoreType().name()));
            }
        }

        sd.setSortCols(new ArrayList<Order>());

        table.setSd(sd);
        client.getHiveClient().createTable(table);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CatalogException(e);
    } finally {
        if (client != null)
            client.release();
    }
}

From source file:org.apache.tajo.catalog.store.HiveCatalogStore.java

License:Apache License

@Override
public final void createTable(final CatalogProtos.TableDescProto tableDescProto) throws CatalogException {
    HiveCatalogStoreClientPool.HiveCatalogStoreClient client = null;

    TableDesc tableDesc = new TableDesc(tableDescProto);
    String[] splitted = CatalogUtil.splitFQTableName(tableDesc.getName());
    String databaseName = splitted[0];
    String tableName = splitted[1];

    try {//w  ww .ja  v a2 s . c  o m
        client = clientPool.getClient();

        org.apache.hadoop.hive.metastore.api.Table table = new org.apache.hadoop.hive.metastore.api.Table();
        table.setDbName(databaseName);
        table.setTableName(tableName);
        table.setParameters(new HashMap<String, String>(tableDesc.getMeta().getOptions().getAllKeyValus()));
        // TODO: set owner
        //table.setOwner();

        StorageDescriptor sd = new StorageDescriptor();
        sd.setSerdeInfo(new SerDeInfo());
        sd.getSerdeInfo().setParameters(new HashMap<String, String>());
        sd.getSerdeInfo().setName(table.getTableName());

        // if tajo set location method, thrift client make exception as follows:
        // Caused by: MetaException(message:java.lang.NullPointerException)
        // If you want to modify table path, you have to modify on Hive cli.
        if (tableDesc.isExternal()) {
            table.setTableType(TableType.EXTERNAL_TABLE.name());
            table.putToParameters("EXTERNAL", "TRUE");

            Path tablePath = new Path(tableDesc.getPath());
            FileSystem fs = tablePath.getFileSystem(conf);
            if (fs.isFile(tablePath)) {
                LOG.warn("A table path is a file, but HiveCatalogStore does not allow a file path.");
                sd.setLocation(tablePath.getParent().toString());
            } else {
                sd.setLocation(tablePath.toString());
            }
        }

        // set column information
        List<Column> columns = tableDesc.getSchema().getColumns();
        ArrayList<FieldSchema> cols = new ArrayList<FieldSchema>(columns.size());

        for (Column eachField : columns) {
            cols.add(new FieldSchema(eachField.getSimpleName(),
                    HiveCatalogUtil.getHiveFieldType(eachField.getDataType()), ""));
        }
        sd.setCols(cols);

        // set partition keys
        if (tableDesc.hasPartition()
                && tableDesc.getPartitionMethod().getPartitionType().equals(PartitionType.COLUMN)) {
            List<FieldSchema> partitionKeys = new ArrayList<FieldSchema>();
            for (Column eachPartitionKey : tableDesc.getPartitionMethod().getExpressionSchema().getColumns()) {
                partitionKeys.add(new FieldSchema(eachPartitionKey.getSimpleName(),
                        HiveCatalogUtil.getHiveFieldType(eachPartitionKey.getDataType()), ""));
            }
            table.setPartitionKeys(partitionKeys);
        }

        if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.RCFILE)) {
            String serde = tableDesc.getMeta().getOption(StorageConstants.RCFILE_SERDE);
            sd.setInputFormat(org.apache.hadoop.hive.ql.io.RCFileInputFormat.class.getName());
            sd.setOutputFormat(org.apache.hadoop.hive.ql.io.RCFileOutputFormat.class.getName());
            if (StorageConstants.DEFAULT_TEXT_SERDE.equals(serde)) {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe.class.getName());
            } else {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe.class.getName());
            }

            if (tableDesc.getMeta().getOptions().containsKey(StorageConstants.RCFILE_NULL)) {
                table.putToParameters(serdeConstants.SERIALIZATION_NULL_FORMAT, StringEscapeUtils
                        .unescapeJava(tableDesc.getMeta().getOption(StorageConstants.RCFILE_NULL)));
            }
        } else if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.CSV)
                || tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.TEXTFILE)) {
            sd.getSerdeInfo()
                    .setSerializationLib(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.class.getName());
            sd.setInputFormat(org.apache.hadoop.mapred.TextInputFormat.class.getName());
            sd.setOutputFormat(org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat.class.getName());

            String fieldDelimiter = tableDesc.getMeta().getOption(StorageConstants.TEXT_DELIMITER,
                    StorageConstants.DEFAULT_FIELD_DELIMITER);

            // User can use an unicode for filed delimiter such as \u0001, \001.
            // In this case, java console will convert this value into "\\u001".
            // And hive will un-espace this value again.
            // As a result, user can use right field delimiter.
            // So, we have to un-escape this value.
            sd.getSerdeInfo().putToParameters(serdeConstants.SERIALIZATION_FORMAT,
                    StringEscapeUtils.unescapeJava(fieldDelimiter));
            sd.getSerdeInfo().putToParameters(serdeConstants.FIELD_DELIM,
                    StringEscapeUtils.unescapeJava(fieldDelimiter));
            table.getParameters().remove(StorageConstants.TEXT_DELIMITER);

            if (tableDesc.getMeta().containsOption(StorageConstants.TEXT_NULL)) {
                table.putToParameters(serdeConstants.SERIALIZATION_NULL_FORMAT, StringEscapeUtils
                        .unescapeJava(tableDesc.getMeta().getOption(StorageConstants.TEXT_NULL)));
                table.getParameters().remove(StorageConstants.TEXT_NULL);
            }
        } else if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.SEQUENCEFILE)) {
            String serde = tableDesc.getMeta().getOption(StorageConstants.SEQUENCEFILE_SERDE);
            sd.setInputFormat(org.apache.hadoop.mapred.SequenceFileInputFormat.class.getName());
            sd.setOutputFormat(org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat.class.getName());

            if (StorageConstants.DEFAULT_TEXT_SERDE.equals(serde)) {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.class.getName());

                String fieldDelimiter = tableDesc.getMeta().getOption(StorageConstants.SEQUENCEFILE_DELIMITER,
                        StorageConstants.DEFAULT_FIELD_DELIMITER);

                // User can use an unicode for filed delimiter such as \u0001, \001.
                // In this case, java console will convert this value into "\\u001".
                // And hive will un-espace this value again.
                // As a result, user can use right field delimiter.
                // So, we have to un-escape this value.
                sd.getSerdeInfo().putToParameters(serdeConstants.SERIALIZATION_FORMAT,
                        StringEscapeUtils.unescapeJava(fieldDelimiter));
                sd.getSerdeInfo().putToParameters(serdeConstants.FIELD_DELIM,
                        StringEscapeUtils.unescapeJava(fieldDelimiter));
                table.getParameters().remove(StorageConstants.SEQUENCEFILE_DELIMITER);
            } else {
                sd.getSerdeInfo().setSerializationLib(
                        org.apache.hadoop.hive.serde2.lazybinary.LazyBinarySerDe.class.getName());
            }

            if (tableDesc.getMeta().containsOption(StorageConstants.SEQUENCEFILE_NULL)) {
                table.putToParameters(serdeConstants.SERIALIZATION_NULL_FORMAT, StringEscapeUtils
                        .unescapeJava(tableDesc.getMeta().getOption(StorageConstants.SEQUENCEFILE_NULL)));
                table.getParameters().remove(StorageConstants.SEQUENCEFILE_NULL);
            }
        } else {
            if (tableDesc.getMeta().getStoreType().equals(CatalogProtos.StoreType.PARQUET)) {
                sd.setInputFormat(parquet.hive.DeprecatedParquetInputFormat.class.getName());
                sd.setOutputFormat(parquet.hive.DeprecatedParquetOutputFormat.class.getName());
                sd.getSerdeInfo().setSerializationLib(parquet.hive.serde.ParquetHiveSerDe.class.getName());
            } else {
                throw new CatalogException(
                        new NotImplementedException(tableDesc.getMeta().getStoreType().name()));
            }
        }

        sd.setSortCols(new ArrayList<Order>());

        table.setSd(sd);
        client.getHiveClient().createTable(table);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CatalogException(e);
    } finally {
        if (client != null)
            client.release();
    }
}

From source file:org.apache.tajo.ha.HdfsServiceTracker.java

License:Apache License

/**
 * Reads a text file stored in HDFS file, and then return all service addresses read from a HDFS file.   *
 *
 * @param conf/*from   ww  w .  j  av a  2s .  c  o m*/
 * @return all service addresses
 * @throws ServiceTrackerException
 */
private static List<String> getAddressElements(TajoConf conf) throws ServiceTrackerException {

    try {
        FileSystem fs = getFileSystem(conf);
        Path activeMasterBaseDir = new Path(TajoConf.getSystemHADir(conf),
                TajoConstants.SYSTEM_HA_ACTIVE_DIR_NAME);

        if (!fs.exists(activeMasterBaseDir)) {
            throw new ServiceTrackerException("No such active master base path: " + activeMasterBaseDir);
        }
        if (!fs.isDirectory(activeMasterBaseDir)) {
            throw new ServiceTrackerException("Active master base path must be a directory.");
        }

        FileStatus[] files = fs.listStatus(activeMasterBaseDir);

        if (files.length < 1) {
            throw new ServiceTrackerException("No active master entry");
        } else if (files.length > 1) {
            throw new ServiceTrackerException("Two or more than active master entries.");
        }

        // We can ensure that there is only one file due to the above assertion.
        Path activeMasterEntry = files[0].getPath();

        if (!fs.isFile(activeMasterEntry)) {
            throw new ServiceTrackerException("Active master entry must be a file, but it is a directory.");
        }

        List<String> addressElements = TUtil.newList();

        addressElements.add(activeMasterEntry.getName().replaceAll("_", ":")); // Add UMBILICAL_RPC_ADDRESS to elements

        FSDataInputStream stream = fs.open(activeMasterEntry);
        String data = stream.readUTF();
        stream.close();

        addressElements.addAll(TUtil.newList(data.split("_"))); // Add remains entries to elements

        // ensure the number of entries
        Preconditions.checkState(addressElements.size() == 5, "Fewer service addresses than necessary.");

        return addressElements;

    } catch (Throwable t) {
        throw new ServiceTrackerException(t);
    }
}

From source file:org.apache.tajo.LocalTajoTestingUtility.java

License:Apache License

public static String getResultText(Class clazz, String fileName) throws IOException {
    FileSystem localFS = FileSystem.getLocal(new Configuration());
    Path path = getResultPath(clazz, fileName);
    Preconditions.checkState(localFS.exists(path) && localFS.isFile(path));
    return FileUtil.readTextFile(new File(path.toUri()));
}