使用Go语言操作HDFS
HDFS(Hadoop分布式文件系统)是Hadoop生态系统的一部分,它是一个可扩展的分布式文件系统,被设计用于在大规模数据集上运行的应用程序
安装相关package:
$ go get github.com/colinmarc/hdfs/v2
创建目录
命令:
$ hdfs dfs -mkdir <path>
示例代码:
package main
import (
"fmt"
"github.com/colinmarc/hdfs"
)
const address = "master:9000"
func main() {
client, err := hdfs.New(address)
if err != nil {
panic(err)
}
path := "/testdir"
err = client.MkdirAll(path, 0777) // 创建testdir目录
if err != nil {
panic(err)
}
fmt.Printf("Created directory: %s\n", path)
}
上传文件
命令:
$ hdfs dfs -put <localpath> <hdfspath>
示例代码:
package main
import (
"fmt"
"github.com/colinmarc/hdfs"
"io"
"os"
)
const address = "master:9000"
func main() {
client, err := hdfs.New(address)
if err != nil {
panic(err)
}
localPath := "./file.txt"
hdfsPath := "/testdir/file.txt"
// 打开本地文件
localFile, err := os.Open(localPath)
if err != nil {
panic(err)
}
defer localFile.Close()
// 创建HDFS文件
hdfsFile, err := client.Create(hdfsPath)
if err != nil {
panic(err)
}
defer hdfsFile.Close()
// 将本地文件复制到HDFS
_, err = io.Copy(hdfsFile, localFile)
if err != nil {
panic(err)
}
fmt.Printf("Uploaded file: %s\n", hdfsPath)
}
下载文件
$ hdfs dfs -get <hdfspath> <localpath>
示例代码:
package main
import (
"fmt"
"github.com/colinmarc/hdfs"
"io"
"os"
)
const address = "master:9000"
func main() {
client, err := hdfs.New(address)
if err != nil {
panic(err)
}
hdfsPath := "/test.txt"
localPath := "/home/ubuntu/workspace/hadoop/test.txt"
hdfsFile, err := client.Open(hdfsPath)
if err != nil {
panic(err)
}
defer hdfsFile.Close()
localFile, err := os.Create(localPath)
if err != nil {
panic(err)
}
defer localFile.Close()
_, err = io.Copy(localFile, hdfsFile)
if err != nil {
panic(err)
}
fmt.Printf("Downloaded file: %s\n", localPath)
}
查看文件列表
命令:
$ hdfs dfs -ls <path>
示例代码:
package main
import (
"fmt"
"github.com/colinmarc/hdfs"
)
const address = "master:9000"
func main() {
client, err := hdfs.New(address)
if err != nil {
panic(err)
}
hdfsPath := "/testdir"
files, err := client.ReadDir(hdfsPath)
if err != nil {
panic(err)
}
fmt.Printf("Files in %s:\n", hdfsPath)
for _, file := range files {
fmt.Printf("%s (size: %d)\n", file.Name(), file.Size())
}
}
删除文件
命令:
$ hdfs dfs -rm <path>
示例代码:
package main
import (
"fmt"
"github.com/colinmarc/hdfs"
)
const address = "master:9000"
func main() {
client, err := hdfs.New(address)
if err != nil {
panic(err)
}
hdfsPath := "/testdir"
err = client.Remove(hdfsPath)
if err != nil {
panic(err)
}
fmt.Printf("Deleted directory: %s\n", hdfsPath)
}
使用Go语言操作HDFS的更多相关文章
- java操作hdfs实例
环境:window7+eclipse+vmware虚拟机+搭建好的hadoop环境(master.slave01.slave02) 内容:主要是在windows环境下,利用eclipse如何来操作hd ...
- Hadoop操作hdfs的命令【转载】
本文系转载,原文地址被黑了,故无法贴出原始链接. Hadoop操作HDFS命令如下所示: hadoop fs 查看Hadoop HDFS支持的所有命令 hadoop fs –ls 列出目录及文件信息 ...
- Linux C语言操作MySQL
原文:Linux C语言操作MySQL 1.MySQL数据库简介 MySQL是一个开源码的小型关系数据库管理系统,体积小,速度快,总体成本低,开源.MySQL有以下特性: (1) 使用C和C++编写, ...
- 使用javaAPI操作hdfs
欢迎到https://github.com/huabingood/everyDayLanguagePractise查看源码. 一.构建环境 在hadoop的安装包中的share目录中有hadoop所有 ...
- 关于操作HDFS的一个问题
近日写程序定时任务调Hadoop MR程序,然后生成报表,发送邮件,当时起了两个任务A和B,调MR程序之前,会操作hdfs(读写都有),任务A每天一点跑,任务B每十分钟跑一次,B任务不会调用MR程序, ...
- 使用Java API操作HDFS文件系统
使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...
- 使用Eclipse来操作HDFS的文件
一.常用类 1.Configuration Hadoop配置文件的管理类,该类的对象封装了客户端或者服务器的配置(配置集群时,所有的xml文件根节点都是configuration) 创建一个Confi ...
- Go语言操作MySQL数据库
Go语言操作MySQL数据库 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用 ...
- Hadoop Java API操作HDFS文件系统(Mac)
1.下载Hadoop的压缩包 tar.gz https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/stable/ 2.关联jar包 在 ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
随机推荐
- Spring 核心容器 IOC
目录 1. BeanFactory 2. BeanDefinition 3. BeanDefinitionReader 4 . Web IOC 容器初体验 一 .BeanFactory Spring ...
- STL练习-ACboy needs your help again!
ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image how dark ...
- js对象常用的方法
1. Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象,它将返回目标对象. 语法: Object.assign(target, ...sources) ...
- 第十二组 -摩天脆脆冰淇淋队-第四次团队作业:Git实战
这个作业属于哪个课程 至诚软工实践F班 - 福州大学至诚学院 - 班级博客 - 博客园 (cnblogs.com) 这个作业要求在哪里 第四次团队作业:Git实战 - 作业 - 至诚软工实践F班 - ...
- bitmap_find_next_zero_area_off函数
备注:
- C# async、await、Task 探讨
test02.ProntOut(); ///*第五题*/ static class test02 { public static async void ProntOut() { Console.Wri ...
- java8 stream按某个字段分组,允许分组字段是null
Map<String, List<Dto>> deviceMap = deviceList.stream().collect(Collectors.groupingBy(Dto ...
- 一个小数据库SQLite
参考 https://blog.csdn.net/csdnhsh/article/details/93376733 https://www.runoob.com/sqlite/sqlite-creat ...
- IBM MQ 配置SSL 连接
图示为思路: 下面介绍具体的步骤 参考文档: https://www.jianshu.com/p/2865965a42d9 http://www.hackdig.com/?01/hack-7976.h ...
- 手把手教你基于luatos的4G(LTE Cat.1)模组接入华为云物联网平台
摘要:本期文章采用了4G LTE Cat.1模块,编程语言用的是lua,实现对华为云物联网平台的设备通信与控制 本文分享自华为云社区<基于luatos的4G(LTE Cat.1)模组接入华为云物 ...