DATAFU-18: Create datafu.util.RandomUUID UDF
https://issues.apache.org/jira/browse/DATAFU-18
Signed-off-by: Matt Hayes <mhayes@linkedin.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-datafu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-datafu/commit/90c754e0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-datafu/tree/90c754e0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-datafu/diff/90c754e0
Branch: refs/heads/master
Commit: 90c754e08c997f12f0c794fc67cfa3f354793a2b
Parents: 41a0c2c
Author: Russell Jurney <russell.jurney@gmail.com>
Authored: Wed Jan 29 15:09:21 2014 -0800
Committer: Matt Hayes <mhayes@linkedin.com>
Committed: Wed Jan 29 15:09:25 2014 -0800
----------------------------------------------------------------------
src/java/datafu/pig/random/RandomUUID.java | 46 +++++++++++++
test/pig/datafu/test/pig/random/UUIDTests.java | 73 +++++++++++++++++++++
2 files changed, 119 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-datafu/blob/90c754e0/src/java/datafu/pig/random/RandomUUID.java
----------------------------------------------------------------------
diff --git a/src/java/datafu/pig/random/RandomUUID.java b/src/java/datafu/pig/random/RandomUUID.java
new file mode 100644
index 0000000..d63f4cf
--- /dev/null
+++ b/src/java/datafu/pig/random/RandomUUID.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package datafu.pig.random;
+
+import java.io.IOException;
+import java.util.UUID;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.builtin.Nondeterministic;
+import org.apache.pig.data.*;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+
+/**
+ * Generates a random UUID using java.util.UUID
+ */
+@Nondeterministic
+public class RandomUUID extends EvalFunc<String>
+{
+ public String exec(Tuple input) throws IOException
+ {
+ return UUID.randomUUID().toString();
+ }
+
+ @Override
+ public Schema outputSchema(Schema input)
+ {
+ return new Schema(new Schema.FieldSchema("uuid", DataType.CHARARRAY));
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-datafu/blob/90c754e0/test/pig/datafu/test/pig/random/UUIDTests.java
----------------------------------------------------------------------
diff --git a/test/pig/datafu/test/pig/random/UUIDTests.java b/test/pig/datafu/test/pig/random/UUIDTests.java
new file mode 100644
index 0000000..e199760
--- /dev/null
+++ b/test/pig/datafu/test/pig/random/UUIDTests.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package datafu.test.pig.random;
+
+import datafu.test.pig.PigTests;
+import junit.framework.Assert;
+import org.adrianwalker.multilinestring.Multiline;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.pigunit.PigTest;
+import org.testng.annotations.Test;
+
+import java.util.*;
+
+import static org.testng.Assert.assertTrue;
+
+public class UUIDTests extends PigTests
+{
+ /**
+ register $JAR_PATH
+
+ define RandomUUID datafu.pig.random.RandomUUID();
+
+ data = LOAD 'input' AS (key: chararray);
+ DUMP data
+
+ data2 = FOREACH data GENERATE key, RandomUUID() as val;
+ DUMP data2
+
+ STORE data2 INTO 'output';
+ */
+ @Multiline private String randomUUIDTest;
+
+ /**
+ * Test the RandomUUID UDF. The main purpose is to make sure it can be used in a Pig
script.
+ * Also the range of length of values is tested.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void randomUUIDTest() throws Exception
+ {
+ PigTest test = createPigTestFromString(randomUUIDTest);
+
+ writeLinesToFile("input",
+ "input1",
+ "input2",
+ "input3");
+
+ List<Tuple> tuples = getLinesForAlias(test, "data2", true);
+ Set<UUID> set = new HashSet<UUID>();
+ for (Tuple tuple : tuples)
+ {
+ set.add(UUID.fromString((String)tuple.get(1)));
+ }
+ Assert.assertEquals(set.size(), 3);
+ }
+}
|