Selaa lähdekoodia

HADOOP-1551. libhdfs supports setting replication factor and
retrieving modification time of files.
Contributed by Sameer Paranjpye.


git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@561432 13f79535-47bb-0310-9956-ffa450edef68

Dhruba Borthakur 18 vuotta sitten
vanhempi
commit
43200c0ad3
5 muutettua tiedostoa jossa 130 lisäystä ja 47 poistoa
  1. 3 0
      CHANGES.txt
  2. 83 33
      src/c++/libhdfs/hdfs.c
  3. 13 2
      src/c++/libhdfs/hdfs.h
  4. 11 0
      src/c++/libhdfs/hdfsJniHelper.c
  5. 20 12
      src/c++/libhdfs/hdfs_test.c

+ 3 - 0
CHANGES.txt

@@ -424,6 +424,9 @@ Branch 0.14 (unreleased changes)
 140. HADOOP-1066.  Restructure documentation to make more user
 140. HADOOP-1066.  Restructure documentation to make more user
      friendly.  (Connie Kleinjans and Jeff Hammerbacher via cutting)
      friendly.  (Connie Kleinjans and Jeff Hammerbacher via cutting)
 
 
+141. HADOOP-1551.  libhdfs supports setting replication factor and
+     retrieving modification time of files.  (Sameer Paranjpye via dhruba)
+
 
 
 Release 0.13.0 - 2007-06-08
 Release 0.13.0 - 2007-06-08
 
 

+ 83 - 33
src/c++/libhdfs/hdfs.c

@@ -28,6 +28,7 @@
 #define HADOOP_DFS      "org/apache/hadoop/dfs/DistributedFileSystem"
 #define HADOOP_DFS      "org/apache/hadoop/dfs/DistributedFileSystem"
 #define HADOOP_ISTRM    "org/apache/hadoop/fs/FSDataInputStream"
 #define HADOOP_ISTRM    "org/apache/hadoop/fs/FSDataInputStream"
 #define HADOOP_OSTRM    "org/apache/hadoop/fs/FSDataOutputStream"
 #define HADOOP_OSTRM    "org/apache/hadoop/fs/FSDataOutputStream"
+#define HADOOP_STAT     "org/apache/hadoop/fs/FileStatus"
 #define JAVA_NET_ISA    "java/net/InetSocketAddress"
 #define JAVA_NET_ISA    "java/net/InetSocketAddress"
 #define JAVA_NET_URI    "java/net/URI"
 #define JAVA_NET_URI    "java/net/URI"
 
 
@@ -1076,6 +1077,42 @@ int hdfsCreateDirectory(hdfsFS fs, const char* path)
 }
 }
 
 
 
 
+int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication)
+{
+    // JAVA EQUIVALENT:
+    //  fs.setReplication(new Path(path), replication);
+
+    //Get the JNIEnv* corresponding to current thread
+    JNIEnv* env = getJNIEnv();
+
+    jobject jFS = (jobject)fs;
+
+    //Create an object of org.apache.hadoop.fs.Path
+    jobject jPath = constructNewObjectOfPath(env, path);
+    if (jPath == NULL) {
+        return -1;
+    }
+
+    //Create the directory
+    jvalue jVal;
+    if (invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
+                     "setReplication", "(Lorg/apache/hadoop/fs/Path;S)Z",
+                     jPath, replication) != 0) {
+        fprintf(stderr, "Call to org.apache.hadoop.fs.FileSystem::"
+                "setReplication failed!\n");
+        errno = EINTERNAL;
+        goto done;
+    }
+
+ done:
+
+    //Delete unnecessary local references
+    destroyLocalReference(env, jPath);
+
+    return (jVal.z) ? 0 : -1;
+}
+
+
 
 
 char***
 char***
 hdfsGetHosts(hdfsFS fs, const char* path, tOffset start, tOffset length)
 hdfsGetHosts(hdfsFS fs, const char* path, tOffset start, tOffset length)
@@ -1287,8 +1324,8 @@ getFileInfo(JNIEnv *env, jobject jFS, jobject jPath, hdfsFileInfo *fileInfo)
     //  fs.getLength(f)
     //  fs.getLength(f)
     //  f.getPath()
     //  f.getPath()
 
 
