2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
5 package com
.yahoo
.pig
.impl
.io
;
7 import java
.io
.BufferedOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
10 import java
.util
.Iterator
;
12 import com
.yahoo
.pig
.StorageFunc
;
13 import com
.yahoo
.pig
.builtin
.PigStorage
;
14 import com
.yahoo
.pig
.data
.BagFactory
;
15 import com
.yahoo
.pig
.data
.DataBag
;
16 import com
.yahoo
.pig
.data
.Tuple
;
17 import com
.yahoo
.pig
.PigServer
.ExecType
;
19 public class PigFile
{
20 private String file
= null;
21 private String delimiterExpression
= null;
22 boolean append
= false;
24 public PigFile(String filename
, boolean append
) {
29 public PigFile(String filename
){
33 public PigFile(String filename
, String delimiterExpression
) {
35 this.delimiterExpression
= delimiterExpression
;
38 public DataBag
load(ExecType execType
) throws IOException
{
40 StorageFunc lfunc
= delimiterExpression
== null ?
new PigStorage() : new PigStorage(delimiterExpression
);
41 return load(execType
, lfunc
);
44 public DataBag
load(ExecType execType
, StorageFunc lfunc
) throws IOException
{
45 DataBag content
= BagFactory
.getInstance().getNewBag();
46 InputStream is
= FileLocalizer
.open(execType
, file
);
47 lfunc
.bindTo(is
, 0, Long
.MAX_VALUE
);
49 while ((f
= lfunc
.getNext()) != null) {
55 public void store(ExecType execType
, DataBag data
) throws IOException
{
56 store(execType
, data
, new PigStorage());
59 public void store(ExecType execType
, DataBag data
, StorageFunc sfunc
) throws IOException
{
60 BufferedOutputStream bos
= new BufferedOutputStream(FileLocalizer
.create(execType
, file
, append
));
62 for (Iterator
<Tuple
> it
= data
.content(); it
.hasNext();) {
63 Tuple row
= it
.next();