public static void write(String file, String valueOfMap1, String valueOfMap2) {
try {
BufferedWriter input = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
input.write("Map1Output: " + valueOfMap1 + "rn");
input.write("Map2Output: " + valueOfmap2 + "rn");
input.newLine();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Hadoop的HDFS和MapReduce子框架主要是针对大数据文件来设计的,在小文件的处理上不但效率低下,而且十分消耗内存资源(每一个小文件占用一个Block,每一个block的元数据都存储在namenode的内存里)。解决办法通常是选择一个容器,将这些小文件组织起来统一存储。HDFS提供了两种类型的容器,分别是SequenceFile和MapFile。
Map的三种遍历方式:
所用到的第三方库:xlrd(读取Excel文件)、xlwt(写入Excel文件)、xlutils(操作Excel文件的使用工具,如复制、分割。筛选等)
一、SequenceFile
SequenceFile的存储类似于Log文件,所不同的是Log
File的每条记录的是纯文本数据,而SequenceFile的每条记录是可序列化的字符数组。
SequenceFile可通过如下API来完成新记录的添加操作:
fileWriter.append(key,value)
可以看到,每条记录以键值对的方式进行组织,但前提是Key和Value需具备序列化和反序列化的功能
Hadoop预定义了一些Key Class和Value
Class,他们直接或间接实现了Writable接口,满足了该功能,包括:
Text 等同于Java中的String
IntWritable 等同于Java中的Int
BooleanWritable 等同于Java中的Boolean
.
.
在存储结构上,SequenceFile主要由一个Header后跟多条Record组成,如图所示:
Header主要包含了Key classname,Value
classname,存储压缩算法,用户自定义元数据等信息,此外,还包含了一些同步标识,用于快速定位到记录的边界。
每条Record以键值对的方式进行存储,用来表示它的字符数组可依次解析成:记录的长度、Key的长度、Key值和Value值,并且Value值的结构取决于该记录是否被压缩。
数据压缩有利于节省磁盘空间和加快网络传输,SeqeunceFile支持两种格式的数据压缩,分别是:record
compression和block compression。
record compression如上图所示,是对每条记录的value进行压缩
block
compression是将一连串的record组织到一起,统一压缩成一个block,如图所示:
block信息主要存储了:块所包含的记录数、每条记录Key长度的集合、每条记录Key值的集合、每条记录Value长度的集合和每条记录Value值的集合
注:每个block的大小是可通过io.seqfile.compress.blocksize属性来指定的
示例:SequenceFile读/写 操作
[java] view
plaincopy
- Configuration conf=new Configuration();
- FileSystem fs=FileSystem.get(conf);
- Path seqFile=new Path(“seqFile.seq”);
- //Reader内部类用于文件的读取操作
- SequenceFile.Reader reader=new SequenceFile.Reader(fs,seqFile,conf);
- //Writer内部类用于文件的写操作,假设Key和Value都为Text类型
- SequenceFile.Writer writer=new SequenceFile.Writer(fs,conf,seqFile,Text.class,Text.class);
- //通过writer向文档中写入记录
- writer.append(new Text(“key”),new Text(“value”));
- IOUtils.closeStream(writer);//关闭write流
- //通过reader从文档中读取记录
- Text key=new Text();
- Text value=new Text();
- while(reader.next(key,value)){
- System.out.println(key);
- System.out.println(value);
- }
- IOUtils.closeStream(reader);//关闭read流
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("孙悟空", 1);
map.put("唐三藏", 2);
map.put("猪八戒", 3);
map.put("沙悟净", 4);
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println("键:"+ key + "值:"+ value);
}
Set<Entry<String, Integer>> set = map.entrySet();
for (Entry<String, Integer> entry : set) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("键:"+ key + "值"+value);
}
Iterator<Entry<String, Integer>> entrySet = map.entrySet().iterator();
while (entrySet.hasNext()) {
Entry<String, Integer> entry = entrySet.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println("键:" + key + " " + "值:" + value);
}
}
3、打开已存在Excel文件,写入信息
import xlrd
from xlutils import copy
data = xlrd.open_workbook(‘G:/221.xls’,formatting_info=True)
#保证修改后的文件格式不变w= copy(data)
news =w.get_sheet(0)
news.write(3,3,’str’)
try:
w.save(‘G:/221.xls’)
except(SyntaxError,PermissionError):
print(“文件未关闭!”)
三种方法的思维都是遍历一个map的Key,然后2个Map分别取这2个Key值所得到的Value。
// map遍历操作的第二种方式:map.entrySet();+foreach 方法进行遍历
Set<Entry<String, Integer>> set = map.entrySet();
for (Entry<String, Integer> entry : set) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(“键:”+ key + “值”+value);
}
1、读取Excel
import xlrd
data = xlrd.open_workbook(‘G:/221.xls’)
table = data.sheets()[0]
print(table.row_云顶娱乐平台,values(1)) #获取整行的值(横)
print(table.col_values(0)) #获取整列的值(竖)
nrows = table.nrows #获取表格行数(横)
ncols = table.ncols #获取表格列数(竖)
cell_a = table.cell(0,1).value#(竖、横)
#遍历Excel
for row in range(nrows):
for col in range(ncols):
print(“(%s,%s): %s”%(row,col,table.cell(row,col).value))
在main方法调用并传递参就可以了,
二、MapFile
MapFile是排序后的SequenceFile,通过观察其目录结构可以看到MapFile由两部分组成,分别是data和index。
index作为文件的数据索引,主要记录了每个Record的key值,以及该Record在文件中的偏移位置。在MapFile被访问的时候,索引文件会被加载到内存,通过索引映射关系可迅速定位到指定Record所在文件位置,因此,相对SequenceFile而言,MapFile的检索效率是高效的,缺点是会消耗一部分内存来存储index数据。
需注意的是,MapFile并不会把所有Record都记录到index中去,默认情况下每隔128条记录存储一个索引映射。当然,记录间隔可人为修改,通过MapFIle.Writer的setIndexInterval()方法,或修改io.map.index.interval属性;
另外,与SequenceFile不同的是,MapFile的KeyClass一定要实现WritableComparable接口,即Key值是可比较的。
示例:MapFile读写操作
[java] view
plaincopy
- Configuration conf=new Configuration();
- FileSystem fs=FileSystem.get(conf);
- Path mapFile=new Path(“mapFile.map”);
- //Reader内部类用于文件的读取操作
- MapFile.Reader reader=new MapFile.Reader(fs,mapFile.toString(),conf);
- //Writer内部类用于文件的写操作,假设Key和Value都为Text类型
- MapFile.Writer writer=new MapFile.Writer(conf,fs,mapFile.toString(),Text.class,Text.class);
- //通过writer向文档中写入记录
- writer.append(new Text(“key”),new Text(“value”));
- IOUtils.closeStream(writer);//关闭write流
- //通过reader从文档中读取记录
- Text key=new Text();
- Text value=new Text();
- while(reader.next(key,value)){
- System.out.println(key);
- System.out.println(key);
- }
- IOUtils.closeStream(reader);//关闭read流
注意:使用MapFile或SequenceFile虽然可以解决HDFS中小文件的存储问题,但也有一定局限性,如:
1.文件不支持复写操作,不能向已存在的SequenceFile(MapFile)追加存储记录
2.当write流不关闭的时候,没有办法构造read流。也就是在执行文件写操作的时候,该文件是不可读取的
// map遍历操作的第一种方法:keyset + foreach
// 采用keyset方法通过key值获取value值—》效率低
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(“键:”+ key + “值:”+ value);
}
2、写入新Excel文件
import xlwt
wb = xlwt.Workbook() # 创建 xls 文件对象
sh = wb.add_sheet(‘A Test Sheet’) # 新增一个表单
# 按位置添加数据
sh.write(0,0,1234.56)
sh.write(1,0,8888)
sh.write(2,0,’hello’)
sh.write(2,1,’world’)
wb.save(‘example.xls’) # 保存文件
private void compareMap(Map<String, String> Map01, Map<String, String Map02>){
for (Map.Entry<String, String> entry : Map1.entrySet())
{
String testKey = entry.getKey();
if(Map1.get(testId).equals(Map2.get(testId))){
System.out.println("equals");
}else{
System.out.println("not equals");
}
}
}
// 第一种方式遍历list集合:for循环
Integer size = list.size();
for (int i = 0; i < size; i++) {
String value1 = list.get(i);
System.out.println(value1);
}
// 第二种方式遍历list集合:iterator 迭代器
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String value2 = iterator.next();
System.out.println(value2);
}
// 第三种方式遍历list集合:foreach
for (String string : list) {
System.out.println(string);
}
// map遍历操作的第三种方式:map.entrySet() +迭代器
Iterator<Entry<String, Integer>> entrySet =
map.entrySet().iterator();
while (entrySet.hasNext()) {
Entry<String, Integer> entry = entrySet.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(“键:” + key + ” ” + “值:” + value);
}
}
所有的方法都是卸载main方法之中的。
PS:如果需要将结果(相同的value、不同的value)写入文件,则可以写一个write方法,在每次打印写入文件,加文件地址参数即可
List的三种遍历方式:
public class TestList {
private void compareMap(Map<String, String> Map01, Map<String, String Map02>){
Iterator<String> iter = Map1.keySet().iterator();
while (iter.hasNext()) {
String testKey = iter.next();
if(Map1.get(testId).equals(Map2.get(testId))){
System.out.println("equals");
}else{
System.out.println("not equals");
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("孙悟空");
list.add("唐三藏");
list.add("猪八戒");
list.add("沙悟净");
String listString = list.toString();
System.out.println(listString);
Integer size = list.size();
for (int i = 0; i < size; i++) {
String value1 = list.get(i);
System.out.println(value1);
}
System.out.println("==========================");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String value2 = iterator.next();
System.out.println(value2);
}
System.out.println("==========================");
for (String string : list) {
System.out.println(string);
}
}}
结果写入文件方法write
#第一种用entry
#第二种用keyset的方法,把key值存到容器,分别取出对比
##第二种用keyset的方法,遍历Key值
public class CompareMap {
public static void main(String[] args) {
}
private void compare(Map<String, String> map1, Map<String, String> map2) {
for (String testkey : map1.keySet()) {
if(map1.get(testkey).equals(map2.get(testkey))){
System.out.println("equals");
}else{
System.out.println("not equals");
}
}
}