-    jboolean jIsDir;
-    jvalue jVal;
+    jobject jStat;
+    jvalue  jVal;
 
 
     if (invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
     if (invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
                      "exists", JMETHOD1(JPARAM(HADOOP_PATH), "Z"),
                      "exists", JMETHOD1(JPARAM(HADOOP_PATH), "Z"),
@@ -1300,47 +1337,69 @@ getFileInfo(JNIEnv *env, jobject jFS, jobject jPath, hdfsFileInfo *fileInfo)
     }
     }
 
 
     if (jVal.z == 0) {
     if (jVal.z == 0) {
-      errno = EINTERNAL;
+      errno = ENOENT;
       return -1;
       return -1;
     }
     }
 
 
     if (invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
     if (invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
-                     "isDirectory", "(Lorg/apache/hadoop/fs/Path;)Z",
+                     "getFileStatus", JMETHOD1(JPARAM(HADOOP_PATH), JPARAM(HADOOP_STAT)),
                      jPath) != 0) {
                      jPath) != 0) {
         fprintf(stderr, "Call to org.apache.hadoop.fs."
         fprintf(stderr, "Call to org.apache.hadoop.fs."
-                "FileSystem::isDirectory failed!\n");
+                "FileSystem::getFileStatus failed!\n");
         errno = EINTERNAL;
         errno = EINTERNAL;
         return -1;
         return -1;
     }
     }
-    jIsDir = jVal.z;
+    jStat = jVal.l;
 
 
-    /*
-    jlong jModTime = 0;
-    if (invokeMethod(env, (RetVal*)&jModTime, &jException, INSTANCE, jFS, 
-                "org/apache/hadoop/fs/FileSystem", "lastModified", 
-                "(Lorg/apache/hadoop/fs/Path;)J", jPath) != 0) {
-        fprintf(stderr, 
-              "Call to org.apache.hadoop.fs.FileSystem::lastModified failed!\n"
-                );
+    if (invokeMethod(env, &jVal, INSTANCE, jStat,
+                     HADOOP_STAT, "isDir", "()Z") != 0) {
+        fprintf(stderr, "Call to org.apache.hadoop.fs."
+                "FileStatus::isDir failed!\n");
         errno = EINTERNAL;
         errno = EINTERNAL;
         return -1;
         return -1;
     }
     }
-    */
+    fileInfo->mKind = jVal.z ? kObjectKindDirectory : kObjectKindFile;
 
 
-    jlong jFileLength = 0;
-    if (!jIsDir) {
-        if (invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
-                         "getLength", "(Lorg/apache/hadoop/fs/Path;)J",
-                         jPath) != 0) {
+    if (invokeMethod(env, &jVal, INSTANCE, jStat,
+                     HADOOP_STAT, "getReplication", "()S") != 0) {
+        fprintf(stderr, "Call to org.apache.hadoop.fs."
+                "FileStatus::getReplication failed!\n");
+        errno = EINTERNAL;
+        return -1;
+    }
+    fileInfo->mReplication = jVal.s;
+
+    if (invokeMethod(env, &jVal, INSTANCE, jStat,
+                     HADOOP_STAT, "getBlockSize", "()J") != 0) {
+        fprintf(stderr, "Call to org.apache.hadoop.fs."
+                "FileStatus::getBlockSize failed!\n");
+        errno = EINTERNAL;
+        return -1;
+    }
+    fileInfo->mBlockSize = jVal.j;
+
+    if (invokeMethod(env, &jVal, INSTANCE, jStat,
+                     HADOOP_STAT, "getModificationTime", "()J") != 0) {
+        fprintf(stderr, "Call to org.apache.hadoop.fs."
+                "FileStatus::getModificationTime failed!\n");
+        errno = EINTERNAL;
+        return -1;
+    }
+    fileInfo->mLastMod = (tTime) (jVal.j / 1000);
+
+    if (fileInfo->mKind == kObjectKindFile) {
+        if (invokeMethod(env, &jVal, INSTANCE, jStat,
+                         HADOOP_STAT, "getLen", "()J") != 0) {
             fprintf(stderr, "Call to org.apache.hadoop.fs."
             fprintf(stderr, "Call to org.apache.hadoop.fs."
-                    "FileSystem::getLength failed!\n");
+                    "FileStatus::getLen failed!\n");
             errno = EINTERNAL;
             errno = EINTERNAL;
             return -1;
             return -1;
         }
         }
-        jFileLength = jVal.j;
+        fileInfo->mSize = jVal.j;
     }
     }
 
 
