Skip to content

Commit d1664a7

Browse files
Sahil Takiardeepakdamri
authored andcommitted
HDFS-14267. Add test_libhdfs_ops to libhdfs tests, mark libhdfs_read/write.c as examples. Contributed by Sahil Takiar.
Signed-off-by: Wei-Chiu Chuang <[email protected]>
1 parent 5a6d88b commit d1664a7

File tree

7 files changed

+97
-20
lines changed

7 files changed

+97
-20
lines changed

hadoop-hdfs-project/hadoop-hdfs-native-client/src/CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,21 @@ if(WIN32)
5858
# Omit unneeded headers.
5959
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32_LEAN_AND_MEAN")
6060
set(OS_DIR ${CMAKE_SOURCE_DIR}/main/native/libhdfs/os/windows)
61-
set(OUT_DIR target/bin)
61+
62+
# IMPORTANT: OUT_DIR MUST be relative to maven's
63+
# project.build.directory (=target) and match dist-copynativelibs
64+
# in order to be in a release
65+
set(OUT_DIR bin)
6266
else()
6367
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
68+
# using old default behavior on GCC >= 10.0
69+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcommon")
6470
set(OS_DIR ${CMAKE_SOURCE_DIR}/main/native/libhdfs/os/posix)
65-
set(OUT_DIR target/usr/local/lib)
71+
72+
# IMPORTANT: OUT_DIR MUST be relative to maven's
73+
# project.build.directory (=target) and match dist-copynativelibs
74+
# in order to be in a release
75+
set(OUT_DIR native/target/usr/local/lib)
6676
endif()
6777

6878
# Configure JNI.
@@ -138,6 +148,7 @@ endif()
138148

139149
add_subdirectory(main/native/libhdfs)
140150
add_subdirectory(main/native/libhdfs-tests)
151+
add_subdirectory(main/native/libhdfs-examples)
141152

142153
# Temporary fix to disable Libhdfs++ build on older systems that do not support thread_local
143154
include(CheckCXXSourceCompiles)
@@ -181,4 +192,4 @@ else()
181192
if(REQUIRE_FUSE)
182193
message(FATAL_ERROR "Required component fuse_dfs could not be built.")
183194
endif()
184-
endif()
195+
endif()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
20+
21+
include_directories(
22+
${CMAKE_CURRENT_SOURCE_DIR}/../libhdfs/include
23+
${GENERATED_JAVAH}
24+
${CMAKE_BINARY_DIR}
25+
${CMAKE_CURRENT_SOURCE_DIR}/../libhdfs
26+
${JNI_INCLUDE_DIRS}
27+
${OS_DIR}
28+
)
29+
30+
add_executable(hdfs_read libhdfs_read.c)
31+
target_link_libraries(hdfs_read hdfs)
32+
33+
add_executable(hdfs_write libhdfs_write.c)
34+
target_link_libraries(hdfs_write hdfs)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
The files in this directory are purely meant to provide additional examples for how to use libhdfs. They are compiled as
20+
part of the build and are thus guaranteed to compile against the associated version of lidhdfs. However, no tests exists
21+
for these examples so their functionality is not guaranteed.
22+
23+
The examples are written to run against a mini-dfs cluster. The script `test-libhdfs.sh` can setup a mini DFS cluster
24+
that the examples can run against. Again, none of this is tested and is thus not guaranteed to work.
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "hdfs/hdfs.h"
19+
#include "hdfs/hdfs.h"
2020

2121
#include <stdio.h>
2222
#include <stdlib.h>
2323

24+
/**
25+
* An example of using libhdfs to read files. The usage of this file is as follows:
26+
*
27+
* Usage: hdfs_read <filename> <filesize> <buffersize>
28+
*/
2429
int main(int argc, char **argv) {
2530
hdfsFS fs;
2631
const char *rfile = argv[1];
@@ -33,12 +38,12 @@ int main(int argc, char **argv) {
3338
fprintf(stderr, "Usage: hdfs_read <filename> <filesize> <buffersize>\n");
3439
exit(-1);
3540
}
36-
41+
3742
fs = hdfsConnect("default", 0);
3843
if (!fs) {
3944
fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
4045
exit(-1);
41-
}
46+
}
4247