-    jstring jPathName;
+    jstring     jPathName;
+    const char *cPathName;
     if (invokeMethod(env, &jVal, INSTANCE, jPath, HADOOP_PATH,
     if (invokeMethod(env, &jVal, INSTANCE, jPath, HADOOP_PATH,
                      "toString", "()Ljava/lang/String;")) { 
                      "toString", "()Ljava/lang/String;")) { 
         fprintf(stderr, "Call to org.apache.hadoop.fs."
         fprintf(stderr, "Call to org.apache.hadoop.fs."
@@ -1349,18 +1408,9 @@ getFileInfo(JNIEnv *env, jobject jFS, jobject jPath, hdfsFileInfo *fileInfo)
         return -1;
         return -1;
     }
     }
     jPathName = jVal.l;
     jPathName = jVal.l;
-
-    fileInfo->mKind = (jIsDir ? kObjectKindDirectory : kObjectKindFile);
-    //fileInfo->mCreationTime = jModTime;
-    fileInfo->mSize = jFileLength;
-
-    const char* cPathName = (const char*)
-      ((*env)->GetStringUTFChars(env, jPathName, NULL));
-
+    cPathName = (const char*) ((*env)->GetStringUTFChars(env, jPathName, NULL));
     fileInfo->mName = strdup(cPathName);
     fileInfo->mName = strdup(cPathName);
-
     (*env)->ReleaseStringUTFChars(env, jPathName, cPathName);
     (*env)->ReleaseStringUTFChars(env, jPathName, cPathName);
-
     destroyLocalReference(env, jPathName);
     destroyLocalReference(env, jPathName);
 
 
     return 0;
     return 0;

+ 13 - 2
src/c++/libhdfs/hdfs.h

@@ -298,15 +298,26 @@ extern  "C" {
     int hdfsCreateDirectory(hdfsFS fs, const char* path);
     int hdfsCreateDirectory(hdfsFS fs, const char* path);
 
 
 
 
+    /** 
+     * hdfsSetReplication - Set the replication of the specified
+     * file to the supplied value
+     * @param fs The configured filesystem handle.
+     * @param path The path of the file. 
+     * @return Returns 0 on success, -1 on error. 
+     */
+    int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication);
+
+
     /** 
     /** 
      * hdfsFileInfo - Information about a file/directory.
      * hdfsFileInfo - Information about a file/directory.
      */
      */
     typedef struct  {
     typedef struct  {
         tObjectKind mKind;   /* file or directory */
         tObjectKind mKind;   /* file or directory */
         char *mName;         /* the name of the file */
         char *mName;         /* the name of the file */
-        tTime mCreationTime; /* the creation time for the file*/
+        tTime mLastMod;      /* the last modification time for the file*/
         tOffset mSize;       /* the size of the file in bytes */
         tOffset mSize;       /* the size of the file in bytes */
-        int replicaCount;    /* the count of replicas */
+        short mReplication;    /* the count of replicas */
+        tOffset mBlockSize;  /* the block size for the file */
     } hdfsFileInfo;
     } hdfsFileInfo;
 
 
 
 

+ 11 - 0
src/c++/libhdfs/hdfsJniHelper.c

@@ -175,6 +175,17 @@ int invokeMethod(JNIEnv *env, RetVal *retval, MethType methType,
         CHECK_EXCEPTION_IN_METH_INVOC
         CHECK_EXCEPTION_IN_METH_INVOC
         retval->z = jbool;
         retval->z = jbool;
     }
     }
+    else if (returnType == JSHORT) {
+        jshort js = 0;
+        if (methType == STATIC) {
+            js = (*env)->CallStaticShortMethodV(env, cls, mid, args);
+        }
+        else if (methType == INSTANCE) {
+            js = (*env)->CallShortMethodV(env, instObj, mid, args);
+        }
+        CHECK_EXCEPTION_IN_METH_INVOC
+        retval->s = js;
+    }
     else if (returnType == JLONG) {
     else if (returnType == JLONG) {
         jlong jl = -1;
         jlong jl = -1;
         if (methType == STATIC) {
         if (methType == STATIC) {

+ 20 - 12
src/c++/libhdfs/hdfs_test.c

@@ -120,9 +120,7 @@ int main(int argc, char **argv) {
         //Generic file-system operations
         //Generic file-system operations
 
 
         const char* srcPath = "/tmp/testfile.txt";
         const char* srcPath = "/tmp/testfile.txt";
-        const char* localSrcPath = "testfile.txt";
         const char* dstPath = "/tmp/testfile2.txt";
         const char* dstPath = "/tmp/testfile2.txt";
-        const char* localDstPath = "testfile2.txt";
 
 
         fprintf(stderr, "hdfsCopy(remote-local): %s\n", ((result = hdfsCopy(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
         fprintf(stderr, "hdfsCopy(remote-local): %s\n", ((result = hdfsCopy(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
         totalResult += result;
         totalResult += result;
@@ -143,13 +141,17 @@ int main(int argc, char **argv) {
         fprintf(stderr, "hdfsCreateDirectory: %s\n", ((result = hdfsCreateDirectory(fs, newDirectory)) ? "Failed!" : "Success!"));
         fprintf(stderr, "hdfsCreateDirectory: %s\n", ((result = hdfsCreateDirectory(fs, newDirectory)) ? "Failed!" : "Success!"));
         totalResult += result;
         totalResult += result;
 
 
+        fprintf(stderr, "hdfsSetReplication: %s\n", ((result = hdfsSetReplication(fs, srcPath, 2)) ? "Failed!" : "Success!"));
+        totalResult += result;
+
         char buffer[256];
         char buffer[256];
-        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((result = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
-        totalResult += (result ? 0 : 1);
+        const char *resp;
+        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
+        totalResult += (resp ? 0 : 1);
         fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ? "Failed!" : "Success!"));
         fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ? "Failed!" : "Success!"));
         totalResult += result;
         totalResult += result;
-        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((result = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
-        totalResult += (result ? 0 : 1);
+        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
+        totalResult += (resp ? 0 : 1);
 
 
         fprintf(stderr, "hdfsGetDefaultBlockSize: %Ld\n", hdfsGetDefaultBlockSize(fs));
         fprintf(stderr, "hdfsGetDefaultBlockSize: %Ld\n", hdfsGetDefaultBlockSize(fs));
         fprintf(stderr, "hdfsGetCapacity: %Ld\n", hdfsGetCapacity(fs));
         fprintf(stderr, "hdfsGetCapacity: %Ld\n", hdfsGetCapacity(fs));
@@ -158,9 +160,12 @@ int main(int argc, char **argv) {
         hdfsFileInfo *fileInfo = NULL;
         hdfsFileInfo *fileInfo = NULL;
         if(fileInfo = hdfsGetPathInfo(fs, slashTmp)) {
         if(fileInfo = hdfsGetPathInfo(fs, slashTmp)) {
             fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
             fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
-            fprintf(stderr, "Name: %s,", fileInfo->mName);
-            fprintf(stderr, "Type: %c,", (char)fileInfo->mKind);
-            fprintf(stderr, "Size: %ld\n", fileInfo->mSize);
+            fprintf(stderr, "Name: %s, ", fileInfo->mName);
+            fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
+            fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
+            fprintf(stderr, "BlockSize: %ld, ", fileInfo->mBlockSize);
+            fprintf(stderr, "Size: %ld, ", fileInfo->mSize);
+            fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod)); 
             hdfsFreeFileInfo(fileInfo, 1);
             hdfsFreeFileInfo(fileInfo, 1);
         } else {
         } else {
             totalResult++;
             totalResult++;
@@ -172,9 +177,12 @@ int main(int argc, char **argv) {
         if(fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) {
         if(fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) {
             int i = 0;
             int i = 0;
             for(i=0; i < numEntries; ++i) {
             for(i=0; i < numEntries; ++i) {
-                fprintf(stderr, "Name: %s,", fileList[i].mName);
-                fprintf(stderr, "Type: %c,", (char)fileList[i].mKind);
-                fprintf(stderr, "Size: %ld\n", fileList[i].mSize);
+                fprintf(stderr, "Name: %s, ", fileList[i].mName);
+                fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
+                fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
+                fprintf(stderr, "BlockSize: %ld, ", fileList[i].mBlockSize);
+                fprintf(stderr, "Size: %ld, ", fileList[i].mSize);
+                fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
             }
             }
             hdfsFreeFileInfo(fileList, numEntries);
             hdfsFreeFileInfo(fileList, numEntries);
         } else {
         } else {