4348
readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0);
4449
if (!readFile) {
@@ -51,13 +56,13 @@ int main(int argc, char **argv) {
5156
if(buffer == NULL) {
5257
return -2;
5358
}
54-
59+
5560
// read from the file
5661
curSize = bufferSize;
5762
for (; curSize == bufferSize;) {
5863
curSize = hdfsRead(fs, readFile, (void*)buffer, curSize);
5964
}
60-
65+
6166

6267
free(buffer);
6368
hdfsCloseFile(fs, readFile);
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "hdfs/hdfs.h"
19+
#include "hdfs/hdfs.h"
2020

2121
#include <limits.h>
2222
#include <stdio.h>
2323
#include <stdlib.h>
2424
#include <sys/types.h>
2525

26+
/**
27+
* An example of using libhdfs to write files. The usage of this file is as follows:
28+
*
29+
* Usage: hdfs_write <filename> <filesize> <buffersize>
30+
*/
2631
int main(int argc, char **argv) {
2732
hdfsFS fs;
2833
const char *writeFileName = argv[1];
@@ -40,12 +45,12 @@ int main(int argc, char **argv) {
4045
fprintf(stderr, "Usage: hdfs_write <filename> <filesize> <buffersize>\n");
4146
exit(-1);
4247
}
43-
48+
4449
fs = hdfsConnect("default", 0);
4550
if (!fs) {
4651
fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
4752
exit(-1);
48-
}
53+
}
4954

5055
// sanity check
5156
if(fileTotalSize == ULONG_MAX && errno == ERANGE) {
@@ -79,7 +84,7 @@ int main(int argc, char **argv) {
7984

8085
// write to the file
8186
for (nrRemaining = fileTotalSize; nrRemaining > 0; nrRemaining -= bufferSize ) {
82-
curSize = ( bufferSize < nrRemaining ) ? bufferSize : (tSize)nrRemaining;
87+
curSize = ( bufferSize < nrRemaining ) ? bufferSize : (tSize)nrRemaining;
8388
if ((written = hdfsWrite(fs, writeFile, (void*)buffer, curSize)) != curSize) {
8489
fprintf(stderr, "ERROR: hdfsWrite returned an error on write: %d\n", written);
8590
exit(-3);

hadoop-hdfs-project/hadoop-hdfs/src/main/native/tests/test-libhdfs.sh renamed to hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-examples/test-libhdfs.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ $HADOOP_HOME/share/hadoop/common/
7070
$HADOOP_HOME/share/hadoop/hdfs
7171
$HADOOP_HOME/share/hadoop/hdfs/lib/"
7272

73-
for d in $JAR_DIRS; do
73+
for d in $JAR_DIRS; do
7474
for j in $d/*.jar; do
7575
CLASSPATH=${CLASSPATH}:$j
7676
done;
@@ -114,14 +114,14 @@ LIB_JVM_DIR=`findlibjvm`
114114
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
115115
echo LIB_JVM_DIR = $LIB_JVM_DIR
116116
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
117-
# Put delays to ensure hdfs is up and running and also shuts down
117+
# Put delays to ensure hdfs is up and running and also shuts down
118118
# after the tests are complete
119119
rm $HDFS_TEST_CONF_DIR/core-site.xml
120120

121121
$HADOOP_HOME/bin/hadoop jar $HDFS_TEST_JAR \
122122
org.apache.hadoop.test.MiniDFSClusterManager \
123123
-format -nnport 20300 -writeConfig $HDFS_TEST_CONF_DIR/core-site.xml \
124-
> /tmp/libhdfs-test-cluster.out 2>&1 &
124+
> /tmp/libhdfs-test-cluster.out 2>&1 &
125125

126126
MINI_CLUSTER_PID=$!
127127
for i in {1..15}; do

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/CMakeLists.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ set_target_properties(hdfs PROPERTIES
5555
SOVERSION ${LIBHDFS_VERSION})
5656

5757
build_libhdfs_test(test_libhdfs_ops hdfs_static test_libhdfs_ops.c)
58-
link_libhdfs_test(test_libhdfs_ops hdfs_static ${JAVA_JVM_LIBRARY})
59-
build_libhdfs_test(test_libhdfs_reads hdfs_static test_libhdfs_read.c)
60-
link_libhdfs_test(test_libhdfs_reads hdfs_static ${JAVA_JVM_LIBRARY})
61-
build_libhdfs_test(test_libhdfs_write hdfs_static test_libhdfs_write.c)
62-
link_libhdfs_test(test_libhdfs_write hdfs_static ${JAVA_JVM_LIBRARY})
58+
link_libhdfs_test(test_libhdfs_ops hdfs_static native_mini_dfs ${JAVA_JVM_LIBRARY})
59+
add_libhdfs_test(test_libhdfs_ops hdfs_static)
60+
6361
build_libhdfs_test(test_libhdfs_threaded hdfs_static expect.c test_libhdfs_threaded.c ${OS_DIR}/thread.c)
6462
link_libhdfs_test(test_libhdfs_threaded hdfs_static native_mini_dfs)
6563
add_libhdfs_test(test_libhdfs_threaded hdfs_static)

0 commit comments

Comments
 (0)